Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/api.git] / main / src / test / java / org / onap / policy / api / contract / ApiContractTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2023 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.api.contract;
23
24 import static org.junit.Assert.assertEquals;
25
26 import jakarta.ws.rs.core.Response;
27 import java.io.IOException;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
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
41 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
42 @ActiveProfiles({"test", "stub"})
43 @DirtiesContext(classMode = ClassMode.AFTER_CLASS)
44 class ApiContractTest extends CommonTestRestController {
45     protected static final String APP_JSON = "application/json";
46     protected static final String APP_YAML = "application/yaml";
47     private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
48         "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
49
50     @LocalServerPort
51     private int apiPort;
52
53     private static SelfSignedKeyStore keystore;
54
55     @BeforeAll
56     public static void setupParameters() throws IOException, InterruptedException {
57         keystore = new SelfSignedKeyStore();
58     }
59
60     @DynamicPropertySource
61     static void registerPgProperties(DynamicPropertyRegistry registry) {
62         registry.add("server.ssl.enabled", () -> "true");
63         registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
64         registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
65         registry.add("server.ssl.key-store-type", () -> "PKCS12");
66         registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
67         registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
68     }
69
70     @Test
71     void testStubPolicyDesign() throws Exception {
72         checkStubJsonGet("policies");
73         checkStubJsonGet("policies/policyname/versions/1.0.2");
74         checkStubJsonGet("policytypes");
75         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16");
76         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/latest");
77         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0");
78         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies");
79         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
80             + "9c65fa1f-2833-4076-a64d-5b62e35cd09b");
81         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
82             + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/latest");
83         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
84             + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/1.2.3");
85         checkStubJsonGet("healthcheck");
86
87         checkStubJsonPost("policies");
88         checkStubJsonPost("policytypes");
89         checkStubJsonPost("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.2.3/policies");
90
91         checkStubJsonDelete("policies/policyname/versions/1.0.2");
92         checkStubJsonDelete("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0");
93         checkStubJsonDelete("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
94             + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/1.2.3");
95     }
96
97     @Test
98     void testStubNodeTemplateDesign() throws Exception {
99         checkStubJsonGet("nodetemplates");
100         checkStubJsonGet("nodetemplates/k8stemplate/versions/1.0.0");
101
102         checkStubJsonPost("nodetemplates");
103
104         checkStubJsonPut("nodetemplates");
105
106         checkStubJsonDelete("nodetemplates/k8stemplate/versions/1.0.0");
107     }
108
109     @Test
110     void testErrors() throws Exception {
111         var responseYaml = super.readResource("policies", APP_YAML, apiPort);
112         assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseYaml.getStatus());
113
114         var responseListYaml = super.readResource("nodetemplates", APP_YAML, apiPort);
115         assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseListYaml.getStatus());
116
117     }
118
119     private void checkStubJsonGet(String url) throws Exception {
120         var response = super.readResource(url, APP_JSON, apiPort);
121         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
122     }
123
124     private void checkStubJsonPost(String url) throws Exception {
125         var response = super.createResource(url, TOSCA_NODE_TEMPLATE_RESOURCE, apiPort);
126         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
127     }
128
129     private void checkStubJsonPut(String url) throws Exception {
130         var response = super.updateResource(url, TOSCA_NODE_TEMPLATE_RESOURCE, APP_JSON, apiPort);
131         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
132     }
133
134     private void checkStubJsonDelete(String url) throws Exception {
135         var response = super.deleteResource(url, APP_JSON, apiPort);
136         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
137     }
138
139 }