2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.rest.stub;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
26 import java.io.IOException;
27 import javax.ws.rs.core.Response;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.policy.api.main.PolicyApiApplication;
32 import org.onap.policy.api.main.rest.utils.CommonTestRestController;
33 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.boot.test.web.server.LocalServerPort;
36 import org.springframework.test.annotation.DirtiesContext;
37 import org.springframework.test.annotation.DirtiesContext.ClassMode;
38 import org.springframework.test.context.ActiveProfiles;
39 import org.springframework.test.context.DynamicPropertyRegistry;
40 import org.springframework.test.context.DynamicPropertySource;
41 import org.springframework.test.context.junit4.SpringRunner;
43 @RunWith(SpringRunner.class)
44 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
45 @ActiveProfiles({ "test", "stub" })
46 @DirtiesContext(classMode = ClassMode.AFTER_CLASS)
47 public class ApisRestControllerStubTest extends CommonTestRestController {
48 protected static final String APP_JSON = "application/json";
49 protected static final String APP_YAML = "application/yaml";
54 private static SelfSignedKeyStore keystore;
58 public static void setupParameters() throws IOException, InterruptedException {
59 keystore = new SelfSignedKeyStore();
62 @DynamicPropertySource
63 static void registerPgProperties(DynamicPropertyRegistry registry) {
64 registry.add("server.ssl.enabled", () -> "true");
65 registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
66 registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
67 registry.add("server.ssl.key-store-type", () -> "PKCS12");
68 registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
69 registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
73 public void testStubbedGet() throws Exception {
74 checkStubJson("policies");
75 checkStubJson("policies/policyname/versions/1.0.2");
76 checkStubJson("nodetemplates");
77 checkStubJson("nodetemplates/k8stemplate/versions/1.0.0");
78 checkStubJson("policytypes");
79 checkStubJson("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/latest");
80 checkStubJson("statistics");
81 checkStubJson("healthcheck");
84 private void checkStubJson(String url) throws Exception {
85 var response = super.readResource(url, APP_JSON, apiPort);
86 assertNotNull(response);
87 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
88 var responseYaml = super.readResource(url, APP_YAML, apiPort);
89 assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseYaml.getStatus());