<artifactId>jackson-module-afterburner</artifactId>
</dependency>
<dependency>
+ <groupId>com.fasterxml.jackson.datatype</groupId>
+ <artifactId>jackson-datatype-jdk8</artifactId>
+ </dependency>
+ <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
}
@Getter @Setter @ToString
+ @AllArgsConstructor @NoArgsConstructor
public static class GeoIP {
private String ip;
@JsonProperty("country_code")
* @version 1.0
*/
@Getter @Setter @ToString
-@Builder
+@Builder @AllArgsConstructor @NoArgsConstructor
public class UIDevice {
private Long id;
private GeoIPService.GeoIP geoIP;
@Getter @Setter @ToString
- @AllArgsConstructor
+ @AllArgsConstructor @NoArgsConstructor
public static class UIDeviceType {
private String name;
private String descr;
}
@Getter @Setter @ToString
- @AllArgsConstructor
+ @AllArgsConstructor @NoArgsConstructor
public static class UIDeviceOS {
private String sw;
private String group;
private static List<String> ordered = Arrays.asList("common", "wifi", "bluetooth", "password");
+ @JsonIgnore
public Integer getOrdered() {
return ordered.indexOf(group);
}
import org.springframework.core.io.ResourceLoader;
import java.nio.charset.StandardCharsets;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import static io.restassured.RestAssured.given;
* Base class for REST API Integration Tests.
*
*/
-public abstract class RestApi {
+public abstract class BaseApi {
int localServerPort;
ResourceLoader resourceLoader;
+ /*
+ Device REST API
+ */
protected Response registerDevice(String inResource) throws Exception {
String in = IOUtils.toString(resourceLoader.getResource(inResource).getInputStream(), StandardCharsets.UTF_8);
String uri = "/api/device-service/register-device";
return resp;
}
- protected Response sendData(String inResource, String duid) throws Exception {
- String in = IOUtils.toString(resourceLoader.getResource(inResource).getInputStream(), StandardCharsets.UTF_8);
+ protected Response sendData(String in, String duid) throws Exception {
String uri = "/api/device-service/send-data";
Response resp = given()
.queryParam("duid", duid)
return curi;
}
+
+ /*
+ Dashboard REST API
+ */
+ protected Response registerUser(String inResource) throws Exception {
+
+ String in = IOUtils.toString(resourceLoader.getResource(inResource).getInputStream(), StandardCharsets.UTF_8);
+ String uri = "/dashboard/auth/register";
+ Response resp = given()
+ .contentType(ContentType.JSON)
+ .body(in)
+ .when().port(this.localServerPort)
+ .post(RestAssured.baseURI + uri);
+ resp.then().log().all();
+ return resp;
+ }
+
+ protected Response login(String inResource) throws Exception {
+ String in = IOUtils.toString(resourceLoader.getResource(inResource).getInputStream(), StandardCharsets.UTF_8);
+ String uri = "/dashboard/auth/login";
+ Response resp = given()
+ .contentType(ContentType.JSON)
+ .body(in)
+ .when().port(this.localServerPort)
+ .post(RestAssured.baseURI + uri);
+ resp.then().log().all();
+ return resp;
+ }
+
+ protected Response getAllPolices(String jwt) {
+ String uri = "/dashboard/policies";
+ Response resp = given()
+ .header("Authorization", jwt)
+ .when().port(this.localServerPort)
+ .get(RestAssured.baseURI + uri);
+ resp.then().log().all();
+ return resp;
+ }
+
+ protected Response getDevicePolices(String jwt, Long id) {
+ String uri = "/dashboard/policies/device";
+ Response resp = given()
+ .header("Authorization", jwt)
+ .queryParam("id", id)
+ .when().port(this.localServerPort)
+ .get(RestAssured.baseURI + uri);
+ resp.then().log().all();
+ return resp;
+ }
+
+ protected Response getAllDevices(String jwt) {
+ String uri = "/dashboard/devices";
+ Response resp = given()
+ .header("Authorization", jwt)
+ .when().port(this.localServerPort)
+ .get(RestAssured.baseURI + uri);
+ resp.then().log().all();
+ return resp;
+ }
+
+ protected String extractBetween(String source, String first, String last) {
+ StringBuilder pattern = new StringBuilder("(?<=")
+ .append(first)
+ .append(")(.|\\n)*?(?=")
+ .append(last)
+ .append(")");
+ String out = "";
+ Matcher mduid = Pattern.compile(pattern.toString()).matcher(source);
+ if (mduid.find()) out=mduid.group();
+ return out;
+ }
+
}
\ No newline at end of file
package com.samsung.samserver.integration;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.samsung.samserver.SamserverApp;
+import com.samsung.samserver.web.rest.service.vm.*;
import io.restassured.response.Response;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.springframework.test.context.junit4.SpringRunner;
import java.nio.charset.StandardCharsets;
+import java.util.*;
/**
* Integration Test for register Device.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SamserverApp.class, webEnvironment = SpringBootTest. WebEnvironment.RANDOM_PORT)
-public class TestScenario1 extends RestApi{
+public class TestScenario1 extends BaseApi {
Logger log = LoggerFactory.getLogger(TestScenario1.class);
log.info("test {}", this.getClass().getSimpleName());
- String inResource = "classpath:test-data/"+this.getClass().getSimpleName()+"/in";
- String outResource = "classpath:test-data/"+this.getClass().getSimpleName()+"/out";
- String out = IOUtils.toString(resourceLoader.getResource(outResource).getInputStream(), StandardCharsets.UTF_8);
- Response resp = registerDevice(inResource);
- resp.then().assertThat().statusCode(HttpStatus.OK.value());
- String duid = resp.asString();
+ // 1. Device register & server prepare updates for device policies request (state-policy)
+
+ String inResourceRegisterDevice = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-registerDevice";
+ String outResourceRegisterDevice = "classpath:test-data/"+this.getClass().getSimpleName()+"/out-registerDevice";
+ String out = IOUtils.toString(resourceLoader.getResource(outResourceRegisterDevice).getInputStream(), StandardCharsets.UTF_8);
+ Response respRegisterDevice = registerDevice(inResourceRegisterDevice);
+ respRegisterDevice.then().assertThat().statusCode(HttpStatus.OK.value());
+ String duid = respRegisterDevice.asString();
Assert.assertEquals(out, duid);
+
+ // 2. Device get updates, calls "update URL", receive "policies list request" & sends data according to the list request
+
+ Response respGetUpdates = getUpdates(duid);
+ respGetUpdates.then().assertThat().statusCode(HttpStatus.OK.value());
+ String updates = respGetUpdates.asString();
+
+ ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
+ List<UInfo> infoList = mapper.readValue(updates, new TypeReference<List<UInfo>>(){});
+ String infoType = "state-policy";
+ String uri = getUriFromUInfo(infoList, infoType);
+ Assert.assertNotNull(uri);
+
+ Response respUdata = get(uri);
+ respUdata.then().assertThat().statusCode(HttpStatus.OK.value());
+ String udata = respUdata.asString();
+
+ String [] policiesRequest = extractBetween(udata,"\\[","\\]")
+ .replaceAll("\n","")
+ .replace(" ","")
+ .split("}");
+ List <String> policiesListRequest = new ArrayList<>();
+ for (String s: policiesRequest) {
+ String name = extractBetween(s,"\"name\":\"","\"");
+ if (policiesListRequest.indexOf(name)==-1) {
+ policiesListRequest.add(name);
+ }
+ }
+
+ // check that "policies list request" contains all policy types, we get all policies types from dashboard and compare
+
+ String inRegisterUserResource = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-registerUser";
+ Response respRegisterUser = registerUser(inRegisterUserResource);
+ respRegisterUser.then().assertThat().statusCode(HttpStatus.CREATED.value());
+
+ String inLoginResource = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-login";
+ Response respLogin = login(inLoginResource);
+ respLogin.then().assertThat().statusCode(HttpStatus.OK.value());
+ String jwtToken = respLogin.asString().replaceAll(" ","");
+ Assert.assertNotNull(jwtToken);
+ String jwt = "Bearer "+extractBetween(jwtToken,"\"id_token\":\"","\"");
+
+ Response respAllPolices = getAllPolices(jwt);
+ List<UIPolicies> allPoliciesList = mapper.readValue(respAllPolices.asString(), new TypeReference<List<UIPolicies>>(){});
+
+ // compare entries and size
+
+ int size = 0;
+ for (UIPolicies uiPolicies: allPoliciesList) {
+ List<UIPolicies.UIPolicy> uiPolicyList = uiPolicies.getUiPolicyList();
+ for (UIPolicies.UIPolicy uiPolicy: uiPolicyList) {
+ String name = uiPolicy.getName();
+ Assert.assertTrue("Error: policy type "+name+" not entry in \"policies list request\"",policiesListRequest.indexOf(name)>-1);
+ size++;
+ }
+ }
+ Assert.assertEquals(policiesListRequest.size(), size);
+
+ // prepare state-policy data
+
+ int c = 0;
+ StringBuilder policyData = new StringBuilder()
+ .append("{\n" +
+ " \"type\": \"state-policy\",\n" +
+ " \"data\": [");
+ for (UIPolicies uiPolicies: allPoliciesList) {
+ List<UIPolicies.UIPolicy> uiPolicyList = uiPolicies.getUiPolicyList();
+ for (UIPolicies.UIPolicy uiPolicy: uiPolicyList) {
+ policyData.append(uiPolicy.toJSONDevice());
+ c++;
+ if (c!=size) policyData.append(",");
+ }
+ }
+ policyData.append("]\n" + "}\n");
+
+ // 3. Server saves device policies information
+
+ // define our device id & check that there is not "device policies" before send "state-policy data"
+
+ Long deviceId = null;
+ Response respDevices = getAllDevices(jwt);
+ List<UIDevice> allDeviceList = mapper.readValue(respDevices.asString(), new TypeReference<List<UIDevice>>(){});
+ for (UIDevice uiDevice: allDeviceList) {
+ if (uiDevice.getDuid().equals(duid)) {
+ deviceId = uiDevice.getId();
+ break;
+ }
+ }
+ Assert.assertNotNull(deviceId);
+
+ Response respDevicePolices = getDevicePolices(jwt, deviceId);
+ List<UIPolicies> devicePolicesList = mapper.readValue(respDevicePolices.asString(), new TypeReference<List<UIPolicies>>(){});
+ size = 0;
+ for (UIPolicies uiPolicies: devicePolicesList) {
+ for (UIPolicies.UIPolicy uiPolicy: uiPolicies.getUiPolicyList()) size++;
+ }
+ Assert.assertEquals(size, 0);
+
+ // send "state-policy data" and check that server saves that device policies
+
+ sendData(policyData.toString(), duid).then().assertThat().statusCode(HttpStatus.OK.value());
+ respDevicePolices = getDevicePolices(jwt, deviceId);
+ devicePolicesList = mapper.readValue(respDevicePolices.asString(), new TypeReference<List<UIPolicies>>(){});
+ size = 0;
+ for (UIPolicies uiPolicies: devicePolicesList) {
+ for (UIPolicies.UIPolicy uiPolicy: uiPolicies.getUiPolicyList()) size++;
+ }
+ Assert.assertEquals(policiesListRequest.size(), size);
+ for (UIPolicies uiPolicies: devicePolicesList) {
+ List<UIPolicies.UIPolicy> uiPolicyList = uiPolicies.getUiPolicyList();
+ for (UIPolicies.UIPolicy uiPolicy: uiPolicyList) {
+ String name = uiPolicy.getName();
+ Assert.assertTrue("Error: policy type not entry in \"device policies\"",policiesListRequest.indexOf(name)>-1);
+ }
+ }
}
}
\ No newline at end of file
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SamserverApp.class, webEnvironment = SpringBootTest. WebEnvironment.RANDOM_PORT)
-public class TestScenario2 extends RestApi {
+public class TestScenario2 extends BaseApi {
Logger log = LoggerFactory.getLogger(TestScenario1.class);
log.info("test {}", this.getClass().getSimpleName());
- // register device
+ // 1. Device register
String inResourceRegisterDevice = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-registerDevice";
String outResourceRegisterDevice = "classpath:test-data/"+this.getClass().getSimpleName()+"/out-registerDevice";
String duid = resp.asString();
Assert.assertEquals(outRegisterDevice, duid);
- // send data
+ // 2. Device send data report, server analyze report & generate blacklist update
String inResourceSendData = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-sendData";
- resp = sendData(inResourceSendData, duid);
+ String inSendData = IOUtils.toString(resourceLoader.getResource(inResourceSendData).getInputStream(), StandardCharsets.UTF_8);
+ resp = sendData(inSendData, duid);
resp.then().assertThat().statusCode(HttpStatus.OK.value());
- // get updates
+ // 3. Device call get updates and receive "update URL" & "confirm URL"
resp = getUpdates(duid);
resp.then().assertThat().statusCode(HttpStatus.OK.value());
String updates = resp.asString();
- // get update data by uri
+ // 4. Device calls "update URL" & and receive the data for update
ObjectMapper mapper = new ObjectMapper();
List<UInfo> infoList = mapper.readValue(updates, new TypeReference<List<UInfo>>(){});
String udata = resp.asString();
Assert.assertEquals(outGetUdata, udata);
- // confirm update by curi
+ // 5. Device calls "confirm URL" and sends the confirmation that update have applied
String curi = getCuriFromUInfo(infoList, infoType);
Assert.assertNotNull(curi);
resp = post(curi);
resp.then().assertThat().statusCode(HttpStatus.OK.value());
- // check that there is no more "blacklist" data in get updates
+ // 6. Device no longer received this update after confirmation (check that there is no more "blacklist" data in get updates)
resp = getUpdates(duid);
resp.then().assertThat().statusCode(HttpStatus.OK.value());
--- /dev/null
+{
+ "email": "test@test.com.ua",
+ "password": "test12"
+}
\ No newline at end of file
--- /dev/null
+{
+ "fullName": "test",
+ "email": "test@test.com.ua",
+ "password": "test12",
+ "confirmPassword": "test12"
+}
-test scenario 1
- - register device
+Test scenario 1
+ 1. Device register & server prepare updates for device policies request (state-policy)
+ 2. Device get updates, calls "update URL", receive "policies list request" & sends data according to the list request
+ 3. Server saves device policies information
-test scenario 2
- - register device
- - send data report, analyze & generate blacklist update
- - get updates
- - get update data (udata)
- - confirm
\ No newline at end of file
+Test scenario 2
+ 1. Device register
+ 2. Device send data report, server analyze report & generate blacklist update
+ 3. Device call get updates and receive "update URL" & "confirm URL"
+ 4. Device calls "update URL" & and receive the data for update
+ 5. Device calls "confirm URL" and sends the confirmation that update have applied
+ 6. Device no longer received this update after confirmation
\ No newline at end of file