37ab48493693cecf6d9f3b8e8a4c5ae02799255a
[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                 try {
142                         testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
143                         Assert.assertTrue(false);
144                 } catch (RestClientException ex) {
145                         logger.info("Caught exception on create as expected: {}", ex.toString());
146                 }
147         }
148
149         @Order(3)
150         @Test
151         public void updateAppStatsTest() {
152                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
153                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
154                 logger.info("Invoking uri {}", uri);
155                 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
156                                 new ParameterizedTypeReference<List<AppStats>>() {
157                                 });
158                 int statToUpdate = 0;
159                 if (response.getBody() != null) {
160                         statToUpdate = response.getBody().get(0).getStatsDetails().getAppId();
161                 }
162                 StatsDetailsTransport statsDetails = new StatsDetailsTransport();
163                 statsDetails.setAppId(statToUpdate);
164                 statsDetails.setAppName("MachLearn-1");
165                 statsDetails.setMetricUrl("https://www.example1.com");
166                 HttpEntity<StatsDetailsTransport> entity = new HttpEntity<>(statsDetails);
167                 ResponseEntity<String> stringResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, entity,
168                                 String.class);
169                 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
170
171                 StatsDetailsTransport bogusDetails = new StatsDetailsTransport();
172                 bogusDetails.setAppId(-1);
173                 bogusDetails.setAppName("bogus");
174                 HttpEntity<StatsDetailsTransport> bogusEntity = new HttpEntity<>(bogusDetails);
175                 ResponseEntity<String> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, bogusEntity,
176                                 String.class);
177                 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
178         }
179
180         @Order(4)
181         @Test
182         public void deleteAppStatsTest() {
183                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
184                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
185                 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
186                                 new ParameterizedTypeReference<List<AppStats>>() {
187                                 });
188                 int statToDelete = 0;
189                 if (response.getBody() != null) {
190                         statToDelete = response.getBody().get(0).getStatsDetails().getAppId();
191                 }
192                 uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
193                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD,
194                                 DashboardConstants.APP_ID, String.valueOf(statToDelete));
195                 logger.info("Invoking uri {}", uri);
196                 ResponseEntity<String> stringResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.DELETE, null,
197                                 String.class);
198                 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
199
200                 URI uri99 = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
201                                 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD,
202                                 DashboardConstants.APP_ID, "999999");
203                 ResponseEntity<String> voidResponse = testRestTemplateAdminRole().exchange(uri99, HttpMethod.DELETE, null,
204                                 String.class);
205                 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
206         }
207
208         @Test
209         public void throwHttpStatusCodeExceptionTest() {
210                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
211                                 AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
212                 logger.debug("Invoking {}", uri);
213                 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
214                                 String.class);
215                 logger.debug("{}", response.getBody().toString());
216                 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
217         }
218
219         @Test
220         public void throwRestClientResponseExceptionTest() {
221                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
222                                 AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
223                 logger.debug("Invoking {}", uri);
224                 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
225                                 String.class);
226                 logger.debug("{}", errorResponse.getBody());
227                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
228         }
229
230         @Test
231         public void throwRuntimeExceptionTest() {
232                 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
233                 logger.debug("Invoking {}", uri);
234                 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
235                                 String.class);
236                 logger.debug("{}", errorResponse.getBody());
237                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
238         }
239
240 }