First version of NonRT RIC Controlpanel
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / controller / AbstractControllerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.controller;
22
23 import java.lang.invoke.MethodHandles;
24 import java.net.URI;
25 import java.util.Map;
26
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.oransc.portal.nonrtric.controlpanel.config.WebSecurityMockConfiguration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
36 import org.springframework.boot.test.web.client.TestRestTemplate;
37 import org.springframework.boot.web.server.LocalServerPort;
38 import org.springframework.test.context.ActiveProfiles;
39 import org.springframework.test.context.junit.jupiter.SpringExtension;
40 import org.springframework.web.util.UriComponentsBuilder;
41
42 @ExtendWith(SpringExtension.class)
43 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
44 // Need the fake answers from the backend
45 @ActiveProfiles("test")
46 public class AbstractControllerTest {
47
48     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49
50     // Created by Spring black magic
51     // https://spring.io/guides/gs/testing-web/
52     @LocalServerPort
53     private int localServerPort;
54
55     @Autowired
56     protected TestRestTemplate restTemplate;
57
58     /**
59      * Flexible URI builder.
60      *
61      * @param queryParams
62      *        Map of string-string query parameters
63      * @param path
64      *        Array of path components. If a component has an
65      *        embedded slash, the string is split and each
66      *        subcomponent is added individually.
67      * @return URI
68      */
69     protected URI buildUri(final Map<String, String> queryParams, final String... path) {
70         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + localServerPort + "/");
71         for (int p = 0; p < path.length; ++p) {
72             if (path[p] == null || path[p].isEmpty()) {
73                 throw new IllegalArgumentException("Unexpected null or empty at path index " + Integer.toString(p));
74             } else if (path[p].contains("/")) {
75                 String[] subpaths = path[p].split("/");
76                 for (String s : subpaths)
77                     if (!s.isEmpty())
78                         builder.pathSegment(s);
79             } else {
80                 builder.pathSegment(path[p]);
81             }
82         }
83         if (queryParams != null && queryParams.size() > 0) {
84             for (Map.Entry<String, String> entry : queryParams.entrySet()) {
85                 if (entry.getKey() == null || entry.getValue() == null)
86                     throw new IllegalArgumentException("Unexpected null key or value");
87                 else
88                     builder.queryParam(entry.getKey(), entry.getValue());
89             }
90         }
91         return builder.build().encode().toUri();
92     }
93
94     // Because I put the annotations on this parent class,
95     // must define at least one test here.
96     @Test
97     public void contextLoads() {
98         // Silence Sonar warning about missing assertion.
99         Assertions.assertTrue(logger.isWarnEnabled());
100         logger.info("Context loads on mock profile");
101     }
102
103     public TestRestTemplate testRestTemplateAdminRole() {
104         return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_ADMIN,
105             WebSecurityMockConfiguration.TEST_CRED_ADMIN);
106     }
107
108     public TestRestTemplate testRestTemplateStandardRole() {
109         return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_STANDARD,
110             WebSecurityMockConfiguration.TEST_CRED_STANDARD);
111     }
112
113 }