Allow semantic versioning in all templates in api
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / TestApiRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4  *  Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2019-2020, 2022-2023 Nordix Foundation.
6  *  Modifications Copyright (C) 2020-2023 Bell Canada. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.rest;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertFalse;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertNull;
31
32 import jakarta.ws.rs.client.Invocation;
33 import jakarta.ws.rs.core.Response;
34 import jakarta.ws.rs.core.Response.Status;
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.List;
38 import java.util.Map;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.Test;
41 import org.onap.policy.api.main.PolicyApiApplication;
42 import org.onap.policy.api.main.rest.utils.CommonTestRestController;
43 import org.onap.policy.common.endpoints.report.HealthCheckReport;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.network.NetworkUtil;
46 import org.onap.policy.common.utils.resources.ResourceUtils;
47 import org.onap.policy.common.utils.resources.TextFileUtils;
48 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
49 import org.onap.policy.models.errors.concepts.ErrorResponse;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.web.server.LocalServerPort;
53 import org.springframework.test.annotation.DirtiesContext;
54 import org.springframework.test.annotation.DirtiesContext.ClassMode;
55 import org.springframework.test.context.ActiveProfiles;
56 import org.springframework.test.context.DynamicPropertyRegistry;
57 import org.springframework.test.context.DynamicPropertySource;
58
59 /**
60  * Class to perform unit test of {@link ApiRestController}.
61  *
62  * @author Chenfei Gao (cgao@research.att.com)
63  */
64 @SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
65 @ActiveProfiles({ "test", "default" })
66 @DirtiesContext(classMode = ClassMode.AFTER_CLASS)
67 class TestApiRestServer extends CommonTestRestController {
68
69     private static final String ALIVE = "alive";
70     private static final String SELF = NetworkUtil.getHostname();
71     private static final String NAME = "Policy API";
72     private static final String APP_JSON = "application/json";
73     private static final String APP_YAML = "application/yaml";
74
75     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
76
77     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
78
79     private static final String POLICYTYPES = "policytypes";
80     private static final String POLICYTYPES_TCA = "policytypes/onap.policies.monitoring.tcagen2";
81     private static final String POLICYTYPES_COLLECTOR =
82             "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
83     private static final String POLICYTYPES_TCA_VERSION = "policytypes/onap.policies.monitoring.tcagen2/versions/1.0.0";
84     private static final String POLICYTYPES_TCA_LATEST = "policytypes/onap.policies.monitoring.tcagen2/versions/latest";
85     private static final String POLICYTYPES_COLLECTOR_VERSION =
86             "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/1.0.0";
87     private static final String POLICYTYPES_COLLECTOR_LATEST =
88             "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/latest";
89
90     private static final String POLICYTYPES_DROOLS = "policytypes/onap.policies.controlloop.operational.common.Drools";
91     private static final String POLICYTYPES_DROOLS_VERSION = POLICYTYPES_DROOLS + "/versions/1.0.0";
92     private static final String POLICYTYPES_DROOLS_VERSION_LATEST = POLICYTYPES_DROOLS + "/versions/latest";
93
94     private static final String POLICYTYPES_NAMING_VERSION = POLICYTYPES + "/onap.policies.Naming/versions/1.0.0";
95
96     private static final String POLICYTYPES_TCA_POLICIES =
97             "policytypes/onap.policies.monitoring.tcagen2/versions/1.0.0/policies";
98     private static final String POLICYTYPES_TCA_POLICIES_VCPE =
99             "policytypes/onap.policies.monitoring.tcagen2/versions/1.0.0/policies/onap.restart.tca";
100     private static final String POLICYTYPES_TCA_POLICIES_VCPE_VERSION1 =
101             "policytypes/" + "onap.policies.monitoring.tcagen2/versions/1.0.0/policies/onap.restart.tca/versions/1.0.0";
102     private static final String POLICYTYPES_TCA_POLICIES_VCPE_LATEST = "policytypes/"
103             + "onap.policies.monitoring.tcagen2/versions/1.0.0/policies/onap.restart.tca/versions/latest";
104
105     private static final String POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION =
106             POLICYTYPES_DROOLS_VERSION + "/policies/" + OP_POLICY_NAME_VCPE + "/versions/1.0.0";
107
108     private static final String POLICIES = "policies";
109
110     private static final String TOSCA_POLICY_VER_RESOURCE =
111         "policytypes/onap.restart.tca.snapshot.yaml";
112     // @formatter:off
113
114     private static final String[] TOSCA_POLICY_RESOURCE_NAMES = {"policies/vCPE.policy.monitoring.input.tosca.json",
115         "policies/vCPE.policy.monitoring.input.tosca.yaml", "policies/vDNS.policy.monitoring.input.tosca.json",
116         "policies/vDNS.policy.monitoring.input.tosca.v2.yaml"};
117
118     private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = {
119         "policies/vCPE.policies.optimization.input.tosca.json", "policies/vCPE.policies.optimization.input.tosca.yaml"};
120
121     private static final String TOSCA_POLICYTYPE_OP_RESOURCE =
122         "policytypes/onap.policies.controlloop.operational.Common.yaml";
123
124     private static final String TOSCA_POLICYTYPE_VER_RESOURCE =
125         "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.snapshot.yaml";
126
127     private static final String[] TOSCA_POLICYTYPE_RESOURCE_NAMES = {
128         "policytypes/onap.policies.monitoring.tcagen2.yaml",
129         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
130         "policytypes/onap.policies.controlloop.operational.common.Drools.yaml",
131         "policytypes/onap.policies.controlloop.guard.Common.yaml",
132         "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml",
133         "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml",
134         "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml",
135         "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
136         "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
137         "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
138         "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
139         "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
140         "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
141         "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
142         "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
143         "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
144         "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"};
145
146     private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON =
147         "policies/vCPE.policy.operational.input.tosca.json";
148
149     private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML =
150         "policies/vCPE.policy.operational.input.tosca.yaml";
151
152     private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0";
153     // @formatter:on
154
155     private static final StandardCoder standardCoder = new StandardCoder();
156     private static SelfSignedKeyStore keystore;
157
158     @LocalServerPort
159     private int apiPort;
160
161     /**
162      * Initializes parameters and set up test environment.
163      *
164      * @throws IOException on I/O exceptions
165      * @throws InterruptedException if interrupted
166      */
167     @BeforeAll
168     static void setupParameters() throws IOException, InterruptedException {
169         keystore = new SelfSignedKeyStore();
170     }
171
172     @DynamicPropertySource
173     static void registerPgProperties(DynamicPropertyRegistry registry) {
174         registry.add("server.ssl.enabled", () -> "true");
175         registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
176         registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
177         registry.add("server.ssl.key-store-type", () -> "PKCS12");
178         registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
179         registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
180     }
181
182     @Test
183     void testSwagger() throws Exception {
184         super.testSwagger(apiPort);
185     }
186
187     @Test
188     void testCreatePolicyTypes() throws Exception {
189         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
190             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
191             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
192             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
193             assertNotNull(response);
194             assertFalse(response.getPolicyTypes().isEmpty());
195         }
196
197         // Send a policy type with a null value to trigger an error
198         Response rawResponse = readResource(POLICYTYPES, APP_JSON, apiPort);
199         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
200         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
201         String firstPolicyType = response.getPolicyTypes().keySet().iterator().next();
202         response.getPolicyTypes().put(firstPolicyType, null);
203         Response rawResponse2 = createResource(POLICYTYPES, standardCoder.encode(response), apiPort);
204         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse2.getStatus());
205         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
206         assertEquals("no policy types specified on service template", errorResponse.getErrorMessage());
207     }
208
209     @Test
210     void testCreatePolicies() throws Exception {
211         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
212             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
213             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
214         }
215
216         new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
217
218         // Send a policy with no policy type trigger an error
219         String toscaPolicy = ResourceUtils
220                 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
221
222         toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
223         TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
224
225         Response rawResponse2 =
226                 createResource(POLICYTYPES_TCA_POLICIES,
227                     "src/test/resources/policies/BadTestPolicy.yaml", apiPort);
228         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
229         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
230         assertThat(errorResponse.getErrorMessage())
231                 .contains("item \"entity\" value \"onap.restart.tca:1.0.0\" INVALID, does not equal existing entity");
232     }
233
234     @Test
235     void testSimpleCreatePolicies() throws Exception {
236         for (String resrcName : TOSCA_POLICIES_RESOURCE_NAMES) {
237             Response rawResponse = createResource(POLICIES, resrcName, apiPort);
238             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
239         }
240
241         new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
242
243         // Send a policy with no policy type trigger an error
244         String toscaPolicy = ResourceUtils
245                 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
246
247         toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
248         toscaPolicy = toscaPolicy.replaceAll("onap.restart.tca", "onap.restart.tca.IDontExist");
249         TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
250
251         Response rawResponse2 =
252             createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml", apiPort);
253         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
254         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
255         assertThat(errorResponse.getErrorMessage())
256                 .contains("item \"policy type\" value \"IDontExist:1.0.0\" INVALID, not found");
257     }
258
259     @Test
260     void testPoliciesVersioning() throws Exception {
261         var rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_VER_RESOURCE, apiPort);
262         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
263
264         rawResponse = createResource(POLICIES, TOSCA_POLICY_VER_RESOURCE, apiPort);
265         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
266     }
267
268     @SuppressWarnings("unchecked")
269     @Test
270     void testToscaCompliantOpDroolsPolicies() throws Exception {
271         Response rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_OP_RESOURCE, apiPort);
272         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
273
274         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, APP_JSON, apiPort);
275         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
276
277         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON, apiPort);
278         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
279
280         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML, apiPort);
281         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
282
283         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
284         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
285
286         rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
287         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
288
289         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML, apiPort);
290         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
291
292         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
293         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
294
295         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_YAML, apiPort);
296         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
297
298         ToscaServiceTemplate toscaVcpeSt = rawResponse.readEntity(ToscaServiceTemplate.class);
299         assertEquals(1, toscaVcpeSt.getToscaTopologyTemplate().getPolicies().size());
300         assertEquals(OP_POLICY_NAME_VCPE,
301                 toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getName());
302
303         Map<String, Object> props =
304                 toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getProperties();
305         assertNotNull(props);
306
307         List<Object> operations = (List<Object>) props.get("operations");
308         assertEquals(1, operations.size());
309         assertEquals(props.get("trigger"), ((Map<String, Object>) operations.get(0)).get("id"));
310
311         Map<String, Object> operation =
312                 (Map<String, Object>) ((Map<String, Object>) operations.get(0)).get("operation");
313         assertEquals("APPC", operation.get("actor"));
314         assertEquals("Restart", operation.get("operation"));
315
316         rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
317         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
318     }
319
320     @Test
321     void testHealthCheckSuccessJson() throws Exception {
322         testHealthCheckSuccess(APP_JSON);
323     }
324
325     @Test
326     void testHealthCheckSuccessYaml() throws Exception {
327         testHealthCheckSuccess(APP_YAML);
328     }
329
330     private void testHealthCheckSuccess(String mediaType) throws Exception {
331         final Invocation.Builder invocationBuilder = sendHttpsRequest(
332                 HEALTHCHECK_ENDPOINT, mediaType, apiPort);
333         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
334         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
335     }
336
337     @Test
338     void testReadPolicyTypesJson() throws Exception {
339         testReadPolicyTypes(APP_JSON);
340     }
341
342     @Test
343     void testReadPolicyTypesYaml() throws Exception {
344         testReadPolicyTypes(APP_YAML);
345     }
346
347     private void testReadPolicyTypes(String mediaType) throws Exception {
348         Response rawResponse =
349             readResource("policytypes/onap.policies.optimization.resource.HpaPolicy", mediaType,
350                 apiPort);
351         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
352         ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
353         assertNotNull(namingServiceTemplate);
354         assertEquals(3, namingServiceTemplate.getPolicyTypesAsMap().size());
355         assertEquals(5, namingServiceTemplate.getDataTypesAsMap().size());
356
357         rawResponse = readResource(POLICYTYPES, mediaType, apiPort);
358         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
359         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
360         assertFalse(response.getPolicyTypes().isEmpty());
361
362         rawResponse = readResource(POLICYTYPES_TCA, mediaType, apiPort);
363         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
364
365         rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType, apiPort);
366         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
367
368         rawResponse = readResource(POLICYTYPES_TCA_LATEST, mediaType, apiPort);
369         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
370
371         rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType, apiPort);
372         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
373
374         rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType, apiPort);
375         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
376
377         rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType, apiPort);
378         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
379
380         rawResponse = readResource(POLICYTYPES_DROOLS, mediaType, apiPort);
381         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
382
383         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, mediaType, apiPort);
384         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
385
386         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION_LATEST, mediaType, apiPort);
387         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
388
389         rawResponse = readResource(POLICYTYPES_NAMING_VERSION, mediaType, apiPort);
390         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
391     }
392
393     @Test
394     void testDeletePolicyTypeJson() throws Exception {
395         testDeletePolicyType(APP_JSON);
396     }
397
398     @Test
399     void testDeletePolicyTypeYaml() throws Exception {
400         testDeletePolicyType(APP_YAML);
401     }
402
403     private void testDeletePolicyType(String mediaType) throws Exception {
404         Response rawResponse = deleteResource("policytypes/onap.policies.IDoNotExist/versions/1.0.0",
405             mediaType, apiPort);
406         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
407
408         rawResponse = createResource(POLICYTYPES, "policytypes/onap.policies.Test.yaml", apiPort);
409         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
410
411         rawResponse =
412             readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
413         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
414
415         rawResponse =
416             deleteResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
417         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
418
419         rawResponse =
420             readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
421         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
422     }
423
424     @Test
425     void testReadPoliciesJson() throws Exception {
426         testReadPolicies(APP_JSON);
427     }
428
429     @Test
430     void testReadPoliciesYaml() throws Exception {
431         testReadPolicies(APP_YAML);
432     }
433
434     private void testReadPolicies(String mediaType) throws Exception {
435         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
436             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
437             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
438         }
439
440         Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType, apiPort);
441         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
442
443         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType, apiPort);
444         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
445
446         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
447         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
448
449         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType, apiPort);
450         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
451
452         rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
453         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
454
455     }
456
457     @Test
458     void testNamingPolicyGet() throws Exception {
459
460         Response rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
461                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON, apiPort);
462         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
463
464         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
465                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0?mode=referenced", APP_JSON, apiPort);
466         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
467
468         ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
469         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
470         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
471         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
472
473         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
474                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest?mode=referenced", APP_JSON, apiPort);
475         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
476
477         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
478         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
479         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
480         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
481
482         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies"
483                 + "?mode=referenced", APP_JSON, apiPort);
484         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
485
486         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
487         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
488         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
489         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
490
491         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
492                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON, apiPort);
493         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
494
495         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
496
497         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
498         assertNull(namingServiceTemplate.getPolicyTypes());
499         assertNull(namingServiceTemplate.getDataTypes());
500
501         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
502                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest", APP_JSON, apiPort);
503         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
504
505         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
506         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
507         assertNull(namingServiceTemplate.getPolicyTypes());
508         assertNull(namingServiceTemplate.getDataTypes());
509
510         rawResponse =
511             readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies", APP_JSON,
512                 apiPort);
513         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
514
515         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
516         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
517         assertNull(namingServiceTemplate.getPolicyTypes());
518         assertNull(namingServiceTemplate.getDataTypes());
519     }
520
521     @Test
522     void testDeletePoliciesJson() throws Exception {
523         testDeletePolicies(APP_JSON);
524     }
525
526     @Test
527     void testDeletePoliciesYaml() throws Exception {
528         testDeletePolicies(APP_YAML);
529     }
530
531     private void testDeletePolicies(String mediaType) throws Exception {
532         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
533         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
534         ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
535         assertEquals("policy onap.restart.tca:1.0.0 not found", error.getErrorMessage());
536     }
537
538     @Test
539     void testDeletePolicyVersionJson() throws Exception {
540         testDeletePolicyVersion(APP_JSON);
541     }
542
543     @Test
544     void testDeletePolicyVersionYaml() throws Exception {
545         testDeletePolicyVersion(APP_YAML);
546     }
547
548     private void testDeletePolicyVersion(String mediaType) throws Exception {
549         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
550             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
551             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
552             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
553             assertNotNull(response);
554             assertFalse(response.getPolicyTypes().isEmpty());
555         }
556         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
557             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
558             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
559         }
560         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
561         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
562
563         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
564         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
565         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
566         assertEquals("policies for onap.restart.tca:1.0.0 do not exist", errorResponse.getErrorMessage());
567
568         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType, apiPort);
569         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
570         errorResponse = rawResponse.readEntity(ErrorResponse.class);
571         assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
572
573         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType, apiPort);
574         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
575         errorResponse = rawResponse.readEntity(ErrorResponse.class);
576         assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
577     }
578
579     @Test
580     void testGetAllVersionOfPolicyJson() throws Exception {
581         testGetAllVersionOfPolicy(APP_JSON);
582     }
583
584     @Test
585     void testGetAllVersionOfPolicyYaml() throws Exception {
586         testGetAllVersionOfPolicy(APP_YAML);
587     }
588
589     private void testGetAllVersionOfPolicy(String mediaType) throws Exception {
590         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
591             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
592             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
593             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
594             assertNotNull(response);
595             assertFalse(response.getPolicyTypes().isEmpty());
596         }
597         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
598             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
599             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
600         }
601         Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType, apiPort);
602         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
603     }
604
605     @Test
606     void testGetPoliciesJson() throws Exception {
607         getPolicies(APP_JSON);
608     }
609
610     @Test
611     void testGetPoliciesYaml() throws Exception {
612         getPolicies(APP_YAML);
613     }
614
615     private void getPolicies(String mediaType) throws Exception {
616         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
617             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
618             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
619             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
620             assertThat(response).isNotNull();
621             assertThat(response.getPolicyTypes()).isNotEmpty();
622         }
623         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
624             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
625             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
626         }
627         Response rawResponse = readResource(POLICIES, mediaType, apiPort);
628         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
629         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
630         assertThat(response.getToscaTopologyTemplate().getPolicies()).isNotEmpty();
631     }
632
633     @Test
634     void testGetSpecificPolicyJson() throws Exception {
635         getSpecificPolicy(APP_JSON);
636     }
637
638     @Test
639     void testGetSpecificPolicyYaml() throws Exception {
640         getSpecificPolicy(APP_YAML);
641     }
642
643     private void getSpecificPolicy(String mediaType) throws Exception {
644         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
645             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
646             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
647             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
648             assertThat(response).isNotNull();
649             assertThat(response.getPolicyTypes()).isNotEmpty();
650         }
651         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
652             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
653             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
654         }
655         Response rawResponse = readResource(POLICIES_VCPE_VERSION1, mediaType, apiPort);
656         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
657         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
658         assertThat(response.getToscaTopologyTemplate().getPolicies()).hasSize(1);
659     }
660
661     @Test
662     void testDeleteSpecificPolicy() throws Exception {
663         Response rawResponse;
664         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
665             rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
666             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
667             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
668             assertThat(response).isNotNull();
669             assertThat(response.getPolicyTypes()).isNotEmpty();
670         }
671         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
672             rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
673             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
674         }
675
676         rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
677         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
678
679         // delete a particular policy
680         rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
681         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
682
683         rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
684         assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
685
686         rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
687         assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
688
689     }
690
691     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
692             final String message, final HealthCheckReport report) {
693
694         assertEquals(name, report.getName());
695         assertEquals(url, report.getUrl());
696         assertEquals(healthy, report.isHealthy());
697         assertEquals(code, report.getCode());
698         assertEquals(message, report.getMessage());
699     }
700 }