[SECIOTSRK-569] Policy integration test
[platform/core/security/suspicious-activity-monitor.git] / servers / api-integration-tests / src / test / java / com / samsung / ci / test / mq / DeviceRegistrationAndUnregistrationWithoutCloudTest.java
1 package com.samsung.ci.test.mq;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
5
6 import org.junit.jupiter.api.BeforeAll;
7 import org.junit.jupiter.api.Test;
8
9 import com.google.gson.Gson;
10 import com.samsung.ci.basic.api.MQPublisherAPI;
11 import com.samsung.ci.basic.api.Rest;
12 import com.samsung.ci.basic.model.Device;
13 import com.samsung.ci.test.useractions.BasicUserAction;
14
15 import io.restassured.response.Response;
16
17 public class DeviceRegistrationAndUnregistrationWithoutCloudTest extends BasicUserAction{
18
19     private static final String DEVICE_DUID = "00000000-1111-2222-3333-4444444444444";
20     private static final String DEVICE_NAME = "TV";
21     private static final String DEVICE_TYPE = "tv";
22     private static final String DEVICE_MODEL = "standard";
23     private static final String DEVICE_PARENT_DUID = "55555555-6666-7777-8888-999999999999";
24     private static final String DEVICE_UNREGISTER_ACTION = "unregister";
25     private static final String DEVICE_DELETE_ACTION = "delete";
26     
27     @BeforeAll
28     public static void info() {
29         log("Device registration/unregistration without cloud scenario:\n"
30                 + "1. Register user;\n"
31                 + "2. Register device;\n"
32                 + "3. Check device;\n"
33                 + "4. Remove device\n"
34                 + "5. Remove user\n");
35     }
36
37     private void registerDevice(String uuid) {
38         log("Device registration");
39         assertTrue(MQPublisherAPI.deviceRegistrationInDSM(DEVICE_DUID, uuid, DEVICE_NAME, DEVICE_TYPE, DEVICE_MODEL, DEVICE_PARENT_DUID));
40     }
41
42     private void checkDevice() {
43         log("Check device");
44         Response resp = Rest.getDeviceByDUID(DEVICE_DUID);
45         assertTrue(resp.getStatusCode() == 200);
46         Gson gson = new Gson();
47         Device device = gson.fromJson(resp.getBody().asString(), Device.class);
48         log(device.toString());
49         assertTrue(device.getUuid().equals(DEVICE_DUID));
50         assertTrue(device.getModel().equals(DEVICE_MODEL));
51         assertTrue(device.getName().equals(DEVICE_NAME));
52         assertTrue(device.getStatus() == 1);
53         assertTrue(device.getParentUuid().equals(DEVICE_PARENT_DUID));
54     }
55
56     private void checkDeviceOnList(String uuid) {
57         log("Check device on list");
58         Response resp = Rest.getDeviceByCloudUUID(uuid);
59         assertTrue(resp.getBody().asString().contains(DEVICE_DUID));
60     }
61     
62
63     private void lastCheck(String uuid) {
64         log("Last check device");
65         Response resp = Rest.getDeviceByDUID(DEVICE_DUID);
66         assertTrue(resp.getStatusCode() == 404);
67         assertFalse(resp.getBody().asString().contains(DEVICE_DUID));
68         resp = Rest.getDeviceByCloudUUID(uuid);
69         assertFalse(resp.getBody().asString().contains(DEVICE_DUID));
70     }
71
72     /**
73      * Positive case
74      * 1. Create new user "userApiIntegrationTest"
75      * 2. Login
76      * 3. Get auth2 code
77      * 4. Get access token
78      * 5. Get UserInfo
79      * 6. Create IoTCloud User
80      * 7. Register device
81      * 8. Check device
82      * 9. Unregister device
83      * 10. Delete IoTCloud User
84      * @throws InterruptedException 
85      */
86     @Test
87     public void T01registerAndUnregisterDevice() throws InterruptedException{
88         log("Authorized without cloud scenario");
89         Thread.sleep(TIMEOUT);
90         createNewUser();
91         loginNewUser();
92         String authcode = getAuthCode(USER_EMAIL, USER_PASSWORD);
93         String token = getToken(authcode);
94         getUserInfo(token);
95         String uuid = "test-uuid";
96         createIoTUser(USER_EMAIL, uuid);
97         registerDevice(uuid);
98         Thread.sleep(TIMEOUT);
99         checkDevice();
100         checkDeviceOnList(uuid);
101         assertTrue(MQPublisherAPI.deviceUnRegistrationInDSM(DEVICE_DUID, DEVICE_DELETE_ACTION));
102         Thread.sleep(TIMEOUT);
103         checkDevice();
104         assertTrue(MQPublisherAPI.deviceUnRegistrationInDSM(DEVICE_DUID, DEVICE_UNREGISTER_ACTION));
105         Thread.sleep(TIMEOUT);
106         lastCheck(uuid);
107         deleteIoTUser(uuid);
108     }
109
110 }