f3f9fad36ef92440d4f42347e5a2881b92e1a64d
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / controller / AdminControllerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23 import java.net.URI;
24 import java.util.List;
25
26 import org.junit.Assert;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.MethodOrderer;
29 import org.junit.jupiter.api.Order;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.TestMethodOrder;
32 import org.onap.portalsdk.core.restful.domain.EcompUser;
33 import org.oransc.ric.portal.dashboard.DashboardConstants;
34 import org.oransc.ric.portal.dashboard.config.RICInstanceMockConfiguration;
35 import org.oransc.ric.portal.dashboard.model.AppStats;
36 import org.oransc.ric.portal.dashboard.model.RicInstanceKeyName;
37 import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
38 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.core.ParameterizedTypeReference;
42 import org.springframework.http.HttpEntity;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.client.RestClientException;
46
47 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
48 public class AdminControllerTest extends AbstractControllerTest {
49
50         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52         @Test
53         public void versionTest() {
54                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.VERSION_METHOD);
55                 logger.info("Invoking {}", uri);
56                 SuccessTransport st = restTemplate.getForObject(uri, SuccessTransport.class);
57                 Assertions.assertFalse(st.getData().toString().isEmpty());
58         }
59
60         @Test
61         public void healthTest() {
62                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.HEALTH_METHOD);
63                 logger.info("Invoking {}", uri);
64                 ResponseEntity<Void> voidResponse = restTemplate.getForEntity(uri, Void.class);
65                 Assertions.assertTrue(voidResponse.getStatusCode().is2xxSuccessful());
66         }
67
68         @Test
69         public void getInstancesTest() {
70                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.INSTANCE_METHOD);
71                 logger.info("Invoking {}", uri);
72                 ResponseEntity<List<RicInstanceKeyName>> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
73                                 null, new ParameterizedTypeReference<List<RicInstanceKeyName>>() {
74                                 });
75                 Assertions.assertFalse(response.getBody().isEmpty());
76         }
77
78         @Test
79         public void getUsersTest() {
80                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.USER_METHOD);
81                 logger.info("Invoking {}", uri);
82                 ResponseEntity<List<EcompUser>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
83                                 new ParameterizedTypeReference<List<EcompUser>>() {
84                                 });
85                 Assertions.assertFalse(response.getBody().isEmpty());
86         }
87
88         @Test
89         public void getUsersTestRoleAuthFail() {
90                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.USER_METHOD);
91                 logger.info("Invoking {}", uri);
92                 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
93                                 String.class);
94                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
95         }
96
97         @Order(1)
98         @Test
99         public void getAppStatsTest() {
100                 // Get all
101                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
102                                 AdminController.STATAPPMETRIC_METHOD);
103                 logger.info("getAppStatsTest: uri {}", uri);
104                 ResponseEntity<List<AppStats>> list = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
105                                 new ParameterizedTypeReference<List<AppStats>>() {
106                                 });
107                 Assertions.assertFalse(list.getBody().isEmpty());
108                 Assertions.assertNotEquals(-1, list.getBody().get(0).getStatsDetails().getAppId());
109
110                 // Get one by ID
111                 int appId = list.getBody().get(0).getStatsDetails().getAppId();
112                 uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
113                                 AdminController.STATAPPMETRIC_METHOD, DashboardConstants.APP_ID, Integer.toString(appId));
114                 logger.info("getAppStatsTest: uri {}", uri);
115                 AppStats stats = testRestTemplateStandardRole().getForObject(uri, AppStats.class);
116                 Assertions.assertEquals(appId, stats.getStatsDetails().getAppId());
117
118                 // Fail to get one by ID
119                 uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
120                                 AdminController.STATAPPMETRIC_METHOD, DashboardConstants.APP_ID, "987654321");
121                 logger.info("getAppStatsTest: uri {}", uri);
122                 stats = testRestTemplateStandardRole().getForObject(uri, AppStats.class);
123                 Assert.assertNull(stats);
124         }
125
126         @Order(2)
127         @Test
128         public void createAppStatsTest() {
129                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
130                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
131                 logger.info("Invoking uri {}", uri);
132                 StatsDetailsTransport statsDetails = new StatsDetailsTransport();
133                 statsDetails.setAppName("MachLearn-2");
134                 statsDetails.setMetricUrl("https://www.example2.com");
135                 AppStats st = testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
136                 Assertions.assertFalse(st.getStatsDetails().getAppName().isEmpty());
137                 statsDetails.setAppName("MachLearn-2-next");
138                 statsDetails.setMetricUrl("https://www.example2-next.com");
139                 AppStats stNext = testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
140                 Assertions.assertTrue(st.getStatsDetails().getAppId() < stNext.getStatsDetails().getAppId());
141                 Assertions.assertThrows(RestClientException.class, () -> {
142                         testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
143                 });
144         }
145
146         @Order(3)
147         @Test
148         public void updateAppStatsTest() {
149                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
150                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
151                 logger.info("Invoking uri {}", uri);
152                 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
153                                 new ParameterizedTypeReference<List<AppStats>>() {
154                                 });
155                 int statToUpdate = 0;
156                 if (response.getBody() != null) {
157                         statToUpdate = response.getBody().get(0).getStatsDetails().getAppId();
158                 }
159                 StatsDetailsTransport statsDetails = new StatsDetailsTransport();
160                 statsDetails.setAppId(statToUpdate);
161                 statsDetails.setAppName("MachLearn-1");
162                 statsDetails.setMetricUrl("https://www.example1.com");
163                 HttpEntity<StatsDetailsTransport> entity = new HttpEntity<>(statsDetails);
164                 ResponseEntity<String> stringResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, entity,
165                                 String.class);
166                 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
167
168                 StatsDetailsTransport bogusDetails = new StatsDetailsTransport();
169                 bogusDetails.setAppId(-1);
170                 bogusDetails.setAppName("bogus");
171                 HttpEntity<StatsDetailsTransport> bogusEntity = new HttpEntity<>(bogusDetails);
172                 ResponseEntity<String> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, bogusEntity,
173                                 String.class);
174                 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
175         }
176
177         @Order(4)
178         @Test
179         public void deleteAppStatsTest() {
180                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
181                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
182                 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
183                                 new ParameterizedTypeReference<List<AppStats>>() {
184                                 });
185                 int statToDelete = 0;
186                 if (response.getBody() != null) {
187                         statToDelete = response.getBody().get(0).getStatsDetails().getAppId();
188                 }
189                 uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
190                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD,
191                                 DashboardConstants.APP_ID, String.valueOf(statToDelete));
192                 logger.info("Invoking uri {}", uri);
193                 ResponseEntity<String> stringResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.DELETE, null,
194                                 String.class);
195                 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
196
197                 URI uri99 = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
198                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD,
199                                 DashboardConstants.APP_ID, "999999");
200                 ResponseEntity<String> voidResponse = testRestTemplateAdminRole().exchange(uri99, HttpMethod.DELETE, null,
201                                 String.class);
202                 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
203         }
204
205         @Test
206         public void throwHttpStatusCodeExceptionTest() {
207                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
208                                 AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
209                 logger.debug("Invoking {}", uri);
210                 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
211                                 String.class);
212                 logger.debug("{}", response.getBody().toString());
213                 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
214         }
215
216         @Test
217         public void throwRestClientResponseExceptionTest() {
218                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
219                                 AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
220                 logger.debug("Invoking {}", uri);
221                 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
222                                 String.class);
223                 logger.debug("{}", errorResponse.getBody());
224                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
225         }
226
227         @Test
228         public void throwRuntimeExceptionTest() {
229                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
230                 logger.debug("Invoking {}", uri);
231                 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
232                                 String.class);
233                 logger.debug("{}", errorResponse.getBody());
234                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
235         }
236
237 }