2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
24 import java.util.List;
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;
47 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
48 public class AdminControllerTest extends AbstractControllerTest {
50 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
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());
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());
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>>() {
75 Assertions.assertFalse(response.getBody().isEmpty());
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>>() {
85 Assertions.assertFalse(response.getBody().isEmpty());
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,
94 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
99 public void getAppStatsTest() {
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>>() {
107 Assertions.assertFalse(list.getBody().isEmpty());
108 Assertions.assertNotEquals(-1, list.getBody().get(0).getStatsDetails().getAppId());
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());
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);
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());
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());
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>>() {
158 int statToUpdate = 0;
159 if (response.getBody() != null) {
160 statToUpdate = response.getBody().get(0).getStatsDetails().getAppId();
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,
169 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
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,
177 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
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>>() {
188 int statToDelete = 0;
189 if (response.getBody() != null) {
190 statToDelete = response.getBody().get(0).getStatsDetails().getAppId();
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,
198 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
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,
205 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
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,
215 logger.debug("{}", response.getBody().toString());
216 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
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,
226 logger.debug("{}", errorResponse.getBody());
227 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
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,
236 logger.debug("{}", errorResponse.getBody());
237 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());