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.config.WebSecurityMockConfiguration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
36 import org.springframework.boot.test.web.client.TestRestTemplate;
37 import org.springframework.boot.web.server.LocalServerPort;
38 import org.springframework.test.context.ActiveProfiles;
39 import org.springframework.test.context.junit.jupiter.SpringExtension;
40 import org.springframework.web.util.UriComponentsBuilder;
42 @ExtendWith(SpringExtension.class)
43 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
44 // Need the fake answers from the backend
45 @ActiveProfiles("test")
46 public class AbstractControllerTest {
48 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50 // Created by Spring black magic
51 // https://spring.io/guides/gs/testing-web/
53 private int localServerPort;
56 protected TestRestTemplate restTemplate;
59 * Flexible URI builder.
62 * Map of string-string query parameters
64 * Array of path components. If a component has an
65 * embedded slash, the string is split and each
66 * subcomponent is added individually.
69 protected URI buildUri(final Map<String, String> queryParams, final String... path) {
70 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + localServerPort + "/");
71 for (int p = 0; p < path.length; ++p) {
72 if (path[p] == null || path[p].isEmpty()) {
73 throw new IllegalArgumentException("Unexpected null or empty at path index " + Integer.toString(p));
74 } else if (path[p].contains("/")) {
75 String[] subpaths = path[p].split("/");
76 for (String s : subpaths)
78 builder.pathSegment(s);
80 builder.pathSegment(path[p]);
83 if (queryParams != null && queryParams.size() > 0) {
84 for (Map.Entry<String, String> entry : queryParams.entrySet()) {
85 if (entry.getKey() == null || entry.getValue() == null)
86 throw new IllegalArgumentException("Unexpected null key or value");
88 builder.queryParam(entry.getKey(), entry.getValue());
91 return builder.build().encode().toUri();
94 // Because I put the annotations on this parent class,
95 // must define at least one test here.
97 public void contextLoads() {
98 // Silence Sonar warning about missing assertion.
99 Assertions.assertTrue(logger.isWarnEnabled());
100 logger.info("Context loads on mock profile");
103 public TestRestTemplate testRestTemplateAdminRole() {
104 return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_ADMIN,
105 WebSecurityMockConfiguration.TEST_CRED_ADMIN);
108 public TestRestTemplate testRestTemplateStandardRole() {
109 return restTemplate.withBasicAuth(WebSecurityMockConfiguration.TEST_CRED_STANDARD,
110 WebSecurityMockConfiguration.TEST_CRED_STANDARD);