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() {
100 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY, "i1",
101 AdminController.STATAPPMETRIC_METHOD);
102 logger.info("Invoking uri {}", uri);
103 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
104 new ParameterizedTypeReference<List<AppStats>>() {
106 Assertions.assertFalse(response.getBody().isEmpty());
107 Assertions.assertNotEquals(-1, response.getBody().get(0).getStatsDetails().getAppId());
112 public void createAppStatsTest() {
113 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
114 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
115 logger.info("Invoking uri {}", uri);
116 StatsDetailsTransport statsDetails = new StatsDetailsTransport();
117 statsDetails.setAppName("MachLearn-2");
118 statsDetails.setMetricUrl("https://www.example2.com");
119 AppStats st = testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
120 Assertions.assertFalse(st.getStatsDetails().getAppName().isEmpty());
121 statsDetails.setAppName("MachLearn-2-next");
122 statsDetails.setMetricUrl("https://www.example2-next.com");
123 AppStats stNext = testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
124 Assertions.assertTrue(st.getStatsDetails().getAppId() < stNext.getStatsDetails().getAppId());
126 testRestTemplateAdminRole().postForObject(uri, statsDetails, AppStats.class);
127 Assert.assertTrue(false);
128 } catch (RestClientException ex) {
129 logger.info("Caught exception on create as expected: {}", ex.toString());
135 public void updateAppStatsTest() {
136 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
137 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
138 logger.info("Invoking uri {}", uri);
139 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
140 new ParameterizedTypeReference<List<AppStats>>() {
142 int statToUpdate = 0;
143 if (response.getBody() != null) {
144 statToUpdate = response.getBody().get(0).getStatsDetails().getAppId();
146 StatsDetailsTransport statsDetails = new StatsDetailsTransport();
147 statsDetails.setAppId(statToUpdate);
148 statsDetails.setAppName("MachLearn-1");
149 statsDetails.setMetricUrl("https://www.example1.com");
150 HttpEntity<StatsDetailsTransport> entity = new HttpEntity<>(statsDetails);
151 ResponseEntity<String> stringResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, entity,
153 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
155 StatsDetailsTransport bogusDetails = new StatsDetailsTransport();
156 bogusDetails.setAppId(-1);
157 bogusDetails.setAppName("bogus");
158 HttpEntity<StatsDetailsTransport> bogusEntity = new HttpEntity<>(bogusDetails);
159 ResponseEntity<String> voidResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.PUT, bogusEntity,
161 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
166 public void deleteAppStatsTest() {
167 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
168 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD);
169 ResponseEntity<List<AppStats>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
170 new ParameterizedTypeReference<List<AppStats>>() {
172 int statToDelete = 0;
173 if (response.getBody() != null) {
174 statToDelete = response.getBody().get(0).getStatsDetails().getAppId();
176 uri = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
177 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD,
178 DashboardConstants.APP_ID, String.valueOf(statToDelete));
179 logger.info("Invoking uri {}", uri);
180 ResponseEntity<String> stringResponse = testRestTemplateAdminRole().exchange(uri, HttpMethod.DELETE, null,
182 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
184 URI uri99 = buildUri(null, AdminController.CONTROLLER_PATH, DashboardConstants.RIC_INSTANCE_KEY,
185 RICInstanceMockConfiguration.INSTANCE_KEY_1, AdminController.STATAPPMETRIC_METHOD,
186 DashboardConstants.APP_ID, "999999");
187 ResponseEntity<String> voidResponse = testRestTemplateAdminRole().exchange(uri99, HttpMethod.DELETE, null,
189 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
193 public void throwHttpStatusCodeExceptionTest() {
194 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
195 AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
196 logger.debug("Invoking {}", uri);
197 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
199 logger.debug("{}", response.getBody().toString());
200 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
204 public void throwRestClientResponseExceptionTest() {
205 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
206 AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
207 logger.debug("Invoking {}", uri);
208 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
210 logger.debug("{}", errorResponse.getBody());
211 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
215 public void throwRuntimeExceptionTest() {
216 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
217 logger.debug("Invoking {}", uri);
218 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
220 logger.debug("{}", errorResponse.getBody());
221 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());