2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
6 * Modifications Copyright (C) 2020 Nordix Foundation
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.portal.nonrtric.controlpanel.controller;
23 import java.lang.invoke.MethodHandles;
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.ActiveProfiles;
37 import org.springframework.test.context.junit.jupiter.SpringExtension;
38 import org.springframework.web.util.UriComponentsBuilder;
40 @ExtendWith(SpringExtension.class)
41 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
42 // Need the fake answers from the backend
43 @ActiveProfiles("test")
44 class AbstractControllerTest {
46 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48 // Created by Spring black magic
49 // https://spring.io/guides/gs/testing-web/
51 private int localServerPort;
53 protected final AsyncRestClient webClient = new AsyncRestClient("");
56 * Flexible URI builder.
58 * @param queryParams Map of string-string query parameters
59 * @param path Array of path components. If a component has an embedded
60 * slash, the string is split and each subcomponent is added
64 protected URI buildUri(final Map<String, String> queryParams, final String... path) {
65 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://localhost:" + localServerPort + "/");
66 for (int p = 0; p < path.length; ++p) {
67 if (path[p] == null || path[p].isEmpty()) {
68 throw new IllegalArgumentException("Unexpected null or empty at path index " + Integer.toString(p));
69 } else if (path[p].contains("/")) {
70 String[] subpaths = path[p].split("/");
71 for (String s : subpaths)
73 builder.pathSegment(s);
75 builder.pathSegment(path[p]);
78 if (queryParams != null && queryParams.size() > 0) {
79 for (Map.Entry<String, String> entry : queryParams.entrySet()) {
80 if (entry.getKey() == null || entry.getValue() == null)
81 throw new IllegalArgumentException("Unexpected null key or value");
83 builder.queryParam(entry.getKey(), entry.getValue());
86 return builder.build().encode().toUri();
89 // Because I put the annotations on this parent class,
90 // must define at least one test here.
93 // Silence Sonar warning about missing assertion.
94 Assertions.assertTrue(logger.isWarnEnabled());
95 logger.info("Context loads on mock profile");