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.contract;
23 import static org.junit.Assert.assertEquals;
25 import java.io.IOException;
26 import javax.ws.rs.core.Response;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.policy.api.main.PolicyApiApplication;
31 import org.onap.policy.api.main.rest.utils.CommonTestRestController;
32 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.boot.test.web.server.LocalServerPort;
35 import org.springframework.test.annotation.DirtiesContext;
36 import org.springframework.test.annotation.DirtiesContext.ClassMode;
37 import org.springframework.test.context.ActiveProfiles;
38 import org.springframework.test.context.DynamicPropertyRegistry;
39 import org.springframework.test.context.DynamicPropertySource;
40 import org.springframework.test.context.junit4.SpringRunner;
42 @RunWith(SpringRunner.class)
43 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
44 @ActiveProfiles({ "test", "stub" })
45 @DirtiesContext(classMode = ClassMode.AFTER_CLASS)
46 public class ApiContractTest extends CommonTestRestController {
47 protected static final String APP_JSON = "application/json";
48 protected static final String APP_YAML = "application/yaml";
49 private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
50 "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
55 private static SelfSignedKeyStore keystore;
59 public static void setupParameters() throws IOException, InterruptedException {
60 keystore = new SelfSignedKeyStore();
63 @DynamicPropertySource
64 static void registerPgProperties(DynamicPropertyRegistry registry) {
65 registry.add("server.ssl.enabled", () -> "true");
66 registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
67 registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
68 registry.add("server.ssl.key-store-type", () -> "PKCS12");
69 registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
70 registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
74 public void testStubPolicyDesign() throws Exception {
75 checkStubJsonGet("policies");
76 checkStubJsonGet("policies/policyname/versions/1.0.2");
77 checkStubJsonGet("policytypes");
78 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16");
79 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/latest");
80 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/");
81 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies");
82 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
83 + "9c65fa1f-2833-4076-a64d-5b62e35cd09b");
84 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
85 + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/latest");
86 checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
87 + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/1.2.3");
88 checkStubJsonGet("statistics");
89 checkStubJsonGet("healthcheck");
91 checkStubJsonPost("policies");
92 checkStubJsonPost("policytypes");
93 checkStubJsonPost("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.2.3/policies");
95 checkStubJsonDelete("policies/policyname/versions/1.0.2");
96 checkStubJsonDelete("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0");
97 checkStubJsonDelete("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
98 + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/1.2.3");
102 public void testStubNodeTemplateDesign() throws Exception {
103 checkStubJsonGet("nodetemplates");
104 checkStubJsonGet("nodetemplates/k8stemplate/versions/1.0.0");
106 checkStubJsonPost("nodetemplates");
108 checkStubJsonPut("nodetemplates");
110 checkStubJsonDelete("nodetemplates/k8stemplate/versions/1.0.0");
114 public void testErrors() throws Exception {
115 var responseYaml = super.readResource("policies", APP_YAML, apiPort);
116 assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseYaml.getStatus());
118 var responseListYaml = super.readResource("nodetemplates", APP_YAML, apiPort);
119 assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseListYaml.getStatus());
123 private void checkStubJsonGet(String url) throws Exception {
124 var response = super.readResource(url, APP_JSON, apiPort);
125 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
128 private void checkStubJsonPost(String url) throws Exception {
129 var response = super.createResource(url, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
130 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
133 private void checkStubJsonPut(String url) throws Exception {
134 var response = super.updateResource(url, TOSCA_NODE_TEMPLATE_RESOURCE, APP_JSON, apiPort);
135 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
138 private void checkStubJsonDelete(String url) throws Exception {
139 var response = super.deleteResource(url, APP_JSON, apiPort);
140 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());