Fetch and Delete policy API with PolicyName and PolicyVersion
[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-2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
6  *  Modifications Copyright (C) 2020 Bell Canada.
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.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertTrue;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.lang.reflect.Constructor;
36 import java.lang.reflect.Modifier;
37 import java.security.SecureRandom;
38 import java.util.Base64;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Properties;
43 import javax.net.ssl.SSLContext;
44 import javax.net.ssl.TrustManager;
45 import javax.ws.rs.client.Client;
46 import javax.ws.rs.client.ClientBuilder;
47 import javax.ws.rs.client.Entity;
48 import javax.ws.rs.client.Invocation;
49 import javax.ws.rs.client.WebTarget;
50 import javax.ws.rs.core.Response;
51 import javax.ws.rs.core.Response.Status;
52 import org.glassfish.jersey.client.ClientProperties;
53 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
54 import org.junit.AfterClass;
55 import org.junit.BeforeClass;
56 import org.junit.Test;
57 import org.onap.policy.api.main.parameters.ApiParameterGroup;
58 import org.onap.policy.api.main.parameters.CommonTestData;
59 import org.onap.policy.api.main.rest.provider.PolicyProvider;
60 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
61 import org.onap.policy.api.main.startstop.Main;
62 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
63 import org.onap.policy.common.endpoints.report.HealthCheckReport;
64 import org.onap.policy.common.gson.GsonMessageBodyHandler;
65 import org.onap.policy.common.parameters.ParameterService;
66 import org.onap.policy.common.utils.coder.StandardCoder;
67 import org.onap.policy.common.utils.coder.StandardYamlCoder;
68 import org.onap.policy.common.utils.network.NetworkUtil;
69 import org.onap.policy.common.utils.resources.ResourceUtils;
70 import org.onap.policy.common.utils.resources.TextFileUtils;
71 import org.onap.policy.models.base.PfModelException;
72 import org.onap.policy.models.errors.concepts.ErrorResponse;
73 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
74 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
75 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
76
77 /**
78  * Class to perform unit test of {@link ApiRestController}.
79  *
80  * @author Chenfei Gao (cgao@research.att.com)
81  */
82 public class TestApiRestServer {
83
84     private static final String ALIVE = "alive";
85     private static final String SELF = NetworkUtil.getHostname();
86     private static final String NAME = "Policy API";
87     private static final String APP_JSON = "application/json";
88     private static final String APP_YAML = "application/yaml";
89
90     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
91     private static final String STATISTICS_ENDPOINT = "statistics";
92
93     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
94     private static final String OP_POLICY_NAME_VDNS = "operational.scaleout";
95     private static final String OP_POLICY_NAME_VFW = "operational.modifyconfig";
96
97     private static final String POLICYTYPES = "policytypes";
98     private static final String POLICYTYPES_TCA = "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app";
99     private static final String POLICYTYPES_COLLECTOR =
100         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
101     private static final String POLICYTYPES_TCA_VERSION =
102         "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0";
103     private static final String POLICYTYPES_TCA_LATEST =
104         "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/latest";
105     private static final String POLICYTYPES_COLLECTOR_VERSION =
106         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/1.0.0";
107     private static final String POLICYTYPES_COLLECTOR_LATEST =
108         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/latest";
109
110     private static final String POLICYTYPES_DROOLS = "policytypes/onap.policies.controlloop.operational.common.Drools";
111     private static final String POLICYTYPES_DROOLS_VERSION = POLICYTYPES_DROOLS + "/versions/1.0.0";
112     private static final String POLICYTYPES_DROOLS_VERSION_LATEST = POLICYTYPES_DROOLS + "/versions/latest";
113
114     private static final String POLICYTYPES_NAMING_VERSION = POLICYTYPES + "/onap.policies.Naming/versions/1.0.0";
115
116     private static final String POLICYTYPES_TCA_POLICIES =
117         "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies";
118     private static final String POLICYTYPES_TCA_POLICIES_VCPE =
119         "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca";
120     private static final String POLICYTYPES_TCA_POLICIES_VCPE_VERSION1 = "policytypes/"
121         + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/1.0.0";
122     private static final String POLICYTYPES_TCA_POLICIES_VCPE_LATEST = "policytypes/"
123         + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/latest";
124
125     private static final String POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION =
126         POLICYTYPES_DROOLS_VERSION + "/policies/" + OP_POLICY_NAME_VCPE + "/versions/1.0.0";
127
128     private static final String OPS_POLICIES =
129         "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies";
130     private static final String OPS_POLICIES_VCPE_LATEST =
131         "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VCPE
132             + "/versions/latest";
133     private static final String OPS_POLICIES_VCPE_DEPLOYED =
134         "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VCPE
135             + "/versions/deployed";
136     private static final String OPS_POLICIES_VDNS_LATEST =
137         "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VDNS
138             + "/versions/latest";
139     private static final String OPS_POLICIES_VFIREWALL_LATEST =
140         "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VFW
141             + "/versions/latest";
142     private static final String OPS_POLICIES_VCPE_VERSION =
143         "policytypes/" + "onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VCPE
144             + "/versions/1";
145     private static final String OPS_POLICIES_VDNS_VERSION =
146         "policytypes/" + "onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VDNS
147             + "/versions/1";
148     private static final String OPS_POLICIES_VFIREWALL_VERSION =
149         "policytypes/" + "onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VFW
150             + "/versions/1";
151     private static final String OPS_POLICIES_VCPE_VERSION_NOEX =
152         "policytypes/" + "onap.policies.controlloop.Operational/versions/1.0.0/policies/" + OP_POLICY_NAME_VCPE
153             + "/versions/99";
154     private static final String POLICIES = "policies";
155
156     private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
157
158     // @formatter:off
159     private static final String[] TOSCA_POLICY_RESOURCE_NAMES = {"policies/vCPE.policy.monitoring.input.tosca.json",
160         "policies/vCPE.policy.monitoring.input.tosca.yaml", "policies/vDNS.policy.monitoring.input.tosca.json",
161         "policies/vDNS.policy.monitoring.input.tosca.v2.yaml"};
162
163     private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = {
164         "policies/vCPE.policies.optimization.input.tosca.json", "policies/vCPE.policies.optimization.input.tosca.yaml"};
165
166     private static final String TOSCA_POLICYTYPE_OP_RESOURCE =
167         "policytypes/onap.policies.controlloop.operational.Common.yaml";
168
169     private static final String LEGACY_POLICYTYPE_OP_RESOURCE =
170         "policytypes/onap.policies.controlloop.Operational.yaml";
171
172     private static final String[] TOSCA_POLICYTYPE_RESOURCE_NAMES = {
173         "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
174         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
175         "policytypes/onap.policies.Optimization.yaml", LEGACY_POLICYTYPE_OP_RESOURCE, TOSCA_POLICYTYPE_OP_RESOURCE,
176         "policytypes/onap.policies.controlloop.operational.common.Drools.yaml",
177         "policytypes/onap.policies.controlloop.guard.Common.yaml",
178         "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml",
179         "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml",
180         "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml",
181         "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
182         "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
183         "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
184         "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
185         "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
186         "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
187         "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
188         "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
189         "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
190         "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"};
191
192     private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON =
193         "policies/vCPE.policy.operational.input.tosca.json";
194
195     private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML =
196         "policies/vCPE.policy.operational.input.tosca.yaml";
197
198     private static final String[] LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES = {
199         "policies/vCPE.policy.operational.legacy.input.json", "policies/vDNS.policy.operational.legacy.input.json",
200         "policies/vFirewall.policy.operational.legacy.input.json"};
201
202     private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0";
203
204     private static PolicyModelsProviderParameters providerParams;
205     private static ApiParameterGroup apiParamGroup;
206     private static PolicyProvider policyProvider;
207     private static PolicyTypeProvider policyTypeProvider;
208
209     // @formatter:on
210
211     private static final StandardCoder standardCoder = new StandardCoder();
212
213     private static int apiPort;
214     private static Main apiMain;
215
216     private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
217
218     /**
219      * Initializes parameters and set up test environment.
220      *
221      * @throws PfModelException the PfModel parsing exception
222      * @throws IOException      on I/O exceptions
223      */
224     @BeforeClass
225     public static void setupParameters() throws PfModelException, IOException {
226         providerParams = new PolicyModelsProviderParameters();
227         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
228         providerParams.setDatabaseDriver("org.h2.Driver");
229         providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
230         providerParams.setDatabaseUser("policy");
231         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
232         providerParams.setPersistenceUnit("ToscaConceptTest");
233         apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList(),
234             Collections.emptyList());
235         ParameterService.register(apiParamGroup, true);
236
237         policyTypeProvider = new PolicyTypeProvider();
238         policyProvider = new PolicyProvider();
239
240         apiPort = NetworkUtil.allocPort();
241
242         final String[] apiConfigParameters = new String[2];
243         final Properties systemProps = System.getProperties();
244         systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
245         systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
246         System.setProperties(systemProps);
247         new CommonTestData().makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
248             "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json", apiPort);
249         apiConfigParameters[0] = "-c";
250         apiConfigParameters[1] = "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json";
251
252         apiMain = new Main(apiConfigParameters);
253     }
254
255     /**
256      * Method for cleanup after each test.
257      */
258     @AfterClass
259     public static void teardown() throws Exception {
260         policyTypeProvider.close();
261         policyProvider.close();
262
263         if (apiMain != null) {
264             apiMain.shutdown();
265         }
266     }
267
268     @Test
269     public void testApiStatisticsConstructorIsPrivate() {
270
271         try {
272             final Constructor<ApiStatisticsManager> constructor = ApiStatisticsManager.class.getDeclaredConstructor();
273             assertTrue(Modifier.isPrivate(constructor.getModifiers()));
274             constructor.setAccessible(true);
275             constructor.newInstance();
276
277         } catch (final Exception exp) {
278             assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
279         }
280     }
281
282     @Test
283     public void testCreatePolicyTypes() throws Exception {
284         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
285             Response rawResponse = createResource(POLICYTYPES, resrcName);
286             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
287             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
288             assertNotNull(response);
289             assertFalse(response.getPolicyTypes().isEmpty());
290         }
291
292         // Send a policy type with a null value to trigger an error
293         Response rawResponse = readResource(POLICYTYPES, APP_JSON);
294         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
295         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
296         String firstPolicyType = response.getPolicyTypes().keySet().iterator().next();
297         response.getPolicyTypes().put(firstPolicyType, null);
298         Response rawResponse2 = createResource(POLICYTYPES, standardCoder.encode(response));
299         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse2.getStatus());
300         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
301         assertEquals("no policy types specified on service template", errorResponse.getErrorMessage());
302     }
303
304     @Test
305     public void testCreatePolicies() throws Exception {
306         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
307             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
308             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
309         }
310
311         new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
312
313         // Send a policy with no policy type trigger an error
314         String toscaPolicy = ResourceUtils
315             .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
316
317         toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.cdap.tca.hi.lo.app", "IDontExist");
318         TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
319
320         Response rawResponse2 = createResource(POLICYTYPES_TCA_POLICIES,
321             "src/test/resources/policies/BadTestPolicy.yaml");
322         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
323         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
324         assertThat(errorResponse.getErrorMessage())
325             .contains("entity in incoming fragment does not equal existing entity");
326     }
327
328     @Test
329     public void testSimpleCreatePolicies() throws Exception {
330         for (String resrcName : TOSCA_POLICIES_RESOURCE_NAMES) {
331             Response rawResponse = createResource(POLICIES, resrcName);
332             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
333         }
334
335         new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
336
337         // Send a policy with no policy type trigger an error
338         String toscaPolicy = ResourceUtils
339             .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
340
341         toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.cdap.tca.hi.lo.app", "IDontExist");
342         toscaPolicy = toscaPolicy.replaceAll("onap.restart.tca", "onap.restart.tca.IDontExist");
343         TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
344
345         Response rawResponse2 = createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml");
346         ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
347         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
348         assertThat(errorResponse.getErrorMessage())
349             .contains("policy type IDontExist:1.0.0 referenced in policy not found");
350     }
351
352     @Test
353     public void testCreateOperationalPolicies() throws Exception {
354         for (String resrcName : LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES) {
355             Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName);
356             ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
357             assertEquals(null, errorResponse.getErrorDetails());
358             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
359         }
360
361         Response rawResponse = deleteResource(OPS_POLICIES + "/operational.restart/versions/1", APP_JSON);
362         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
363
364         rawResponse = deleteResource(OPS_POLICIES + "/operational.scaleout/versions/1", APP_JSON);
365         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
366
367         rawResponse = deleteResource(OPS_POLICIES + "/operational.modifyconfig/versions/1", APP_JSON);
368         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
369     }
370
371     @SuppressWarnings("unchecked")
372     @Test
373     public void testToscaCompliantOpDroolsPolicies() throws Exception {
374         Response rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_OP_RESOURCE);
375         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
376
377         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, APP_JSON);
378         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
379
380         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON);
381         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
382
383         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML);
384         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
385
386         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
387         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
388
389         rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
390         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
391
392         rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML);
393         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
394
395         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
396         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
397
398         rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_YAML);
399         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
400
401         ToscaServiceTemplate toscaVcpeSt = rawResponse.readEntity(ToscaServiceTemplate.class);
402         assertEquals(1, toscaVcpeSt.getToscaTopologyTemplate().getPolicies().size());
403         assertEquals(OP_POLICY_NAME_VCPE,
404             toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getName());
405
406         Map<String, Object> props = toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE)
407             .getProperties();
408         assertNotNull(props);
409
410         List<Object> operations = (List<Object>) props.get("operations");
411         assertEquals(1, operations.size());
412         assertEquals(props.get("trigger"), ((Map<String, Object>) operations.get(0)).get("id"));
413
414         Map<String, Object> operation = (Map<String, Object>) ((Map<String, Object>) operations.get(0))
415             .get("operation");
416         assertEquals("APPC", operation.get("actor"));
417         assertEquals("Restart", operation.get("operation"));
418
419         rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
420         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
421     }
422
423     @Test
424     public void testHealthCheckSuccessJson() throws Exception {
425         testHealthCheckSuccess(APP_JSON);
426     }
427
428     @Test
429     public void testHealthCheckSuccessYaml() throws Exception {
430         testHealthCheckSuccess(APP_YAML);
431     }
432
433     private void testHealthCheckSuccess(String mediaType) throws Exception {
434         final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT, mediaType);
435         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
436         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
437     }
438
439     @Test
440     public void testApiStatistics_200_Json() throws Exception {
441         testApiStatistics_200(APP_JSON);
442     }
443
444     @Test
445     public void testApiStatistics_200_Yaml() throws Exception {
446         testApiStatistics_200(APP_YAML);
447     }
448
449     private void testApiStatistics_200(String mediaType) throws Exception {
450         Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
451         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
452         validateStatisticsReport(report, 200);
453         updateApiStatistics();
454         invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
455         report = invocationBuilder.get(StatisticsReport.class);
456         validateStatisticsReport(report, 200);
457         ApiStatisticsManager.resetAllStatistics();
458     }
459
460     @Test
461     public void testReadPolicyTypesJson() throws Exception {
462         testReadPolicyTypes(APP_JSON);
463     }
464
465     @Test
466     public void testReadPolicyTypesYaml() throws Exception {
467         testReadPolicyTypes(APP_YAML);
468     }
469
470     private void testReadPolicyTypes(String mediaType) throws Exception {
471         Response rawResponse = readResource("policytypes/onap.policies.optimization.resource.HpaPolicy", mediaType);
472         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
473         ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
474         assertNotNull(namingServiceTemplate);
475         assertEquals(3, namingServiceTemplate.getPolicyTypesAsMap().size());
476         assertEquals(5, namingServiceTemplate.getDataTypesAsMap().size());
477
478         rawResponse = readResource(POLICYTYPES, mediaType);
479         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
480         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
481         assertFalse(response.getPolicyTypes().isEmpty());
482
483         rawResponse = readResource(POLICYTYPES_TCA, mediaType);
484         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
485
486         rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType);
487         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
488
489         rawResponse = readResource(POLICYTYPES_TCA_LATEST, mediaType);
490         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
491
492         rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType);
493         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
494
495         rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType);
496         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
497
498         rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType);
499         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
500
501         rawResponse = readResource(POLICYTYPES_DROOLS, mediaType);
502         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
503
504         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, mediaType);
505         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
506
507         rawResponse = readResource(POLICYTYPES_DROOLS_VERSION_LATEST, mediaType);
508         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
509
510         rawResponse = readResource(POLICYTYPES_NAMING_VERSION, mediaType);
511         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
512     }
513
514     @Test
515     public void testDeletePolicyTypeJson() throws Exception {
516         testDeletePolicyType(APP_JSON);
517     }
518
519     @Test
520     public void testDeletePolicyTypeYaml() throws Exception {
521         testDeletePolicyType(APP_YAML);
522     }
523
524     private void testDeletePolicyType(String mediaType) throws Exception {
525         Response rawResponse = deleteResource("policytypes/onap.policies.IDoNotExist/versions/1.0.0", mediaType);
526         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
527
528         rawResponse = createResource(POLICYTYPES, "policytypes/onap.policies.Test.yaml");
529         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
530
531         rawResponse = readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType);
532         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
533
534         rawResponse = deleteResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType);
535         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
536
537         rawResponse = readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType);
538         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
539     }
540
541     @Test
542     public void testReadPoliciesJson() throws Exception {
543         testReadPolicies(APP_JSON);
544     }
545
546     @Test
547     public void testReadPoliciesYaml() throws Exception {
548         testReadPolicies(APP_YAML);
549     }
550
551     private void testReadPolicies(String mediaType) throws Exception {
552         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
553             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
554             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
555         }
556
557         Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType);
558         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
559
560         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType);
561         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
562
563         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
564         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
565
566         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType);
567         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
568
569         rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
570         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
571
572     }
573
574     @Test
575     public void testNamingPolicyGet() throws Exception {
576
577         Response rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
578             + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON);
579         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
580
581         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
582             + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0?mode=referenced", APP_JSON);
583         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
584
585         ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
586         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
587         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
588         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
589
590         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
591             + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest?mode=referenced", APP_JSON);
592         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
593
594         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
595         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
596         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
597         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
598
599         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies?mode=referenced",
600             APP_JSON);
601         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
602
603         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
604         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
605         assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
606         assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
607
608         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
609             + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON);
610         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
611
612         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
613
614         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
615         assertNull(namingServiceTemplate.getPolicyTypes());
616         assertNull(namingServiceTemplate.getDataTypes());
617
618         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
619             + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest", APP_JSON);
620         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
621
622         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
623         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
624         assertNull(namingServiceTemplate.getPolicyTypes());
625         assertNull(namingServiceTemplate.getDataTypes());
626
627         rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies", APP_JSON);
628         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
629
630         namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
631         assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
632         assertNull(namingServiceTemplate.getPolicyTypes());
633         assertNull(namingServiceTemplate.getDataTypes());
634     }
635
636     @Test
637     public void testDeletePoliciesJson() throws Exception {
638         testDeletePolicies(APP_JSON);
639     }
640
641     @Test
642     public void testDeletePoliciesYaml() throws Exception {
643         testDeletePolicies(APP_YAML);
644     }
645
646     private void testDeletePolicies(String mediaType) throws Exception {
647         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
648         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
649         ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
650         assertEquals("policy onap.restart.tca:1.0.0 not found", error.getErrorMessage());
651     }
652
653     @Test
654     public void testDeletePolicyVersionJson() throws Exception {
655         testDeletePolicyVersion(APP_JSON);
656     }
657
658     @Test
659     public void testDeletePolicyVersionYaml() throws Exception {
660         testDeletePolicyVersion(APP_YAML);
661     }
662
663     private void testDeletePolicyVersion(String mediaType) throws Exception {
664         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
665             Response rawResponse = createResource(POLICYTYPES, resrcName);
666             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
667             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
668             assertNotNull(response);
669             assertFalse(response.getPolicyTypes().isEmpty());
670         }
671         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
672             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
673             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
674         }
675         Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
676         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
677
678         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
679         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
680         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
681         assertEquals("policies for onap.restart.tca:1.0.0 do not exist", errorResponse.getErrorMessage());
682
683         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType);
684         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
685         errorResponse = rawResponse.readEntity(ErrorResponse.class);
686         assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
687
688         rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType);
689         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
690         errorResponse = rawResponse.readEntity(ErrorResponse.class);
691         assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
692     }
693
694     @Test
695     public void testGetAllVersionOfPolicyJson() throws Exception {
696         testGetAllVersionOfPolicy(APP_JSON);
697     }
698
699     @Test
700     public void testGetAllVersionOfPolicyYaml() throws Exception {
701         testGetAllVersionOfPolicy(APP_YAML);
702     }
703
704     private void testGetAllVersionOfPolicy(String mediaType) throws Exception {
705         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
706             Response rawResponse = createResource(POLICYTYPES, resrcName);
707             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
708             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
709             assertNotNull(response);
710             assertFalse(response.getPolicyTypes().isEmpty());
711         }
712         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
713             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
714             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
715         }
716         Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType);
717         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
718     }
719
720     @Test
721     public void testReadOperationalPoliciesJson() throws Exception {
722         testReadOperationalPolicies(APP_JSON);
723     }
724
725     @Test
726     public void testReadOperationalPoliciesYaml() throws Exception {
727         testReadOperationalPolicies(APP_YAML);
728     }
729
730     private void testReadOperationalPolicies(String mediaType) throws Exception {
731         for (String resrcName : LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES) {
732             Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName);
733             ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
734             assertEquals(null, error.getErrorDetails());
735             assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
736         }
737
738         Response rawResponse = readResource(OPS_POLICIES_VCPE_LATEST, mediaType);
739         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
740
741         rawResponse = readResource(OPS_POLICIES_VCPE_VERSION, mediaType);
742         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
743
744         rawResponse = readResource(OPS_POLICIES_VDNS_LATEST, mediaType);
745         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
746
747         rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, mediaType);
748         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
749
750         rawResponse = readResource(OPS_POLICIES_VFIREWALL_LATEST, mediaType);
751         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
752
753         rawResponse = readResource(OPS_POLICIES_VFIREWALL_VERSION, mediaType);
754         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
755
756         rawResponse = deleteResource(OPS_POLICIES + "/operational.restart/versions/1", APP_JSON);
757         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
758
759         rawResponse = deleteResource(OPS_POLICIES + "/operational.scaleout/versions/1", APP_JSON);
760         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
761
762         rawResponse = deleteResource(OPS_POLICIES + "/operational.modifyconfig/versions/1", APP_JSON);
763         assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
764
765     }
766
767     @Test
768     public void testDeleteOperationalPolicyJson() throws Exception {
769         testDeleteOperationalPolicy(APP_JSON);
770     }
771
772     @Test
773     public void testDeleteOperationalPolicyYaml() throws Exception {
774         testDeleteOperationalPolicy(APP_YAML);
775     }
776
777     private void testDeleteOperationalPolicy(String mediaType) throws Exception {
778         Response rawResponse = deleteResource(OPS_POLICIES_VCPE_VERSION_NOEX, mediaType);
779         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
780         ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
781         assertEquals("policy operational.restart:99.0.0 not found", error.getErrorMessage());
782     }
783
784     @Test
785     public void testGetLatestVersionOfOperationalPolicyJson() throws Exception {
786         testGetLatestVersionOfOperationalPolicy(APP_JSON);
787     }
788
789     @Test
790     public void testGetLatestVersionOfOperationalPolicyYaml() throws Exception {
791         testGetLatestVersionOfOperationalPolicy(APP_YAML);
792     }
793
794     private void testGetLatestVersionOfOperationalPolicy(String mediaType) throws Exception {
795         Response rawResponse = readResource(OPS_POLICIES_VDNS_LATEST, mediaType);
796         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
797         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
798         assertEquals("policies for operational.scaleout:null do not exist", errorResponse.getErrorMessage());
799     }
800
801     @Test
802     public void testGetSpecificVersionOfOperationalPolicyJson() throws Exception {
803         testGetSpecificVersionOfOperationalPolicy(APP_JSON);
804     }
805
806     @Test
807     public void testGetSpecificVersionOfOperationalPolicyYaml() throws Exception {
808         testGetSpecificVersionOfOperationalPolicy(APP_YAML);
809     }
810
811     private void testGetSpecificVersionOfOperationalPolicy(String mediaType) throws Exception {
812         Response rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, mediaType);
813         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
814         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
815         assertEquals("policies for operational.scaleout:1.0.0 do not exist", errorResponse.getErrorMessage());
816     }
817
818     @Test
819     public void testGetDeployedVersionsOfOperationalPolicyJson() throws Exception {
820         testGetDeployedVersionsOfOperationalPolicy(APP_JSON);
821     }
822
823     @Test
824     public void testGetDeployedVersionsOfOperationalPolicyYaml() throws Exception {
825         testGetDeployedVersionsOfOperationalPolicy(APP_YAML);
826     }
827
828     private void testGetDeployedVersionsOfOperationalPolicy(String mediaType) throws Exception {
829         Response rawResponse = readResource(OPS_POLICIES_VCPE_DEPLOYED, mediaType);
830         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
831         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
832         assertEquals("could not find policy with ID " + OP_POLICY_NAME_VCPE + " and type "
833             + "onap.policies.controlloop.Operational:1.0.0 deployed in any pdp group", errorResponse.getErrorMessage());
834     }
835
836     @Test
837     public void testDeleteSpecificVersionOfOperationalPolicy() throws Exception {
838         Response rawResponse = deleteResource(OPS_POLICIES_VDNS_VERSION, APP_YAML);
839         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
840         ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
841         assertEquals("policy operational.scaleout:1.0.0 not found", errorResponse.getErrorMessage());
842     }
843
844     @Test
845     public void testGetPoliciesJson() throws Exception {
846         getPolicies(APP_JSON);
847     }
848
849     @Test
850     public void testGetPoliciesYaml() throws Exception {
851         getPolicies(APP_YAML);
852     }
853
854     private void getPolicies(String mediaType) throws Exception {
855         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
856             Response rawResponse = createResource(POLICYTYPES, resrcName);
857             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
858             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
859             assertThat(response).isNotNull();
860             assertThat(response.getPolicyTypes()).isNotEmpty();
861         }
862         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
863             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
864             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
865         }
866         Response rawResponse = readResource(POLICIES, mediaType);
867         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
868         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
869         assertThat(response.getToscaTopologyTemplate().getPolicies()).isNotEmpty();
870     }
871
872     @Test
873     public void testGetSpecificPolicyJson() throws Exception {
874         getSpecificPolicy(APP_JSON);
875     }
876
877     @Test
878     public void testGetSpecificPolicyYaml() throws Exception {
879         getSpecificPolicy(APP_YAML);
880     }
881
882     private void getSpecificPolicy(String mediaType) throws Exception {
883         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
884             Response rawResponse = createResource(POLICYTYPES, resrcName);
885             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
886             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
887             assertThat(response).isNotNull();
888             assertThat(response.getPolicyTypes()).isNotEmpty();
889         }
890         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
891             Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
892             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
893         }
894         Response rawResponse = readResource(POLICIES_VCPE_VERSION1, mediaType);
895         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
896         ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
897         assertThat(response.getToscaTopologyTemplate().getPolicies()).hasSize(1);
898     }
899
900     @Test
901     public void testDeleteSpecificPolicy() throws Exception {
902         Response rawResponse;
903         for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
904             rawResponse = createResource(POLICYTYPES, resrcName);
905             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
906             ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
907             assertThat(response).isNotNull();
908             assertThat(response.getPolicyTypes()).isNotEmpty();
909         }
910         for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
911             rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
912             assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
913         }
914
915         rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON);
916         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
917
918         // delete a particular policy
919         rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON);
920         assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
921
922         rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON);
923         assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
924
925         rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON);
926         assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
927
928     }
929
930
931     private Response createResource(String endpoint, String resourceName) throws Exception {
932
933         String mediaType = APP_JSON; // default media type
934         ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
935         if (resourceName.endsWith(".json")) {
936             rawServiceTemplate = standardCoder
937                 .decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
938         } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
939             mediaType = APP_YAML;
940             rawServiceTemplate = standardYamlCoder
941                 .decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
942         }
943
944         final Invocation.Builder invocationBuilder;
945
946         invocationBuilder = sendHttpsRequest(endpoint, mediaType);
947
948         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
949         return invocationBuilder.post(entity);
950     }
951
952     private Response createOperationalPolicy(String endpoint, String resourceName) throws Exception {
953
954         String mediaType = APP_JSON; // default media type
955         LegacyOperationalPolicy rawOpsPolicy = new LegacyOperationalPolicy();
956         if (resourceName.endsWith(".json")) {
957             rawOpsPolicy = standardCoder
958                 .decode(ResourceUtils.getResourceAsString(resourceName), LegacyOperationalPolicy.class);
959         } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
960             mediaType = APP_YAML;
961             rawOpsPolicy = standardYamlCoder
962                 .decode(ResourceUtils.getResourceAsString(resourceName), LegacyOperationalPolicy.class);
963         }
964
965         final Invocation.Builder invocationBuilder;
966
967         invocationBuilder = sendHttpsRequest(endpoint, mediaType);
968
969         Entity<LegacyOperationalPolicy> entity = Entity.entity(rawOpsPolicy, mediaType);
970         return invocationBuilder.post(entity);
971     }
972
973     private Response readResource(String endpoint, String mediaType) throws Exception {
974
975         final Invocation.Builder invocationBuilder;
976
977         invocationBuilder = sendHttpsRequest(endpoint, mediaType);
978
979         return invocationBuilder.get();
980
981     }
982
983     private Response deleteResource(String endpoint, String mediaType) throws Exception {
984
985         final Invocation.Builder invocationBuilder;
986
987         invocationBuilder = sendHttpsRequest(endpoint, mediaType);
988
989         return invocationBuilder.delete();
990     }
991
992     private Invocation.Builder sendHttpsRequest(final String endpoint, String mediaType) throws Exception {
993
994         final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
995
996         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
997         sc.init(null, noopTrustManager, new SecureRandom());
998         final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc)
999             .hostnameVerifier((host, session) -> true);
1000         final Client client = clientBuilder.build();
1001         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
1002         client.register(feature);
1003
1004         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
1005         if (APP_JSON.equalsIgnoreCase(mediaType)) {
1006             client.register(GsonMessageBodyHandler.class);
1007         } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
1008             client.register(YamlMessageBodyHandler.class);
1009         }
1010
1011         final WebTarget webTarget = client.target("https://localhost:" + apiPort + "/policy/api/v1/" + endpoint);
1012
1013         final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
1014
1015         if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
1016             throw new IllegalStateException("cannot connect to port " + apiPort);
1017         }
1018         return invocationBuilder;
1019     }
1020
1021     private void updateApiStatistics() {
1022
1023         ApiStatisticsManager.updateTotalApiCallCount();
1024         ApiStatisticsManager.updateApiCallSuccessCount();
1025         ApiStatisticsManager.updateApiCallFailureCount();
1026         ApiStatisticsManager.updateTotalPolicyGetCount();
1027         ApiStatisticsManager.updateTotalPolicyPostCount();
1028         ApiStatisticsManager.updateTotalPolicyTypeGetCount();
1029         ApiStatisticsManager.updateTotalPolicyTypePostCount();
1030         ApiStatisticsManager.updatePolicyGetSuccessCount();
1031         ApiStatisticsManager.updatePolicyGetFailureCount();
1032         ApiStatisticsManager.updatePolicyPostSuccessCount();
1033         ApiStatisticsManager.updatePolicyPostFailureCount();
1034         ApiStatisticsManager.updatePolicyTypeGetSuccessCount();
1035         ApiStatisticsManager.updatePolicyTypeGetFailureCount();
1036         ApiStatisticsManager.updatePolicyTypePostSuccessCount();
1037         ApiStatisticsManager.updatePolicyTypePostFailureCount();
1038     }
1039
1040     private void validateStatisticsReport(final StatisticsReport report, final int code) {
1041
1042         assertEquals(code, report.getCode());
1043     }
1044
1045     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
1046         final String message, final HealthCheckReport report) {
1047
1048         assertEquals(name, report.getName());
1049         assertEquals(url, report.getUrl());
1050         assertEquals(healthy, report.isHealthy());
1051         assertEquals(code, report.getCode());
1052         assertEquals(message, report.getMessage());
1053     }
1054 }