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.junit.jupiter.SpringExtension;
37 import org.springframework.web.util.UriComponentsBuilder;
39 @ExtendWith(SpringExtension.class)
40 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
41 // Need the fake answers from the backend
42 class AbstractControllerTest {
44 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46 // Created by Spring black magic
47 // https://spring.io/guides/gs/testing-web/
49 private int localServerPort;
51 protected final AsyncRestClient webClient = new AsyncRestClient("");
54 * Flexible URI builder.
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
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)
71 builder.pathSegment(s);
73 builder.pathSegment(path[p]);
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");
81 builder.queryParam(entry.getKey(), entry.getValue());
84 return builder.build().encode().toUri();
87 // Because I put the annotations on this parent class,
88 // must define at least one test here.
91 // Silence Sonar warning about missing assertion.
92 Assertions.assertTrue(logger.isWarnEnabled());
93 logger.info("Context loads on mock profile");