4bcd86ca3edca67ac3b8cc5ecb460f08709de9c7
[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     // @formatter:off
111     private static final String[] TOSCA_POLICY_RESOURCE_NAMES = {"policies/vCPE.policy.monitoring.input.tosca.json",
112         "policies/vCPE.policy.monitoring.input.tosca.yaml", "policies/vDNS.policy.monitoring.input.tosca.json",
113         "policies/vDNS.policy.monitoring.input.tosca.v2.yaml"};
114
115     private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = {
116         "policies/vCPE.policies.optimization.input.tosca.json", "policies/vCPE.policies.optimization.input.tosca.yaml"};
117
118     private static final String TOSCA_POLICYTYPE_OP_RESOURCE =
119         "policytypes/onap.policies.controlloop.operational.Common.yaml";
120
121     private static final String[] TOSCA_POLICYTYPE_RESOURCE_NAMES = {
122         "policytypes/onap.policies.monitoring.tcagen2.yaml",
123         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
124         "policytypes/onap.policies.controlloop.operational.common.Drools.yaml",
125         "policytypes/onap.policies.controlloop.guard.Common.yaml",
126         "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml",
127         "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml",
128         "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml",
129         "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
130         "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
131         "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
132         "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
133         "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
134         "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
135         "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
136         "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
137         "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
138         "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"};
139
140     private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON =
141         "policies/vCPE.policy.operational.input.tosca.json";
142
143     private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML =
144         "policies/vCPE.policy.operational.input.tosca.yaml";
145
146     private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0";
147     // @formatter:on
148
149     private static final StandardCoder standardCoder = new StandardCoder();
150     private static SelfSignedKeyStore keystore;
151
152     @LocalServerPort
153     private int apiPort;
154
155     /**
156      * Initializes parameters and set up test environment.
157      *
158      * @throws IOException on I/O exceptions
159      * @throws InterruptedException if interrupted
160      */
161     @BeforeAll
162     static void setupParameters() throws IOException, InterruptedException {
163         keystore = new SelfSignedKeyStore();
164     }
165
166     @DynamicPropertySource
167     static void registerPgProperties(DynamicPropertyRegistry registry) {
168         registry.add("server.ssl.enabled", () -> "true");
169         registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
170         registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
171         registry.add("server.ssl.key-store-type", () -> "PKCS12");
172         registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
173         registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
174     }
175
176     @Test
177     void testSwagger() throws Exception {
178         super.testSwagger(apiPort);
179     }
180
181     @Test
182     void testCreatePolicyTypes() throws Exception {
183         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
184             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
185             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
186             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
187             assertNotNull(response);
188             assertFalse(response.getPolicyTypes().isEmpty());
189         }
190
191         // Send a policy type with a null value to trigger an error
192         Response rawResponse = readResource(POLICYTYPES, APP_JSON, apiPort);
193         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
194         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
195         String firstPolicyType = response.getPolicyTypes().keySet().iterator().next();
196         response.getPolicyTypes().put(firstPolicyType, null);
197         Response rawResponse2 = createResource(POLICYTYPES, standardCoder.encode(response), apiPort);
198         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse2.getStatus());
199         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
200         assertEquals("no policy types specified on service template", errorResponse.getErrorMessage());
201     }
202
203     @Test
204     void testCreatePolicies() throws Exception {
205         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
206             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
207             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
208         }
209
210         new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
211
212         // Send a policy with no policy type trigger an error
213         String toscaPolicy = ResourceUtils
214                 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
215
216         toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
217         TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
218
219         Response rawResponse2 =
220                 createResource(POLICYTYPES_TCA_POLICIES,
221                     "src/test/resources/policies/BadTestPolicy.yaml", apiPort);
222         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
223         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
224         assertThat(errorResponse.getErrorMessage())
225                 .contains("item \"entity\" value \"onap.restart.tca:1.0.0\" INVALID, does not equal existing entity");
226     }
227
228     @Test
229     void testSimpleCreatePolicies() throws Exception {
230         for (String resrcName : TOSCA_POLICIES_RESOURCE_NAMES) {
231             Response rawResponse = createResource(POLICIES, resrcName, apiPort);
232             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
233         }
234
235         new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
236
237         // Send a policy with no policy type trigger an error
238         String toscaPolicy = ResourceUtils
239                 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
240
241         toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
242         toscaPolicy = toscaPolicy.replaceAll("onap.restart.tca", "onap.restart.tca.IDontExist");
243         TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
244
245         Response rawResponse2 =
246             createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml", apiPort);
247         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
248         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
249         assertThat(errorResponse.getErrorMessage())
250                 .contains("item \"policy type\" value \"IDontExist:1.0.0\" INVALID, not found");
251     }
252
253     @SuppressWarnings("unchecked")
254     @Test
255     void testToscaCompliantOpDroolsPolicies() throws Exception {
256         Response rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_OP_RESOURCE, apiPort);
257         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
258
259         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, APP_JSON, apiPort);
260         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
261
262         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON, apiPort);
263         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
264
265         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML, apiPort);
266         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
267
268         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
269         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
270
271         rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
272         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
273
274         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML, apiPort);
275         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
276
277         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
278         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
279
280         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_YAML, apiPort);
281         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
282
283         ToscaServiceTemplate toscaVcpeSt = rawResponse.readEntity(ToscaServiceTemplate.class);
284         assertEquals(1, toscaVcpeSt.getToscaTopologyTemplate().getPolicies().size());
285         assertEquals(OP_POLICY_NAME_VCPE,
286                 toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getName());
287
288         Map<String, Object> props =
289                 toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getProperties();
290         assertNotNull(props);
291
292         List<Object> operations = (List<Object>) props.get("operations");
293         assertEquals(1, operations.size());
294         assertEquals(props.get("trigger"), ((Map<String, Object>) operations.get(0)).get("id"));
295
296         Map<String, Object> operation =
297                 (Map<String, Object>) ((Map<String, Object>) operations.get(0)).get("operation");
298         assertEquals("APPC", operation.get("actor"));
299         assertEquals("Restart", operation.get("operation"));
300
301         rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
302         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
303     }
304
305     @Test
306     void testHealthCheckSuccessJson() throws Exception {
307         testHealthCheckSuccess(APP_JSON);
308     }
309
310     @Test
311     void testHealthCheckSuccessYaml() throws Exception {
312         testHealthCheckSuccess(APP_YAML);
313     }
314
315     private void testHealthCheckSuccess(String mediaType) throws Exception {
316         final Invocation.Builder invocationBuilder = sendHttpsRequest(
317                 HEALTHCHECK_ENDPOINT, mediaType, apiPort);
318         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
319         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
320     }
321
322     @Test
323     void testReadPolicyTypesJson() throws Exception {
324         testReadPolicyTypes(APP_JSON);
325     }
326
327     @Test
328     void testReadPolicyTypesYaml() throws Exception {
329         testReadPolicyTypes(APP_YAML);
330     }
331
332     private void testReadPolicyTypes(String mediaType) throws Exception {
333         Response rawResponse =
334             readResource("policytypes/onap.policies.optimization.resource.HpaPolicy", mediaType,
335                 apiPort);
336         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
337         ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
338         assertNotNull(namingServiceTemplate);
339         assertEquals(3, namingServiceTemplate.getPolicyTypesAsMap().size());
340         assertEquals(5, namingServiceTemplate.getDataTypesAsMap().size());
341
342         rawResponse = readResource(POLICYTYPES, mediaType, apiPort);
343         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
344         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
345         assertFalse(response.getPolicyTypes().isEmpty());
346
347         rawResponse = readResource(POLICYTYPES_TCA, mediaType, apiPort);
348         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
349
350         rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType, apiPort);
351         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
352
353         rawResponse = readResource(POLICYTYPES_TCA_LATEST, mediaType, apiPort);
354         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
355
356         rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType, apiPort);
357         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
358
359         rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType, apiPort);
360         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
361
362         rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType, apiPort);
363         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
364
365         rawResponse = readResource(POLICYTYPES_DROOLS, mediaType, apiPort);
366         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
367
368         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, mediaType, apiPort);
369         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
370
371         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION_LATEST, mediaType, apiPort);
372         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
373
374         rawResponse = readResource(POLICYTYPES_NAMING_VERSION, mediaType, apiPort);
375         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
376     }
377
378     @Test
379     void testDeletePolicyTypeJson() throws Exception {
380         testDeletePolicyType(APP_JSON);
381     }
382
383     @Test
384     void testDeletePolicyTypeYaml() throws Exception {
385         testDeletePolicyType(APP_YAML);
386     }
387
388     private void testDeletePolicyType(String mediaType) throws Exception {
389         Response rawResponse = deleteResource("policytypes/onap.policies.IDoNotExist/versions/1.0.0",
390             mediaType, apiPort);
391         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
392
393         rawResponse = createResource(POLICYTYPES, "policytypes/onap.policies.Test.yaml", apiPort);
394         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
395
396         rawResponse =
397             readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
398         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
399
400         rawResponse =
401             deleteResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
402         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
403
404         rawResponse =
405             readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
406         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
407     }
408
409     @Test
410     void testReadPoliciesJson() throws Exception {
411         testReadPolicies(APP_JSON);
412     }
413
414     @Test
415     void testReadPoliciesYaml() throws Exception {
416         testReadPolicies(APP_YAML);
417     }
418
419     private void testReadPolicies(String mediaType) throws Exception {
420         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
421             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
422             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
423         }
424
425         Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType, apiPort);
426         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
427
428         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType, apiPort);
429         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
430
431         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
432         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
433
434         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType, apiPort);
435         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
436
437         rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
438         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
439
440     }
441
442     @Test
443     void testNamingPolicyGet() throws Exception {
444
445         Response rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
446                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON, apiPort);
447         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
448
449         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
450                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0?mode=referenced", APP_JSON, apiPort);
451         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
452
453         ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
454         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
455         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
456         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
457
458         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
459                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest?mode=referenced", APP_JSON, apiPort);
460         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
461
462         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
463         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
464         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
465         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
466
467         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies"
468                 + "?mode=referenced", APP_JSON, apiPort);
469         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
470
471         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
472         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
473         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
474         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
475
476         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
477                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON, apiPort);
478         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
479
480         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
481
482         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
483         assertNull(namingServiceTemplate.getPolicyTypes());
484         assertNull(namingServiceTemplate.getDataTypes());
485
486         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
487                 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest", APP_JSON, apiPort);
488         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
489
490         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
491         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
492         assertNull(namingServiceTemplate.getPolicyTypes());
493         assertNull(namingServiceTemplate.getDataTypes());
494
495         rawResponse =
496             readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies", APP_JSON,
497                 apiPort);
498         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
499
500         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
501         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
502         assertNull(namingServiceTemplate.getPolicyTypes());
503         assertNull(namingServiceTemplate.getDataTypes());
504     }
505
506     @Test
507     void testDeletePoliciesJson() throws Exception {
508         testDeletePolicies(APP_JSON);
509     }
510
511     @Test
512     void testDeletePoliciesYaml() throws Exception {
513         testDeletePolicies(APP_YAML);
514     }
515
516     private void testDeletePolicies(String mediaType) throws Exception {
517         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
518         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
519         ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
520         assertEquals("policy onap.restart.tca:1.0.0 not found", error.getErrorMessage());
521     }
522
523     @Test
524     void testDeletePolicyVersionJson() throws Exception {
525         testDeletePolicyVersion(APP_JSON);
526     }
527
528     @Test
529     void testDeletePolicyVersionYaml() throws Exception {
530         testDeletePolicyVersion(APP_YAML);
531     }
532
533     private void testDeletePolicyVersion(String mediaType) throws Exception {
534         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
535             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
536             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
537             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
538             assertNotNull(response);
539             assertFalse(response.getPolicyTypes().isEmpty());
540         }
541         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
542             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
543             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
544         }
545         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
546         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
547
548         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
549         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
550         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
551         assertEquals("policies for onap.restart.tca:1.0.0 do not exist", errorResponse.getErrorMessage());
552
553         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType, apiPort);
554         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
555         errorResponse = rawResponse.readEntity(ErrorResponse.class);
556         assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
557
558         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType, apiPort);
559         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
560         errorResponse = rawResponse.readEntity(ErrorResponse.class);
561         assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
562     }
563
564     @Test
565     void testGetAllVersionOfPolicyJson() throws Exception {
566         testGetAllVersionOfPolicy(APP_JSON);
567     }
568
569     @Test
570     void testGetAllVersionOfPolicyYaml() throws Exception {
571         testGetAllVersionOfPolicy(APP_YAML);
572     }
573
574     private void testGetAllVersionOfPolicy(String mediaType) throws Exception {
575         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
576             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
577             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
578             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
579             assertNotNull(response);
580             assertFalse(response.getPolicyTypes().isEmpty());
581         }
582         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
583             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
584             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
585         }
586         Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType, apiPort);
587         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
588     }
589
590     @Test
591     void testGetPoliciesJson() throws Exception {
592         getPolicies(APP_JSON);
593     }
594
595     @Test
596     void testGetPoliciesYaml() throws Exception {
597         getPolicies(APP_YAML);
598     }
599
600     private void getPolicies(String mediaType) throws Exception {
601         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
602             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
603             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
604             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
605             assertThat(response).isNotNull();
606             assertThat(response.getPolicyTypes()).isNotEmpty();
607         }
608         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
609             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
610             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
611         }
612         Response rawResponse = readResource(POLICIES, mediaType, apiPort);
613         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
614         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
615         assertThat(response.getToscaTopologyTemplate().getPolicies()).isNotEmpty();
616     }
617
618     @Test
619     void testGetSpecificPolicyJson() throws Exception {
620         getSpecificPolicy(APP_JSON);
621     }
622
623     @Test
624     void testGetSpecificPolicyYaml() throws Exception {
625         getSpecificPolicy(APP_YAML);
626     }
627
628     private void getSpecificPolicy(String mediaType) throws Exception {
629         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
630             Response rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
631             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
632             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
633             assertThat(response).isNotNull();
634             assertThat(response.getPolicyTypes()).isNotEmpty();
635         }
636         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
637             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
638             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
639         }
640         Response rawResponse = readResource(POLICIES_VCPE_VERSION1, mediaType, apiPort);
641         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
642         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
643         assertThat(response.getToscaTopologyTemplate().getPolicies()).hasSize(1);
644     }
645
646     @Test
647     void testDeleteSpecificPolicy() throws Exception {
648         Response rawResponse;
649         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
650             rawResponse = createResource(POLICYTYPES, resrcName, apiPort);
651             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
652             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
653             assertThat(response).isNotNull();
654             assertThat(response.getPolicyTypes()).isNotEmpty();
655         }
656         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
657             rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName, apiPort);
658             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
659         }
660
661         rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
662         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
663
664         // delete a particular policy
665         rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
666         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
667
668         rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
669         assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
670
671         rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
672         assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
673
674     }
675
676     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
677             final String message, final HealthCheckReport report) {
678
679         assertEquals(name, report.getName());
680         assertEquals(url, report.getUrl());
681         assertEquals(healthy, report.isHealthy());
682         assertEquals(code, report.getCode());
683         assertEquals(message, report.getMessage());
684     }
685 }