46bf8ea91e423749d299f5a5d9fbce70a2c6d061
[platform/core/security/suspicious-activity-monitor.git] / server / samserver / src / test / java / com / samsung / samserver / integration / RestApi.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.samsung.samserver.web.rest.service.vm.UInfo;
10 import io.restassured.RestAssured;
11 import io.restassured.http.ContentType;
12 import io.restassured.response.Response;
13 import org.apache.commons.io.IOUtils;
14 import org.springframework.core.io.ResourceLoader;
15 import java.nio.charset.StandardCharsets;
16 import java.util.List;
17
18 import static io.restassured.RestAssured.given;
19
20 /**
21  * Base class for REST API Integration Tests.
22  *
23  */
24 public abstract class RestApi {
25
26     int localServerPort;
27     ResourceLoader resourceLoader;
28
29     protected Response registerDevice(String inResource) throws Exception {
30         String in = IOUtils.toString(resourceLoader.getResource(inResource).getInputStream(), StandardCharsets.UTF_8);
31         String uri = "/api/device-service/register-device";
32         Response resp = given()
33                 .contentType(ContentType.JSON)
34                 .body(in)
35                 .when().port(this.localServerPort)
36                 .post(RestAssured.baseURI + uri);
37         resp.then().log().all();
38         return resp;
39     }
40
41     protected Response sendData(String inResource, String duid) throws Exception {
42         String in = IOUtils.toString(resourceLoader.getResource(inResource).getInputStream(), StandardCharsets.UTF_8);
43         String uri = "/api/device-service/send-data";
44         Response resp = given()
45                 .queryParam("duid", duid)
46                 .contentType(ContentType.JSON)
47                 .body(in)
48                 .when().port(this.localServerPort)
49                 .post(RestAssured.baseURI + uri);
50         resp.then().log().all();
51         return resp;
52     }
53
54     protected Response getUpdates(String duid) {
55         String uri = "/api/device-service/get-updates";
56         Response resp = given()
57                 .queryParam("duid", duid)
58                 .when().port(this.localServerPort)
59                 .get(RestAssured.baseURI + uri);
60         resp.then().log().all();
61         return resp;
62     }
63
64     protected Response get(String uri) {
65         Response resp = given()
66                 .when().port(this.localServerPort)
67                 .get(RestAssured.baseURI + uri);
68         resp.then().log().all();
69         return resp;
70     }
71
72     protected Response post(String uri) {
73         Response resp = given()
74                 .when().port(this.localServerPort)
75                 .post(RestAssured.baseURI + uri);
76         resp.then().log().all();
77         return resp;
78     }
79
80     protected String getUriFromUInfo(List<UInfo> infoList, String type) {
81         String uri = null;
82         for (UInfo uInfo: infoList) {
83             if (uInfo.getType().equals(type)){
84                 uri = uInfo.getUri();
85                 break;
86             }
87         }
88         return uri;
89     }
90
91     protected String getCuriFromUInfo(List<UInfo> infoList, String type) {
92         String curi = null;
93         for (UInfo uInfo: infoList) {
94             if (uInfo.getType().equals(type)){
95                 curi = uInfo.getCuri();
96                 break;
97             }
98         }
99         return curi;
100     }
101
102 }