c5ae5da2ccc72ab7494bd54e21bee73d399b91c5
[platform/core/security/suspicious-activity-monitor.git] / server / samserver / src / test / java / com / samsung / samserver / integration / TestScenario2.java
1 /*
2  * In Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6
7 package com.samsung.samserver.integration;
8
9 import com.fasterxml.jackson.core.type.TypeReference;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.samsung.samserver.SamserverApp;
12 import com.samsung.samserver.web.rest.service.vm.UInfo;
13 import io.restassured.response.Response;
14 import org.apache.commons.io.IOUtils;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.boot.context.embedded.LocalServerPort;
22 import org.springframework.boot.test.context.SpringBootTest;
23 import org.springframework.core.io.ResourceLoader;
24 import org.springframework.http.HttpStatus;
25 import org.springframework.test.context.junit4.SpringRunner;
26
27 import java.nio.charset.StandardCharsets;
28 import java.util.List;
29
30 /**
31  * Integration Test for
32  * register device
33  * send data report, analyze & generate blacklist update
34  * get update & confirm
35  *
36  */
37 @RunWith(SpringRunner.class)
38 @SpringBootTest(classes = SamserverApp.class, webEnvironment = SpringBootTest. WebEnvironment.RANDOM_PORT)
39 public class TestScenario2 extends RestApi {
40
41     Logger log = LoggerFactory.getLogger(TestScenario1.class);
42
43     @LocalServerPort
44     void setLocalServerPort(int localServerPort) {
45         this.localServerPort = localServerPort;
46     }
47
48     @Autowired
49     void setResourceLoader(ResourceLoader resourceLoader) {
50         this.resourceLoader = resourceLoader;
51     }
52
53
54     @Test
55     public void test() throws Exception {
56
57         log.info("test {}", this.getClass().getSimpleName());
58
59         // register device
60
61         String inResourceRegisterDevice = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-registerDevice";
62         String outResourceRegisterDevice = "classpath:test-data/"+this.getClass().getSimpleName()+"/out-registerDevice";
63         String outRegisterDevice = IOUtils.toString(resourceLoader.getResource(outResourceRegisterDevice).getInputStream(), StandardCharsets.UTF_8);
64         Response resp = registerDevice(inResourceRegisterDevice);
65         resp.then().assertThat().statusCode(HttpStatus.OK.value());
66         String duid = resp.asString();
67         Assert.assertEquals(outRegisterDevice, duid);
68
69         // send data
70
71         String inResourceSendData = "classpath:test-data/"+this.getClass().getSimpleName()+"/in-sendData";
72         resp = sendData(inResourceSendData, duid);
73         resp.then().assertThat().statusCode(HttpStatus.OK.value());
74
75         // get updates
76
77         resp = getUpdates(duid);
78         resp.then().assertThat().statusCode(HttpStatus.OK.value());
79         String updates = resp.asString();
80
81         // get update data by uri
82
83         ObjectMapper mapper = new ObjectMapper();
84         List<UInfo> infoList = mapper.readValue(updates, new TypeReference<List<UInfo>>(){});
85         String infoType = "blacklist";
86         String uri = getUriFromUInfo(infoList, infoType);
87         Assert.assertNotNull(uri);
88
89         String outResourceGetUdata = "classpath:test-data/"+this.getClass().getSimpleName()+"/out-getUdata";
90         String outGetUdata = IOUtils.toString(resourceLoader.getResource(outResourceGetUdata).getInputStream(), StandardCharsets.UTF_8);
91         resp = get(uri);
92         resp.then().assertThat().statusCode(HttpStatus.OK.value());
93         String udata = resp.asString();
94         Assert.assertEquals(outGetUdata, udata);
95
96         // confirm update by curi
97
98         String curi = getCuriFromUInfo(infoList, infoType);
99         Assert.assertNotNull(curi);
100         resp = post(curi);
101         resp.then().assertThat().statusCode(HttpStatus.OK.value());
102
103         // check that there is no more "blacklist" data in get updates
104
105         resp = getUpdates(duid);
106         resp.then().assertThat().statusCode(HttpStatus.OK.value());
107         updates = resp.asString();
108         infoList = mapper.readValue(updates, new TypeReference<List<UInfo>>(){});
109         uri = getUriFromUInfo(infoList, infoType);
110         Assert.assertNull(uri);
111     }
112
113 }