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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.rest;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
32 import jakarta.ws.rs.client.Invocation;
33 import jakarta.ws.rs.core.Response;
34 import jakarta.ws.rs.core.Response.Status;
36 import java.io.IOException;
37 import java.util.List;
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;
60 * Class to perform unit test of {@link ApiRestController}.
62 * @author Chenfei Gao (cgao@research.att.com)
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 {
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";
75 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
77 private static final String OP_POLICY_NAME_VCPE = "operational.restart";
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";
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";
94 private static final String POLICYTYPES_NAMING_VERSION = POLICYTYPES + "/onap.policies.Naming/versions/1.0.0";
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";
105 private static final String POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION =
106 POLICYTYPES_DROOLS_VERSION + "/policies/" + OP_POLICY_NAME_VCPE + "/versions/1.0.0";
108 private static final String POLICIES = "policies";
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"};
115 private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = {
116 "policies/vCPE.policies.optimization.input.tosca.json", "policies/vCPE.policies.optimization.input.tosca.yaml"};
118 private static final String TOSCA_POLICYTYPE_OP_RESOURCE =
119 "policytypes/onap.policies.controlloop.operational.Common.yaml";
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"};
140 private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON =
141 "policies/vCPE.policy.operational.input.tosca.json";
143 private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML =
144 "policies/vCPE.policy.operational.input.tosca.yaml";
146 private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0";
149 private static final StandardCoder standardCoder = new StandardCoder();
150 private static SelfSignedKeyStore keystore;
156 * Initializes parameters and set up test environment.
158 * @throws IOException on I/O exceptions
159 * @throws InterruptedException if interrupted
162 static void setupParameters() throws IOException, InterruptedException {
163 keystore = new SelfSignedKeyStore();
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);
177 void testSwagger() throws Exception {
178 super.testSwagger(apiPort);
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());
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());
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());
210 new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
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]);
216 toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
217 TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
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");
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());
235 new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
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]);
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");
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");
253 @SuppressWarnings("unchecked")
255 void testToscaCompliantOpDroolsPolicies() throws Exception {
256 Response rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_OP_RESOURCE, apiPort);
257 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
259 rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, APP_JSON, apiPort);
260 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
262 rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON, apiPort);
263 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
265 rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML, apiPort);
266 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
268 rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
269 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
271 rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
272 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
274 rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML, apiPort);
275 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
277 rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
278 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
280 rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_YAML, apiPort);
281 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
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());
288 Map<String, Object> props =
289 toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getProperties();
290 assertNotNull(props);
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"));
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"));
301 rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON, apiPort);
302 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
306 void testHealthCheckSuccessJson() throws Exception {
307 testHealthCheckSuccess(APP_JSON);
311 void testHealthCheckSuccessYaml() throws Exception {
312 testHealthCheckSuccess(APP_YAML);
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);
323 void testReadPolicyTypesJson() throws Exception {
324 testReadPolicyTypes(APP_JSON);
328 void testReadPolicyTypesYaml() throws Exception {
329 testReadPolicyTypes(APP_YAML);
332 private void testReadPolicyTypes(String mediaType) throws Exception {
333 Response rawResponse =
334 readResource("policytypes/onap.policies.optimization.resource.HpaPolicy", mediaType,
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());
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());
347 rawResponse = readResource(POLICYTYPES_TCA, mediaType, apiPort);
348 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
350 rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType, apiPort);
351 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
353 rawResponse = readResource(POLICYTYPES_TCA_LATEST, mediaType, apiPort);
354 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
356 rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType, apiPort);
357 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
359 rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType, apiPort);
360 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
362 rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType, apiPort);
363 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
365 rawResponse = readResource(POLICYTYPES_DROOLS, mediaType, apiPort);
366 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
368 rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, mediaType, apiPort);
369 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
371 rawResponse = readResource(POLICYTYPES_DROOLS_VERSION_LATEST, mediaType, apiPort);
372 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
374 rawResponse = readResource(POLICYTYPES_NAMING_VERSION, mediaType, apiPort);
375 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
379 void testDeletePolicyTypeJson() throws Exception {
380 testDeletePolicyType(APP_JSON);
384 void testDeletePolicyTypeYaml() throws Exception {
385 testDeletePolicyType(APP_YAML);
388 private void testDeletePolicyType(String mediaType) throws Exception {
389 Response rawResponse = deleteResource("policytypes/onap.policies.IDoNotExist/versions/1.0.0",
391 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
393 rawResponse = createResource(POLICYTYPES, "policytypes/onap.policies.Test.yaml", apiPort);
394 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
397 readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
398 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
401 deleteResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
402 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
405 readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType, apiPort);
406 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
410 void testReadPoliciesJson() throws Exception {
411 testReadPolicies(APP_JSON);
415 void testReadPoliciesYaml() throws Exception {
416 testReadPolicies(APP_YAML);
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());
425 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType, apiPort);
426 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
428 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType, apiPort);
429 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
431 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
432 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
434 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType, apiPort);
435 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
437 rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
438 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
443 void testNamingPolicyGet() throws Exception {
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());
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());
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());
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());
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());
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());
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());
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());
480 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
482 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
483 assertNull(namingServiceTemplate.getPolicyTypes());
484 assertNull(namingServiceTemplate.getDataTypes());
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());
490 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
491 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
492 assertNull(namingServiceTemplate.getPolicyTypes());
493 assertNull(namingServiceTemplate.getDataTypes());
496 readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies", APP_JSON,
498 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
500 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
501 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
502 assertNull(namingServiceTemplate.getPolicyTypes());
503 assertNull(namingServiceTemplate.getDataTypes());
507 void testDeletePoliciesJson() throws Exception {
508 testDeletePolicies(APP_JSON);
512 void testDeletePoliciesYaml() throws Exception {
513 testDeletePolicies(APP_YAML);
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());
524 void testDeletePolicyVersionJson() throws Exception {
525 testDeletePolicyVersion(APP_JSON);
529 void testDeletePolicyVersionYaml() throws Exception {
530 testDeletePolicyVersion(APP_YAML);
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());
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());
545 Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType, apiPort);
546 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
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());
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());
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());
565 void testGetAllVersionOfPolicyJson() throws Exception {
566 testGetAllVersionOfPolicy(APP_JSON);
570 void testGetAllVersionOfPolicyYaml() throws Exception {
571 testGetAllVersionOfPolicy(APP_YAML);
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());
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());
586 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType, apiPort);
587 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
591 void testGetPoliciesJson() throws Exception {
592 getPolicies(APP_JSON);
596 void testGetPoliciesYaml() throws Exception {
597 getPolicies(APP_YAML);
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();
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());
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();
619 void testGetSpecificPolicyJson() throws Exception {
620 getSpecificPolicy(APP_JSON);
624 void testGetSpecificPolicyYaml() throws Exception {
625 getSpecificPolicy(APP_YAML);
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();
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());
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);
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();
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());
661 rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
662 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
664 // delete a particular policy
665 rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
666 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
668 rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
669 assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
671 rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON, apiPort);
672 assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
676 private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
677 final String message, final HealthCheckReport report) {
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());