Removed db-based statistics feature
[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 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;
42
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 ApiContractTest extends CommonTestRestController {
48     protected static final String APP_JSON = "application/json";
49     protected static final String APP_YAML = "application/yaml";
50     private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
51             "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
52
53     @LocalServerPort
54     private int apiPort;
55
56     private static SelfSignedKeyStore keystore;
57
58
59     @BeforeClass
60     public static void setupParameters() throws IOException, InterruptedException {
61         keystore = new SelfSignedKeyStore();
62     }
63
64     @DynamicPropertySource
65     static void registerPgProperties(DynamicPropertyRegistry registry) {
66         registry.add("server.ssl.enabled", () -> "true");
67         registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
68         registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
69         registry.add("server.ssl.key-store-type", () -> "PKCS12");
70         registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
71         registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
72     }
73
74     @Test
75     public void testStubPolicyDesign() throws Exception {
76         checkStubJsonGet("policies");
77         checkStubJsonGet("policies/policyname/versions/1.0.2");
78         checkStubJsonGet("policytypes");
79         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16");
80         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/latest");
81         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/");
82         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies");
83         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
84             + "9c65fa1f-2833-4076-a64d-5b62e35cd09b");
85         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
86                 + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/latest");
87         checkStubJsonGet("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.0.0/policies/"
88                 + "9c65fa1f-2833-4076-a64d-5b62e35cd09b/versions/1.2.3");
89         checkStubJsonGet("healthcheck");
90
91         checkStubJsonPost("policies");
92         checkStubJsonPost("policytypes");
93         checkStubJsonPost("policytypes/380d5cb1-e43d-45b7-b10b-ebd15dfabd16/versions/1.2.3/policies");
94
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");
99     }
100
101     @Test
102     public void testStubNodeTemplateDesign() throws Exception {
103         checkStubJsonGet("nodetemplates");
104         checkStubJsonGet("nodetemplates/k8stemplate/versions/1.0.0");
105
106         checkStubJsonPost("nodetemplates");
107
108         checkStubJsonPut("nodetemplates");
109
110         checkStubJsonDelete("nodetemplates/k8stemplate/versions/1.0.0");
111     }
112
113     @Test
114     public void testErrors() throws Exception {
115         var responseYaml = super.readResource("policies", APP_YAML, apiPort);
116         assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseYaml.getStatus());
117
118         var responseListYaml = super.readResource("nodetemplates", APP_YAML, apiPort);
119         assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), responseListYaml.getStatus());
120
121     }
122
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());
126     }
127
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());
131     }
132
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());
136     }
137
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());
141     }
142
143 }