Add tests to improve code-coverage stats in Sonar
[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.List;
25
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;
46
47 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
48 public class AdminControllerTest extends AbstractControllerTest {
49
50         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52         @Test
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());
58         }
59
60         @Test
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());
66         }
67
68         @Test
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>>() {
74                                 });
75                 Assertions.assertFalse(response.getBody().isEmpty());
76         }
77
78         @Test
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>>() {
84                                 });
85                 Assertions.assertFalse(response.getBody().isEmpty());
86         }
87
88         @Test
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,
93                                 String.class);
94                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
95         }
96
97         @Order(1)
98         @Test
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>>() {
105                                 });
106                 Assertions.assertFalse(response.getBody().isEmpty());
107                 Assertions.assertNotEquals(-1, response.getBody().get(0).getStatsDetails().getAppId());
108         }
109
110         @Order(2)
111         @Test
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());
125                 try {
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());
130                 }
131         }
132
133         @Order(3)
134         @Test
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>>() {
141                                 });
142                 int statToUpdate = 0;
143                 if (response.getBody() != null) {
144                         statToUpdate = response.getBody().get(0).getStatsDetails().getAppId();
145                 }
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,
152                                 String.class);
153                 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
154
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,
160                                 String.class);
161                 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
162         }
163
164         @Order(4)
165         @Test
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>>() {
171                                 });
172                 int statToDelete = 0;
173                 if (response.getBody() != null) {
174                         statToDelete = response.getBody().get(0).getStatsDetails().getAppId();
175                 }
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,
181                                 String.class);
182                 Assertions.assertTrue(stringResponse.getStatusCode().is2xxSuccessful());
183
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,
188                                 String.class);
189                 Assertions.assertTrue(voidResponse.getStatusCode().is4xxClientError());
190         }
191
192         @Test
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,
198                                 String.class);
199                 logger.debug("{}", response.getBody().toString());
200                 Assertions.assertTrue(response.getStatusCode().is5xxServerError());
201         }
202
203         @Test
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,
209                                 String.class);
210                 logger.debug("{}", errorResponse.getBody());
211                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
212         }
213
214         @Test
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,
219                                 String.class);
220                 logger.debug("{}", errorResponse.getBody());
221                 Assertions.assertTrue(errorResponse.getStatusCode().is5xxServerError());
222         }
223
224 }