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
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;
31 import static org.junit.Assert.assertTrue;
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.Collections;
39 import java.util.List;
41 import java.util.Properties;
42 import javax.net.ssl.SSLContext;
43 import javax.net.ssl.TrustManager;
44 import javax.ws.rs.client.Client;
45 import javax.ws.rs.client.ClientBuilder;
46 import javax.ws.rs.client.Entity;
47 import javax.ws.rs.client.Invocation;
48 import javax.ws.rs.client.WebTarget;
49 import javax.ws.rs.core.Response;
50 import javax.ws.rs.core.Response.Status;
51 import org.glassfish.jersey.client.ClientProperties;
52 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
53 import org.junit.AfterClass;
54 import org.junit.BeforeClass;
55 import org.junit.Test;
56 import org.onap.policy.api.main.parameters.ApiParameterGroup;
57 import org.onap.policy.api.main.parameters.CommonTestData;
58 import org.onap.policy.api.main.rest.provider.PolicyProvider;
59 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
60 import org.onap.policy.api.main.startstop.Main;
61 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
62 import org.onap.policy.common.endpoints.report.HealthCheckReport;
63 import org.onap.policy.common.gson.GsonMessageBodyHandler;
64 import org.onap.policy.common.parameters.ParameterService;
65 import org.onap.policy.common.utils.coder.StandardCoder;
66 import org.onap.policy.common.utils.coder.StandardYamlCoder;
67 import org.onap.policy.common.utils.network.NetworkUtil;
68 import org.onap.policy.common.utils.resources.ResourceUtils;
69 import org.onap.policy.common.utils.resources.TextFileUtils;
70 import org.onap.policy.models.base.PfModelException;
71 import org.onap.policy.models.errors.concepts.ErrorResponse;
72 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
73 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
76 * Class to perform unit test of {@link ApiRestController}.
78 * @author Chenfei Gao (cgao@research.att.com)
80 public class TestApiRestServer {
82 private static final String ALIVE = "alive";
83 private static final String SELF = NetworkUtil.getHostname();
84 private static final String NAME = "Policy API";
85 private static final String APP_JSON = "application/json";
86 private static final String APP_YAML = "application/yaml";
88 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
89 private static final String STATISTICS_ENDPOINT = "statistics";
91 private static final String OP_POLICY_NAME_VCPE = "operational.restart";
93 private static final String POLICYTYPES = "policytypes";
94 private static final String POLICYTYPES_TCA = "policytypes/onap.policies.monitoring.tcagen2";
95 private static final String POLICYTYPES_COLLECTOR =
96 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
97 private static final String POLICYTYPES_TCA_VERSION =
98 "policytypes/onap.policies.monitoring.tcagen2/versions/1.0.0";
99 private static final String POLICYTYPES_TCA_LATEST =
100 "policytypes/onap.policies.monitoring.tcagen2/versions/latest";
101 private static final String POLICYTYPES_COLLECTOR_VERSION =
102 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/1.0.0";
103 private static final String POLICYTYPES_COLLECTOR_LATEST =
104 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/latest";
106 private static final String POLICYTYPES_DROOLS = "policytypes/onap.policies.controlloop.operational.common.Drools";
107 private static final String POLICYTYPES_DROOLS_VERSION = POLICYTYPES_DROOLS + "/versions/1.0.0";
108 private static final String POLICYTYPES_DROOLS_VERSION_LATEST = POLICYTYPES_DROOLS + "/versions/latest";
110 private static final String POLICYTYPES_NAMING_VERSION = POLICYTYPES + "/onap.policies.Naming/versions/1.0.0";
112 private static final String POLICYTYPES_TCA_POLICIES =
113 "policytypes/onap.policies.monitoring.tcagen2/versions/1.0.0/policies";
114 private static final String POLICYTYPES_TCA_POLICIES_VCPE =
115 "policytypes/onap.policies.monitoring.tcagen2/versions/1.0.0/policies/onap.restart.tca";
116 private static final String POLICYTYPES_TCA_POLICIES_VCPE_VERSION1 = "policytypes/"
117 + "onap.policies.monitoring.tcagen2/versions/1.0.0/policies/onap.restart.tca/versions/1.0.0";
118 private static final String POLICYTYPES_TCA_POLICIES_VCPE_LATEST = "policytypes/"
119 + "onap.policies.monitoring.tcagen2/versions/1.0.0/policies/onap.restart.tca/versions/latest";
121 private static final String POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION =
122 POLICYTYPES_DROOLS_VERSION + "/policies/" + OP_POLICY_NAME_VCPE + "/versions/1.0.0";
124 private static final String POLICIES = "policies";
126 private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
129 private static final String[] TOSCA_POLICY_RESOURCE_NAMES = {"policies/vCPE.policy.monitoring.input.tosca.json",
130 "policies/vCPE.policy.monitoring.input.tosca.yaml", "policies/vDNS.policy.monitoring.input.tosca.json",
131 "policies/vDNS.policy.monitoring.input.tosca.v2.yaml"};
133 private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = {
134 "policies/vCPE.policies.optimization.input.tosca.json", "policies/vCPE.policies.optimization.input.tosca.yaml"};
136 private static final String TOSCA_POLICYTYPE_OP_RESOURCE =
137 "policytypes/onap.policies.controlloop.operational.Common.yaml";
139 private static final String[] TOSCA_POLICYTYPE_RESOURCE_NAMES = {
140 "policytypes/onap.policies.monitoring.tcagen2.yaml",
141 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
142 "policytypes/onap.policies.controlloop.operational.common.Drools.yaml",
143 "policytypes/onap.policies.controlloop.guard.Common.yaml",
144 "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml",
145 "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml",
146 "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml",
147 "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
148 "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
149 "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
150 "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
151 "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
152 "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
153 "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
154 "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
155 "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
156 "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"};
158 private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON =
159 "policies/vCPE.policy.operational.input.tosca.json";
161 private static final String TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML =
162 "policies/vCPE.policy.operational.input.tosca.yaml";
164 private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0";
166 private static PolicyModelsProviderParameters providerParams;
167 private static ApiParameterGroup apiParamGroup;
168 private static PolicyProvider policyProvider;
169 private static PolicyTypeProvider policyTypeProvider;
173 private static final StandardCoder standardCoder = new StandardCoder();
175 private static int apiPort;
176 private static Main apiMain;
178 private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
181 * Initializes parameters and set up test environment.
183 * @throws PfModelException the PfModel parsing exception
184 * @throws IOException on I/O exceptions
187 public static void setupParameters() throws PfModelException, IOException {
188 providerParams = new PolicyModelsProviderParameters();
189 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
190 providerParams.setDatabaseDriver("org.h2.Driver");
191 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
192 providerParams.setDatabaseUser("policy");
193 providerParams.setDatabasePassword("P01icY");
194 providerParams.setPersistenceUnit("ToscaConceptTest");
195 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList(),
196 Collections.emptyList());
197 ParameterService.register(apiParamGroup, true);
199 policyTypeProvider = new PolicyTypeProvider();
200 policyProvider = new PolicyProvider();
202 apiPort = NetworkUtil.allocPort();
204 final String[] apiConfigParameters = new String[2];
205 final Properties systemProps = System.getProperties();
206 systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
207 systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
208 System.setProperties(systemProps);
209 new CommonTestData().makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
210 "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json", apiPort);
211 apiConfigParameters[0] = "-c";
212 apiConfigParameters[1] = "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json";
214 apiMain = new Main(apiConfigParameters);
218 * Method for cleanup after each test.
221 public static void teardown() throws Exception {
222 policyTypeProvider.close();
223 policyProvider.close();
225 if (apiMain != null) {
231 public void testApiStatisticsConstructorIsPrivate() {
234 final Constructor<ApiStatisticsManager> constructor = ApiStatisticsManager.class.getDeclaredConstructor();
235 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
236 constructor.setAccessible(true);
237 constructor.newInstance();
239 } catch (final Exception exp) {
240 assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
245 public void testCreatePolicyTypes() throws Exception {
246 for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
247 Response rawResponse = createResource(POLICYTYPES, resrcName);
248 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
249 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
250 assertNotNull(response);
251 assertFalse(response.getPolicyTypes().isEmpty());
254 // Send a policy type with a null value to trigger an error
255 Response rawResponse = readResource(POLICYTYPES, APP_JSON);
256 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
257 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
258 String firstPolicyType = response.getPolicyTypes().keySet().iterator().next();
259 response.getPolicyTypes().put(firstPolicyType, null);
260 Response rawResponse2 = createResource(POLICYTYPES, standardCoder.encode(response));
261 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse2.getStatus());
262 ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
263 assertEquals("no policy types specified on service template", errorResponse.getErrorMessage());
267 public void testCreatePolicies() throws Exception {
268 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
269 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
270 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
273 new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
275 // Send a policy with no policy type trigger an error
276 String toscaPolicy = ResourceUtils
277 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
279 toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
280 TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
282 Response rawResponse2 = createResource(POLICYTYPES_TCA_POLICIES,
283 "src/test/resources/policies/BadTestPolicy.yaml");
284 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
285 ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
286 assertThat(errorResponse.getErrorMessage())
287 .contains("entity in incoming fragment does not equal existing entity");
291 public void testSimpleCreatePolicies() throws Exception {
292 for (String resrcName : TOSCA_POLICIES_RESOURCE_NAMES) {
293 Response rawResponse = createResource(POLICIES, resrcName);
294 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
297 new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
299 // Send a policy with no policy type trigger an error
300 String toscaPolicy = ResourceUtils
301 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
303 toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.tcagen2", "IDontExist");
304 toscaPolicy = toscaPolicy.replaceAll("onap.restart.tca", "onap.restart.tca.IDontExist");
305 TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
307 Response rawResponse2 = createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml");
308 ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
309 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), rawResponse2.getStatus());
310 assertThat(errorResponse.getErrorMessage())
311 .contains("policy type IDontExist:1.0.0 referenced in policy not found");
314 @SuppressWarnings("unchecked")
316 public void testToscaCompliantOpDroolsPolicies() throws Exception {
317 Response rawResponse = createResource(POLICYTYPES, TOSCA_POLICYTYPE_OP_RESOURCE);
318 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
320 rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, APP_JSON);
321 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
323 rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_JSON);
324 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
326 rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML);
327 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
329 rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
330 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
332 rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
333 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
335 rawResponse = createResource(POLICIES, TOSCA_POLICY_OP_DROOLS_VCPE_RESOURSE_YAML);
336 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
338 rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
339 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
341 rawResponse = readResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_YAML);
342 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
344 ToscaServiceTemplate toscaVcpeSt = rawResponse.readEntity(ToscaServiceTemplate.class);
345 assertEquals(1, toscaVcpeSt.getToscaTopologyTemplate().getPolicies().size());
346 assertEquals(OP_POLICY_NAME_VCPE,
347 toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE).getName());
349 Map<String, Object> props = toscaVcpeSt.getToscaTopologyTemplate().getPolicies().get(0).get(OP_POLICY_NAME_VCPE)
351 assertNotNull(props);
353 List<Object> operations = (List<Object>) props.get("operations");
354 assertEquals(1, operations.size());
355 assertEquals(props.get("trigger"), ((Map<String, Object>) operations.get(0)).get("id"));
357 Map<String, Object> operation = (Map<String, Object>) ((Map<String, Object>) operations.get(0))
359 assertEquals("APPC", operation.get("actor"));
360 assertEquals("Restart", operation.get("operation"));
362 rawResponse = deleteResource(POLICYTYPES_DROOLS_POLICIES_VCPE_VERSION, APP_JSON);
363 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
367 public void testHealthCheckSuccessJson() throws Exception {
368 testHealthCheckSuccess(APP_JSON);
372 public void testHealthCheckSuccessYaml() throws Exception {
373 testHealthCheckSuccess(APP_YAML);
376 private void testHealthCheckSuccess(String mediaType) throws Exception {
377 final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT, mediaType);
378 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
379 validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
383 public void testApiStatistics_200_Json() throws Exception {
384 testApiStatistics_200(APP_JSON);
388 public void testApiStatistics_200_Yaml() throws Exception {
389 testApiStatistics_200(APP_YAML);
392 private void testApiStatistics_200(String mediaType) throws Exception {
393 Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
394 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
395 validateStatisticsReport(report, 200);
396 updateApiStatistics();
397 invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
398 report = invocationBuilder.get(StatisticsReport.class);
399 validateStatisticsReport(report, 200);
400 ApiStatisticsManager.resetAllStatistics();
404 public void testReadPolicyTypesJson() throws Exception {
405 testReadPolicyTypes(APP_JSON);
409 public void testReadPolicyTypesYaml() throws Exception {
410 testReadPolicyTypes(APP_YAML);
413 private void testReadPolicyTypes(String mediaType) throws Exception {
414 Response rawResponse = readResource("policytypes/onap.policies.optimization.resource.HpaPolicy", mediaType);
415 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
416 ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
417 assertNotNull(namingServiceTemplate);
418 assertEquals(3, namingServiceTemplate.getPolicyTypesAsMap().size());
419 assertEquals(5, namingServiceTemplate.getDataTypesAsMap().size());
421 rawResponse = readResource(POLICYTYPES, mediaType);
422 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
423 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
424 assertFalse(response.getPolicyTypes().isEmpty());
426 rawResponse = readResource(POLICYTYPES_TCA, mediaType);
427 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
429 rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType);
430 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
432 rawResponse = readResource(POLICYTYPES_TCA_LATEST, mediaType);
433 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
435 rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType);
436 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
438 rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType);
439 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
441 rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType);
442 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
444 rawResponse = readResource(POLICYTYPES_DROOLS, mediaType);
445 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
447 rawResponse = readResource(POLICYTYPES_DROOLS_VERSION, mediaType);
448 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
450 rawResponse = readResource(POLICYTYPES_DROOLS_VERSION_LATEST, mediaType);
451 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
453 rawResponse = readResource(POLICYTYPES_NAMING_VERSION, mediaType);
454 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
458 public void testDeletePolicyTypeJson() throws Exception {
459 testDeletePolicyType(APP_JSON);
463 public void testDeletePolicyTypeYaml() throws Exception {
464 testDeletePolicyType(APP_YAML);
467 private void testDeletePolicyType(String mediaType) throws Exception {
468 Response rawResponse = deleteResource("policytypes/onap.policies.IDoNotExist/versions/1.0.0", mediaType);
469 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
471 rawResponse = createResource(POLICYTYPES, "policytypes/onap.policies.Test.yaml");
472 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
474 rawResponse = readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType);
475 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
477 rawResponse = deleteResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType);
478 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
480 rawResponse = readResource("policytypes/onap.policies.Test/versions/1.0.0", mediaType);
481 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
485 public void testReadPoliciesJson() throws Exception {
486 testReadPolicies(APP_JSON);
490 public void testReadPoliciesYaml() throws Exception {
491 testReadPolicies(APP_YAML);
494 private void testReadPolicies(String mediaType) throws Exception {
495 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
496 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
497 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
500 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType);
501 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
503 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType);
504 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
506 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
507 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
509 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType);
510 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
512 rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
513 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
518 public void testNamingPolicyGet() throws Exception {
520 Response rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
521 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON);
522 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
524 rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
525 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0?mode=referenced", APP_JSON);
526 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
528 ToscaServiceTemplate namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
529 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
530 assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
531 assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
533 rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
534 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest?mode=referenced", APP_JSON);
535 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
537 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
538 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
539 assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
540 assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
542 rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies?mode=referenced",
544 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
546 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
547 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
548 assertEquals(1, namingServiceTemplate.getPolicyTypesAsMap().size());
549 assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size());
551 rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
552 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/1.0.0", APP_JSON);
553 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
555 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
557 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
558 assertNull(namingServiceTemplate.getPolicyTypes());
559 assertNull(namingServiceTemplate.getDataTypes());
561 rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/"
562 + "policies/SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP/versions/latest", APP_JSON);
563 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
565 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
566 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
567 assertNull(namingServiceTemplate.getPolicyTypes());
568 assertNull(namingServiceTemplate.getDataTypes());
570 rawResponse = readResource("policytypes/onap.policies.Naming/versions/1.0.0/policies", APP_JSON);
571 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
573 namingServiceTemplate = rawResponse.readEntity(ToscaServiceTemplate.class);
574 assertEquals(1, namingServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
575 assertNull(namingServiceTemplate.getPolicyTypes());
576 assertNull(namingServiceTemplate.getDataTypes());
580 public void testDeletePoliciesJson() throws Exception {
581 testDeletePolicies(APP_JSON);
585 public void testDeletePoliciesYaml() throws Exception {
586 testDeletePolicies(APP_YAML);
589 private void testDeletePolicies(String mediaType) throws Exception {
590 Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
591 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
592 ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
593 assertEquals("policy onap.restart.tca:1.0.0 not found", error.getErrorMessage());
597 public void testDeletePolicyVersionJson() throws Exception {
598 testDeletePolicyVersion(APP_JSON);
602 public void testDeletePolicyVersionYaml() throws Exception {
603 testDeletePolicyVersion(APP_YAML);
606 private void testDeletePolicyVersion(String mediaType) throws Exception {
607 for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
608 Response rawResponse = createResource(POLICYTYPES, resrcName);
609 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
610 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
611 assertNotNull(response);
612 assertFalse(response.getPolicyTypes().isEmpty());
614 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
615 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
616 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
618 Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
619 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
621 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType);
622 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
623 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
624 assertEquals("policies for onap.restart.tca:1.0.0 do not exist", errorResponse.getErrorMessage());
626 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType);
627 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
628 errorResponse = rawResponse.readEntity(ErrorResponse.class);
629 assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
631 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType);
632 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
633 errorResponse = rawResponse.readEntity(ErrorResponse.class);
634 assertEquals("policies for onap.restart.tca:null do not exist", errorResponse.getErrorMessage());
638 public void testGetAllVersionOfPolicyJson() throws Exception {
639 testGetAllVersionOfPolicy(APP_JSON);
643 public void testGetAllVersionOfPolicyYaml() throws Exception {
644 testGetAllVersionOfPolicy(APP_YAML);
647 private void testGetAllVersionOfPolicy(String mediaType) throws Exception {
648 for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
649 Response rawResponse = createResource(POLICYTYPES, resrcName);
650 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
651 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
652 assertNotNull(response);
653 assertFalse(response.getPolicyTypes().isEmpty());
655 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
656 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
657 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
659 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType);
660 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
664 public void testGetPoliciesJson() throws Exception {
665 getPolicies(APP_JSON);
669 public void testGetPoliciesYaml() throws Exception {
670 getPolicies(APP_YAML);
673 private void getPolicies(String mediaType) throws Exception {
674 for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
675 Response rawResponse = createResource(POLICYTYPES, resrcName);
676 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
677 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
678 assertThat(response).isNotNull();
679 assertThat(response.getPolicyTypes()).isNotEmpty();
681 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
682 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
683 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
685 Response rawResponse = readResource(POLICIES, mediaType);
686 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
687 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
688 assertThat(response.getToscaTopologyTemplate().getPolicies()).isNotEmpty();
692 public void testGetSpecificPolicyJson() throws Exception {
693 getSpecificPolicy(APP_JSON);
697 public void testGetSpecificPolicyYaml() throws Exception {
698 getSpecificPolicy(APP_YAML);
701 private void getSpecificPolicy(String mediaType) throws Exception {
702 for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
703 Response rawResponse = createResource(POLICYTYPES, resrcName);
704 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
705 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
706 assertThat(response).isNotNull();
707 assertThat(response.getPolicyTypes()).isNotEmpty();
709 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
710 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
711 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
713 Response rawResponse = readResource(POLICIES_VCPE_VERSION1, mediaType);
714 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
715 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
716 assertThat(response.getToscaTopologyTemplate().getPolicies()).hasSize(1);
720 public void testDeleteSpecificPolicy() throws Exception {
721 Response rawResponse;
722 for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) {
723 rawResponse = createResource(POLICYTYPES, resrcName);
724 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
725 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
726 assertThat(response).isNotNull();
727 assertThat(response.getPolicyTypes()).isNotEmpty();
729 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
730 rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
731 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
734 rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON);
735 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
737 // delete a particular policy
738 rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON);
739 assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
741 rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON);
742 assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
744 rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON);
745 assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode());
750 private Response createResource(String endpoint, String resourceName) throws Exception {
752 String mediaType = APP_JSON; // default media type
753 ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
754 if (resourceName.endsWith(".json")) {
755 rawServiceTemplate = standardCoder
756 .decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
757 } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
758 mediaType = APP_YAML;
759 rawServiceTemplate = standardYamlCoder
760 .decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
763 final Invocation.Builder invocationBuilder;
765 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
767 Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
768 return invocationBuilder.post(entity);
771 private Response readResource(String endpoint, String mediaType) throws Exception {
773 final Invocation.Builder invocationBuilder;
775 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
777 return invocationBuilder.get();
781 private Response deleteResource(String endpoint, String mediaType) throws Exception {
783 final Invocation.Builder invocationBuilder;
785 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
787 return invocationBuilder.delete();
790 private Invocation.Builder sendHttpsRequest(final String endpoint, String mediaType) throws Exception {
792 final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
794 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
795 sc.init(null, noopTrustManager, new SecureRandom());
796 final ClientBuilder clientBuilder = ClientBuilder.newBuilder().sslContext(sc)
797 .hostnameVerifier((host, session) -> true);
798 final Client client = clientBuilder.build();
799 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
800 client.register(feature);
802 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
803 if (APP_JSON.equalsIgnoreCase(mediaType)) {
804 client.register(GsonMessageBodyHandler.class);
805 } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
806 client.register(YamlMessageBodyHandler.class);
809 final WebTarget webTarget = client.target("https://localhost:" + apiPort + "/policy/api/v1/" + endpoint);
811 final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
813 if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
814 throw new IllegalStateException("cannot connect to port " + apiPort);
816 return invocationBuilder;
819 private void updateApiStatistics() {
821 ApiStatisticsManager.updateTotalApiCallCount();
822 ApiStatisticsManager.updateApiCallSuccessCount();
823 ApiStatisticsManager.updateApiCallFailureCount();
824 ApiStatisticsManager.updateTotalPolicyGetCount();
825 ApiStatisticsManager.updateTotalPolicyPostCount();
826 ApiStatisticsManager.updateTotalPolicyTypeGetCount();
827 ApiStatisticsManager.updateTotalPolicyTypePostCount();
828 ApiStatisticsManager.updatePolicyGetSuccessCount();
829 ApiStatisticsManager.updatePolicyGetFailureCount();
830 ApiStatisticsManager.updatePolicyPostSuccessCount();
831 ApiStatisticsManager.updatePolicyPostFailureCount();
832 ApiStatisticsManager.updatePolicyTypeGetSuccessCount();
833 ApiStatisticsManager.updatePolicyTypeGetFailureCount();
834 ApiStatisticsManager.updatePolicyTypePostSuccessCount();
835 ApiStatisticsManager.updatePolicyTypePostFailureCount();
838 private void validateStatisticsReport(final StatisticsReport report, final int code) {
840 assertEquals(code, report.getCode());
843 private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
844 final String message, final HealthCheckReport report) {
846 assertEquals(name, report.getName());
847 assertEquals(url, report.getUrl());
848 assertEquals(healthy, report.isHealthy());
849 assertEquals(code, report.getCode());
850 assertEquals(message, report.getMessage());