2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.test.controller;
22 import java.lang.invoke.MethodHandles;
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;
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 {
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;
54 protected TestRestTemplate restTemplate;
57 * Flexible URI builder.
60 * Map of string-string query parameters
62 * Array of path components. If a component has an
63 * embedded slash, the string is split and each
64 * subcomponent is added individually.
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)
76 builder.pathSegment(s);
78 builder.pathSegment(path[p]);
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");
86 builder.queryParam(entry.getKey(), entry.getValue());
89 return builder.build().encode().toUri();
92 // Because I put the annotations on this parent class,
93 // must define at least one test here.
95 public void contextLoads() {
96 // Silence Sonar warning about missing assertion.
97 Assertions.assertTrue(logger.isWarnEnabled());
98 logger.info("Context loads on mock profile");