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.RicInstanceKeyName;
33 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.core.ParameterizedTypeReference;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.ResponseEntity;
40 public class AdminControllerTest extends AbstractControllerTest {
42 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45 public void versionTest() {
46 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.VERSION_METHOD);
47 logger.info("Invoking {}", uri);
48 SuccessTransport st = restTemplate.getForObject(uri, SuccessTransport.class);
49 Assertions.assertFalse(st.getData().toString().isEmpty());
53 public void healthTest() {
54 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.HEALTH_METHOD);
55 logger.info("Invoking {}", uri);
56 ResponseEntity<Void> voidResponse = restTemplate.getForEntity(uri, Void.class);
57 Assertions.assertTrue(voidResponse.getStatusCode().is2xxSuccessful());
61 public void getInstancesTest() {
62 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.INSTANCE_METHOD);
63 logger.info("Invoking {}", uri);
64 ResponseEntity<List<RicInstanceKeyName>> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
65 null, new ParameterizedTypeReference<List<RicInstanceKeyName>>() {
67 Assertions.assertFalse(response.getBody().isEmpty());
71 public void getUsersTest() {
72 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.USER_METHOD);
73 logger.info("Invoking {}", uri);
74 ResponseEntity<List<EcompUser>> response = testRestTemplateAdminRole().exchange(uri, HttpMethod.GET, null,
75 new ParameterizedTypeReference<List<EcompUser>>() {
77 Assertions.assertFalse(response.getBody().isEmpty());
81 public void getUsersTestRoleAuthFail() {
82 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminController.USER_METHOD);
83 logger.info("Invoking {}", uri);
84 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
86 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
90 public void getxAppMetricsUrlTest() {
91 Map<String, String> metricsQueryParms = new HashMap<String, String>();
94 metricsQueryParms.clear();
95 metricsQueryParms.put("app", DashboardConstants.APP_NAME_MC);
96 uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
97 logger.debug("Invoking {}", uri);
98 ResponseEntity<SuccessTransport> successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET,
99 null, SuccessTransport.class);
100 Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
101 Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
103 metricsQueryParms.clear();
104 metricsQueryParms.put("app", DashboardConstants.APP_NAME_ML);
105 logger.debug("Invoking {}", uri);
106 successResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null, SuccessTransport.class);
107 Assertions.assertFalse(successResponse.getBody().getData().toString().isEmpty());
108 Assertions.assertTrue(successResponse.getStatusCode().is2xxSuccessful());
112 public void getxAppMetricsUrlTestFail() {
113 Map<String, String> metricsQueryParms = new HashMap<String, String>();
114 // Providing a bogus value for application name in query parameter to test
116 metricsQueryParms.put("app", "ABCD");
117 URI uri = buildUri(metricsQueryParms, AdminController.CONTROLLER_PATH, AdminController.XAPPMETRICS_METHOD);
118 logger.debug("Invoking {}", uri);
119 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
121 logger.debug("{}", errorResponse.getBody().toString());
122 Assertions.assertTrue(errorResponse.getStatusCode().is4xxClientError());
126 public void throwHttpStatusCodeExceptionTest() {
127 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
128 AdminControllerExtension.HTTP_STATUS_CODE_EXCEPTION_METHOD);
129 logger.debug("Invoking {}", uri);
130 ResponseEntity<String> response = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
132 logger.debug("{}", response.getBody().toString());
133 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
137 public void throwRestClientResponseExceptionTest() {
138 URI uri = buildUri(null, AdminController.CONTROLLER_PATH,
139 AdminControllerExtension.REST_CLIENT_RESPONSE_EXCEPTION_METHOD);
140 logger.debug("Invoking {}", uri);
141 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
143 logger.debug("{}", errorResponse.getBody());
144 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
148 public void throwRuntimeExceptionTest() {
149 URI uri = buildUri(null, AdminController.CONTROLLER_PATH, AdminControllerExtension.RUNTIME_EXCEPTION_METHOD);
150 logger.debug("Invoking {}", uri);
151 ResponseEntity<String> errorResponse = testRestTemplateStandardRole().exchange(uri, HttpMethod.GET, null,
153 logger.debug("{}", errorResponse.getBody());
154 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());