Add asserts to silence Sonar warnings re tests
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / controller / AbstractControllerTest.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.Map;
25
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.oransc.ric.portal.dashboard.config.WebSecurityMockConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
35 import org.springframework.boot.test.web.client.TestRestTemplate;
36 import org.springframework.boot.web.server.LocalServerPort;
37 import org.springframework.test.context.ActiveProfiles;
38 import org.springframework.test.context.junit.jupiter.SpringExtension;
39 import org.springframework.web.util.UriComponentsBuilder;
40
41 @ExtendWith(SpringExtension.class)
42 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
43 // Need the fake answers from the backend
44 @ActiveProfiles("test")
45 public class AbstractControllerTest {
46
47         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48
49         // Created by Spring black magic
50         // https://spring.io/guides/gs/testing-web/
51         @LocalServerPort
52         private int localServerPort;
53
54         @Autowired
55         protected TestRestTemplate restTemplate;
56
57         // Because I put the annotations on this parent class,
58         // must define at least one test here.
59         @Test
60         public void beQuietSonar() {
61                 // Silence Sonar warning about missing assertion.
62                 Assertions.assertTrue(logger.isWarnEnabled());
63         }
64
65         /**
66          * Builds URI from path components and query parameters.
67          * 
68          * @param queryParams
69          *                        Map of string-string query parameters
70          * @param path
71          *                        Array of path components. If a component has an
72          *                        embedded slash, that string is split and each
73          *                        subcomponent is added individually.
74          * @return URI
75          */
76         protected URI buildUri(final Map<String, String> queryParams, final String... path) {
77                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + localServerPort + "/");
78                 for (int p = 0; p < path.length; ++p) {
79                         if (path[p] == null || path[p].isEmpty()) {
80                                 throw new IllegalArgumentException("Unexpected null or empty at path index " + Integer.toString(p));
81                         } else if (path[p].contains("/")) {
82                                 String[] subpaths = path[p].split("/");
83                                 for (String s : subpaths)
84                                         if (!s.isEmpty())
85                                                 builder.pathSegment(s);
86                         } else {
87                                 builder.pathSegment(path[p]);
88                         }
89                 }
90                 if (queryParams != null && queryParams.size() > 0) {
91                         for (Map.Entry<String, String> entry : queryParams.entrySet()) {
92                                 if (entry.getKey() == null || entry.getValue() == null)
93                                         throw new IllegalArgumentException("Unexpected null key or value");
94                                 else
95                                         builder.queryParam(entry.getKey(), entry.getValue());
96                         }
97                 }
98                 return builder.build().encode().toUri();
99         }
100
101         protected TestRestTemplate testRestTemplateAdminRole() {
102                 return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_ADMIN,
103                                 WebSecurityMockConfiguration.TEST_CRED_ADMIN);
104         }
105
106         protected TestRestTemplate testRestTemplateStandardRole() {
107                 return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_STANDARD,
108                                 WebSecurityMockConfiguration.TEST_CRED_STANDARD);
109         }
110
111 }