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