Skip to content

Commit

Permalink
Add test cases for HTTP and MQTT communication
Browse files Browse the repository at this point in the history
Add missing dependencies
  • Loading branch information
tondrusk committed Nov 28, 2021
1 parent 672d9db commit a00e580
Show file tree
Hide file tree
Showing 18 changed files with 904 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,48 @@
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.1</version>
</dependency>
q
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${org.junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.moquette</groupId>
<artifactId>moquette-broker</artifactId>
<version>0.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-client</artifactId>
<version>9.4.35.v20201120</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-http-client-transport</artifactId>
<version>9.4.35.v20201120</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down Expand Up @@ -327,4 +369,11 @@
</profile>
</profiles>

<repositories>
<repository>
<id>bintray</id>
<url>http://dl.bintray.com/andsel/maven/</url>
</repository>
</repositories>

</project>
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());
}
}
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());
}
}
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();
}
}
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();
}
}
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);
}
}
Loading

0 comments on commit a00e580

Please sign in to comment.