NO-TICKET +api integration test: register device, state-policy request, sends state...
authorm.dalakov <m.dalakov@samsung.com>
Mon, 2 Apr 2018 15:31:35 +0000 (18:31 +0300)
committerm.dalakov <m.dalakov@samsung.com>
Mon, 2 Apr 2018 15:33:15 +0000 (18:33 +0300)
12 files changed:
server/samserver/pom.xml
server/samserver/src/main/java/com/samsung/samserver/service/impl/GeoIPService.java
server/samserver/src/main/java/com/samsung/samserver/web/rest/service/vm/UIDevice.java
server/samserver/src/main/java/com/samsung/samserver/web/rest/service/vm/UIPolicies.java
server/samserver/src/test/java/com/samsung/samserver/integration/BaseApi.java [moved from server/samserver/src/test/java/com/samsung/samserver/integration/RestApi.java with 54% similarity]
server/samserver/src/test/java/com/samsung/samserver/integration/TestScenario1.java
server/samserver/src/test/java/com/samsung/samserver/integration/TestScenario2.java
server/samserver/src/test/resources/test-data/TestScenario1/in-login [new file with mode: 0644]
server/samserver/src/test/resources/test-data/TestScenario1/in-registerDevice [moved from server/samserver/src/test/resources/test-data/TestScenario1/in with 100% similarity]
server/samserver/src/test/resources/test-data/TestScenario1/in-registerUser [new file with mode: 0644]
server/samserver/src/test/resources/test-data/TestScenario1/out-registerDevice [moved from server/samserver/src/test/resources/test-data/TestScenario1/out with 100% similarity]
server/samserver/src/test/resources/test-data/info

index 265b51f..459e859 100644 (file)
             <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>
index 9738edd..7076a29 100644 (file)
@@ -47,6 +47,7 @@ public class GeoIPService {
     }
 
     @Getter @Setter @ToString
+    @AllArgsConstructor @NoArgsConstructor
     public static class GeoIP {
         private String ip;
         @JsonProperty("country_code")
index a23cf92..337c8c0 100644 (file)
@@ -17,7 +17,7 @@ import lombok.*;
  * @version 1.0
  */
 @Getter @Setter @ToString
-@Builder
+@Builder @AllArgsConstructor @NoArgsConstructor
 public class UIDevice {
 
     private Long id;
@@ -39,14 +39,14 @@ public class UIDevice {
     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;
index 6188cba..d7da17b 100644 (file)
@@ -34,6 +34,7 @@ public class UIPolicies {
     private String group;
 
     private static List<String> ordered = Arrays.asList("common", "wifi", "bluetooth", "password");
+    @JsonIgnore
     public Integer getOrdered() {
         return ordered.indexOf(group);
     }
@@ -14,6 +14,8 @@ import org.apache.commons.io.IOUtils;
 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;
 
@@ -21,11 +23,14 @@ 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";
@@ -38,8 +43,7 @@ public abstract class RestApi {
         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)
@@ -99,4 +103,76 @@ public abstract class RestApi {
         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
index 6f98339..ee4ba57 100644 (file)
@@ -6,7 +6,10 @@
 
 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;
@@ -22,6 +25,7 @@ import org.springframework.http.HttpStatus;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import java.nio.charset.StandardCharsets;
+import java.util.*;
 
 /**
  * Integration Test for register Device.
@@ -29,7 +33,7 @@ import java.nio.charset.StandardCharsets;
  */
 @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);
 
@@ -49,13 +53,130 @@ public class TestScenario1 extends RestApi{
 
         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
index c5ae5da..7040cfd 100644 (file)
@@ -36,7 +36,7 @@ import java.util.List;
  */
 @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);
 
@@ -56,7 +56,7 @@ public class TestScenario2 extends RestApi {
 
         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";
@@ -66,19 +66,20 @@ public class TestScenario2 extends RestApi {
         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>>(){});
@@ -93,14 +94,14 @@ public class TestScenario2 extends RestApi {
         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());
diff --git a/server/samserver/src/test/resources/test-data/TestScenario1/in-login b/server/samserver/src/test/resources/test-data/TestScenario1/in-login
new file mode 100644 (file)
index 0000000..c8c3868
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "email": "test@test.com.ua",
+  "password": "test12"
+}
\ No newline at end of file
diff --git a/server/samserver/src/test/resources/test-data/TestScenario1/in-registerUser b/server/samserver/src/test/resources/test-data/TestScenario1/in-registerUser
new file mode 100644 (file)
index 0000000..76755e7
--- /dev/null
@@ -0,0 +1,6 @@
+{
+  "fullName": "test",
+  "email": "test@test.com.ua",
+  "password": "test12",
+  "confirmPassword": "test12"
+}
index e367a61..7bca531 100644 (file)
@@ -1,9 +1,12 @@
-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