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