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