Revise controllers to use ResponseEntity
[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.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
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;
39
40 public class AdminControllerTest extends AbstractControllerTest {
41
42         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
43
44         @Test
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());
50         }
51
52         @Test
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());
58         }
59
60         @Test
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>>() {
66                                 });
67                 Assertions.assertFalse(response.getBody().isEmpty());
68         }
69
70         @Test
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>>() {
76                                 });
77                 Assertions.assertFalse(response.getBody().isEmpty());
78         }
79
80         @Test
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,
85                                 String.class);
86                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
87         }
88
89         @Test
90         public void getxAppMetricsUrlTest() {
91                 Map<String, String> metricsQueryParms = new HashMap<String, String>();
92                 URI uri;
93
94                 metricsQueryParms.clear();
95                 metricsQueryParms.put("app", DashboardConstants.APP_NAME_AC);
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());
102
103                 metricsQueryParms.clear();
104                 metricsQueryParms.put("app", DashboardConstants.APP_NAME_MC);
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());
109         }
110
111         @Test
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
115                 // failure
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,
120                                 String.class);
121                 logger.debug("{}", errorResponse.getBody().toString());
122                 Assertions.assertTrue(errorResponse.getStatusCode().is4xxClientError());
123         }
124
125         @Test
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,
131                                 String.class);
132                 logger.debug("{}", response.getBody().toString());
133                 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
134         }
135
136         @Test
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,
142                                 String.class);
143                 logger.debug("{}", errorResponse.getBody());
144                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
145         }
146
147         @Test
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,
152                                 String.class);
153                 logger.debug("{}", errorResponse.getBody());
154                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
155         }
156
157 }