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.HashMap;
25 import java.util.List;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.Test;
30 import org.onap.portalsdk.core.restful.domain.EcompUser;
31 import org.oransc.ric.portal.dashboard.DashboardConstants;
32 import org.oransc.ric.portal.dashboard.model.ErrorTransport;
33 import org.oransc.ric.portal.dashboard.model.RicInstanceKeyName;
34 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.core.ParameterizedTypeReference;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.ResponseEntity;
41 public class AdminControllerTest extends AbstractControllerTest {
43 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46 public void versionTest() {
47 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.VERSION_METHOD);
48 logger.info("Invoking {}", uri);
49 SuccessTransport st = restTemplate.getForObject(uri, SuccessTransport.class);
50 Assertions.assertFalse(st.getData().toString().isEmpty());
54 public void healthTest() {
55 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.HEALTH_METHOD);
56 logger.info("Invoking {}", uri);
57 ResponseEntity<Void> voidResponse = restTemplate.getForEntity(uri, Void.class);
58 Assertions.assertTrue(voidResponse.getStatusCode().is2xxSuccessful());
62 public void getInstancesTest() {
63 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.INSTANCE_METHOD);
64 logger.info("Invoking {}", uri);
65 ResponseEntity<List<RicInstanceKeyName>> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
66 null, new ParameterizedTypeReference<List<RicInstanceKeyName>>() {
68 Assertions.assertFalse(response.getBody().isEmpty());
72 public void getUsersTest() {
73 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.USER_METHOD);
74 logger.info("Invoking {}", uri);
75 ResponseEntity<List<EcompUser>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
76 new ParameterizedTypeReference<List<EcompUser>>() {
78 Assertions.assertFalse(response.getBody().isEmpty());
82 public void getUsersTestRoleAuthFail() {
83 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.USER_METHOD);
84 logger.info("Invoking {}", uri);
85 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
87 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
91 public void getxAppMetricsUrlTest() {
92 Map<String, String> metricsQueryParms = new HashMap<String, String>();
95 metricsQueryParms.clear();
96 metricsQueryParms.put("app", DashboardConstants.APP_NAME_AC);
97 uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
98 logger.debug("Invoking {}", uri);
99 ResponseEntity<SuccessTransport> successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
100 null, SuccessTransport.class);
101 Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
102 Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
104 metricsQueryParms.clear();
105 metricsQueryParms.put("app", DashboardConstants.APP_NAME_MC);
106 logger.debug("Invoking {}", uri);
107 successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null, SuccessTransport.class);
108 Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
109 Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
113 public void getxAppMetricsUrlTestFail() {
114 Map<String, String> metricsQueryParms = new HashMap<String, String>();
115 // Providing a bogus value for application name in query parameter to test
117 metricsQueryParms.put("app", "ABCD");
118 URI uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
119 logger.debug("Invoking {}", uri);
120 ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
121 null, ErrorTransport.class);
122 logger.debug("{}", errorResponse.getBody().getError().toString());
123 Assertions.assertTrue(errorResponse.getStatusCode().is4xxClientError());
127 public void throwHttpStatusCodeExceptionTest() {
128 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
129 AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
130 logger.debug("Invoking {}", uri);
131 ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
132 null, ErrorTransport.class);
133 logger.debug("{}", errorResponse.getBody().getError().toString());
134 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
138 public void throwRestClientResponseExceptionTest() {
139 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
140 AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
141 logger.debug("Invoking {}", uri);
142 ResponseEntity<ErrorTransport> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
143 null, ErrorTransport.class);
144 logger.debug("{}", errorResponse.getBody().getError().toString());
145 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
149 public void throwRuntimeExceptionTest() {
150 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
151 logger.debug("Invoking {}", uri);
152 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
154 logger.debug("{}", errorResponse.getBody());
155 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());