-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for HTTP and MQTT communication
Add missing dependencies
- Loading branch information
Showing
18 changed files
with
904 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/io/patriot_framework/generator/device/consumer/http/HttpDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2021 Patriot project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.patriot_framework.generator.device.consumer.http; | ||
|
||
import io.patriot_framework.generator.device.consumer.exceptions.ConsumerException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class HttpDataTest extends HttpTestBase { | ||
|
||
@Test | ||
void checkHttpData() throws ConsumerException, IOException { | ||
super.runServer(); | ||
|
||
createHttpClientWithPayload("payload", port, "text/plain"); | ||
httpClient.execute(httpPost); | ||
|
||
HttpData data = (HttpData) storage.get(); | ||
HttpMeta meta = data.getMeta(); | ||
|
||
assertEquals(server.getUUID(), meta.getUUID()); | ||
|
||
LocalDateTime timestamp = meta.getTimestamp(); | ||
assertTrue(timestamp.compareTo(LocalDateTime.now().minusSeconds(3)) >= 0); | ||
|
||
assertEquals("POST", meta.getRequestMethod()); | ||
assertEquals("/endpoint", meta.getEndpoint()); | ||
assertEquals("HTTP/1.1", meta.getProtocolVersion()); | ||
|
||
Map<String, String> headers = meta.getHeaders(); | ||
assertEquals("text/plain", headers.get("Content-type")); | ||
|
||
Map<String, List<String>> queryParams = meta.getQueryParams(); | ||
assertEquals("value1", queryParams.get("key1").get(0)); | ||
assertEquals("value2", queryParams.get("key1").get(1)); | ||
assertEquals("value3", queryParams.get("key2").get(0)); | ||
|
||
assertArrayEquals("payload".getBytes(), data.getPayload()); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...t/java/io/patriot_framework/generator/device/consumer/http/HttpIncompleteRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2021 Patriot project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.patriot_framework.generator.device.consumer.http; | ||
|
||
import io.patriot_framework.generator.device.consumer.exceptions.ConsumerException; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class HttpIncompleteRequestTest extends HttpTestBase { | ||
|
||
public void sendIncompleteRequest() { | ||
try { | ||
String hostname = "localhost"; | ||
InetAddress addr = InetAddress.getByName(hostname); | ||
Socket socket = new Socket(addr, 8080); | ||
|
||
String payload = "Payload"; | ||
String httpMessage = "POST / HTTP/1.0\r\nAccept: */*\r\n" | ||
+ "Host: " + hostname + ":" + port + "\r\n" | ||
+ "Content-Type: text/plain\r\n" | ||
+ "Content-Length: " + payload.length() + "\r\n" | ||
+ "\r\n\r\n" +payload + "\r\n\r\n"; | ||
|
||
if(socket.isConnected()) { | ||
System.out.println("Socket is connected to: " + socket.getInetAddress() + " on port: " + socket.getPort()); | ||
|
||
OutputStream os = socket.getOutputStream(); | ||
os.write(httpMessage.getBytes("ASCII")); | ||
os.flush(); | ||
|
||
os.close(); | ||
} | ||
socket.close(); | ||
System.out.println("Socket closed"); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void timeoutRequest() throws IOException { | ||
String hostname = "localhost"; | ||
URL url = new URL("http://" + hostname +":8080"); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
|
||
String payload = "Payload"; | ||
|
||
connection.setRequestMethod("POST"); | ||
connection.setReadTimeout(3000); | ||
connection.setDoOutput(true); | ||
|
||
try (DataOutputStream writer = new DataOutputStream(connection.getOutputStream())) { | ||
for (byte ch: payload.getBytes("ASCII")) { | ||
writer.write(ch); | ||
Thread.sleep(1000); | ||
} | ||
writer.flush(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
connection.disconnect(); | ||
} | ||
} | ||
|
||
@Test | ||
@Disabled | ||
public void incompleteRequest() throws ConsumerException, IOException { | ||
runServer(); | ||
sendIncompleteRequest(); | ||
|
||
HttpData data = (HttpData) storage.get(); | ||
HttpMeta meta = data.getMeta(); | ||
|
||
assertArrayEquals("Payload".getBytes(), data.getPayload()); | ||
} | ||
|
||
@Test | ||
public void requestTimeoutTest() throws ConsumerException, IOException { | ||
runServer(); | ||
timeoutRequest(); | ||
|
||
assertNull(storage.get()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/io/patriot_framework/generator/device/consumer/http/HttpJsonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2021 Patriot project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.patriot_framework.generator.device.consumer.http; | ||
|
||
import io.patriot_framework.generator.device.consumer.exceptions.ConsumerException; | ||
import io.restassured.http.ContentType; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class HttpJsonTest extends HttpTestBase { | ||
|
||
HttpData data; | ||
JSONObject request; | ||
|
||
@Test | ||
void jsonPost() throws ConsumerException, IOException { | ||
super.runServer(); | ||
|
||
request = new JSONObject(); | ||
|
||
request.put("ID", 1); | ||
request.put("name", "Johny"); | ||
|
||
given(). | ||
contentType(ContentType.JSON). | ||
header("Content-Type", "application/json"). | ||
body(request.toString()). | ||
when(). | ||
post("http://localhost:8080"). | ||
then(). | ||
statusCode(200). | ||
statusLine("HTTP/1.1 200 OK"); | ||
|
||
data = (HttpData) storage.get(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/test/java/io/patriot_framework/generator/device/consumer/http/HttpTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2021 Patriot project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.patriot_framework.generator.device.consumer.http; | ||
|
||
import io.patriot_framework.generator.device.consumer.Storage; | ||
import io.patriot_framework.generator.device.consumer.exceptions.ConsumerException; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.junit.jupiter.api.AfterEach; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public abstract class HttpTestBase { | ||
|
||
Server server; | ||
static int port = 8080; | ||
Storage storage = new Storage(); | ||
HttpClient httpClient; | ||
HttpPost httpPost; | ||
|
||
void createHttpClientWithPayload(String payload, int port, String contentType) throws UnsupportedEncodingException { | ||
httpClient = HttpClients.createDefault(); | ||
|
||
httpPost = new HttpPost("http://localhost:" + port + "/endpoint?key1=value1&key1=value2&key2=value3"); | ||
httpPost.setHeader("Content-type", contentType); | ||
httpPost.setEntity(new StringEntity(payload)); | ||
} | ||
|
||
// TODO ? @mijaros -> @BeforeAll | ||
void runServer() throws ConsumerException, IOException { | ||
storage = new Storage(); | ||
server = new Server("localhost", port, storage); | ||
server.start(); | ||
} | ||
|
||
void runServer(SSLInit sslInit) throws ConsumerException, IOException { | ||
storage = new Storage(); | ||
server = new Server("localhost", port, storage, sslInit); | ||
server.start(); | ||
} | ||
|
||
// TODO ? @mijaros -> @AfterAll | ||
@AfterEach | ||
void closeServer() { | ||
server.stop(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/io/patriot_framework/generator/device/consumer/http/HttpsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2021 Patriot project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.patriot_framework.generator.device.consumer.http; | ||
|
||
import io.patriot_framework.generator.device.consumer.exceptions.ConsumerException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class HttpsTest extends HttpTestBase { | ||
|
||
@BeforeEach | ||
void runServer() throws ConsumerException, IOException { | ||
String passphrase = "Patriot"; | ||
SSLInit sslInit = new BasicSSLInit("src/test/resources/sslcerts/server_keystore.jks", passphrase, | ||
"src/test/resources/sslcerts/server_truststore.jks", passphrase); | ||
super.runServer(sslInit); | ||
} | ||
|
||
@Test | ||
void statusCode200(){ | ||
given().relaxedHTTPSValidation("TLS").when().post("https://localhost:" + port).then().statusCode(200); | ||
} | ||
} |
Oops, something went wrong.