Fix formatting in the dashboard
[nonrtric.git] / dashboard / 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     /**
58      * Flexible URI builder.
59      *
60      * @param queryParams
61      *        Map of string-string query parameters
62      * @param path
63      *        Array of path components. If a component has an
64      *        embedded slash, the string is split and each
65      *        subcomponent is added individually.
66      * @return URI
67      */
68     protected URI buildUri(final Map<String, String> queryParams, final String... path) {
69         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + localServerPort + "/");
70         for (int p = 0; p < path.length; ++p) {
71             if (path[p] == null || path[p].isEmpty()) {
72                 throw new IllegalArgumentException("Unexpected null or empty at path index " + Integer.toString(p));
73             } else if (path[p].contains("/")) {
74                 String[] subpaths = path[p].split("/");
75                 for (String s : subpaths)
76                     if (!s.isEmpty())
77                         builder.pathSegment(s);
78             } else {
79                 builder.pathSegment(path[p]);
80             }
81         }
82         if (queryParams != null && queryParams.size() > 0) {
83             for (Map.Entry<String, String> entry : queryParams.entrySet()) {
84                 if (entry.getKey() == null || entry.getValue() == null)
85                     throw new IllegalArgumentException("Unexpected null key or value");
86                 else
87                     builder.queryParam(entry.getKey(), entry.getValue());
88             }
89         }
90         return builder.build().encode().toUri();
91     }
92
93     // Because I put the annotations on this parent class,
94     // must define at least one test here.
95     @Test
96     public void contextLoads() {
97         // Silence Sonar warning about missing assertion.
98         Assertions.assertTrue(logger.isWarnEnabled());
99         logger.info("Context loads on mock profile");
100     }
101
102     public TestRestTemplate testRestTemplateAdminRole() {
103         return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_ADMIN,
104             WebSecurityMockConfiguration.TEST_CRED_ADMIN);
105     }
106
107     public TestRestTemplate testRestTemplateStandardRole() {
108         return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_STANDARD,
109             WebSecurityMockConfiguration.TEST_CRED_STANDARD);
110     }
111
112 }