Add JUnit tests of backend controllers
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / 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;
21
22 import java.lang.invoke.MethodHandles;
23 import java.net.URI;
24 import java.util.Map;
25
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.test.context.SpringBootTest;
32 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
33 import org.springframework.boot.test.web.client.TestRestTemplate;
34 import org.springframework.boot.web.server.LocalServerPort;
35 import org.springframework.test.context.ActiveProfiles;
36 import org.springframework.test.context.junit4.SpringRunner;
37 import org.springframework.web.util.UriComponentsBuilder;
38
39 @RunWith(SpringRunner.class)
40 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
41 // Need the fake answers from the backend
42 @ActiveProfiles("mock")
43 public class AbstractControllerTest {
44
45         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46
47         // Created by Spring black magic
48         // https://spring.io/guides/gs/testing-web/
49         @LocalServerPort
50         private int localServerPort;
51
52         @Autowired
53         protected TestRestTemplate restTemplate;
54
55         /**
56          * Flexible URI builder.
57          * 
58          * @param queryParams
59          *                        Map of string-string query parameters
60          * @param path
61          *                        Array of path components. If a component has an
62          *                        embedded slash, the string is split and each
63          *                        subcomponent is added individually.
64          * @return URI
65          */
66         protected URI buildUri(final Map<String, String> queryParams, final String... path) {
67                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + localServerPort + "/");
68                 for (int p = 0; p < path.length; ++p) {
69                         if (path[p] == null || path[p].isEmpty()) {
70                                 throw new IllegalArgumentException("Unexpected null or empty at path index " + Integer.toString(p));
71                         } else if (path[p].contains("/")) {
72                                 String[] subpaths = path[p].split("/");
73                                 for (String s : subpaths)
74                                         if (!s.isEmpty())
75                                                 builder.pathSegment(s);
76                         } else {
77                                 builder.pathSegment(path[p]);
78                         }
79                 }
80                 if (queryParams != null && queryParams.size() > 0) {
81                         for (Map.Entry<String, String> entry : queryParams.entrySet()) {
82                                 if (entry.getKey() == null || entry.getValue() == null)
83                                         throw new IllegalArgumentException("Unexpected null key or value");
84                                 else
85                                         builder.queryParam(entry.getKey(), entry.getValue());
86                         }
87                 }
88                 return builder.build().encode().toUri();
89         }
90
91         // Must have at least one test here
92         @Test
93         public void contextLoads() {
94                 logger.info("Context loads on mock profile");
95         }
96
97 }