2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2019 Nordix Foundation.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Modifier;
33 import java.security.SecureRandom;
34 import java.util.Base64;
35 import java.util.Collections;
36 import java.util.Properties;
38 import javax.net.ssl.SSLContext;
39 import javax.net.ssl.TrustManager;
40 import javax.ws.rs.client.Client;
41 import javax.ws.rs.client.ClientBuilder;
42 import javax.ws.rs.client.Entity;
43 import javax.ws.rs.client.Invocation;
44 import javax.ws.rs.client.WebTarget;
45 import javax.ws.rs.core.Response;
47 import org.glassfish.jersey.client.ClientProperties;
48 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.onap.policy.api.main.parameters.ApiParameterGroup;
54 import org.onap.policy.api.main.parameters.CommonTestData;
55 import org.onap.policy.api.main.rest.provider.PolicyProvider;
56 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
57 import org.onap.policy.api.main.startstop.Main;
58 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
59 import org.onap.policy.common.endpoints.report.HealthCheckReport;
60 import org.onap.policy.common.gson.GsonMessageBodyHandler;
61 import org.onap.policy.common.parameters.ParameterService;
62 import org.onap.policy.common.utils.coder.StandardCoder;
63 import org.onap.policy.common.utils.coder.StandardYamlCoder;
64 import org.onap.policy.common.utils.network.NetworkUtil;
65 import org.onap.policy.common.utils.resources.ResourceUtils;
66 import org.onap.policy.common.utils.resources.TextFileUtils;
67 import org.onap.policy.models.base.PfModelException;
68 import org.onap.policy.models.errors.concepts.ErrorResponse;
69 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
70 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
71 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
72 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
73 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
76 * Class to perform unit test of {@link ApiRestServer}.
78 * @author Chenfei Gao (cgao@research.att.com)
80 public class TestApiRestServer {
81 private static final String ALIVE = "alive";
82 private static final String SELF = NetworkUtil.getHostname();
83 private static final String NAME = "Policy API";
84 private static final String APP_JSON = "application/json";
85 private static final String APP_YAML = "application/yaml";
87 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
88 private static final String STATISTICS_ENDPOINT = "statistics";
90 private static final String POLICYTYPES = "policytypes";
91 private static final String POLICYTYPES_TCA = "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app";
92 private static final String POLICYTYPES_COLLECTOR =
93 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
94 private static final String POLICYTYPES_TCA_VERSION =
95 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0";
96 private static final String POLICYTYPES_TCA_LATEST =
97 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/latest";
98 private static final String POLICYTYPES_COLLECTOR_VERSION =
99 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/1.0.0";
100 private static final String POLICYTYPES_COLLECTOR_LATEST =
101 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server/versions/latest";
103 private static final String POLICYTYPES_TCA_POLICIES =
104 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies";
105 private static final String POLICYTYPES_TCA_POLICIES_VCPE =
106 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca";
107 private static final String POLICYTYPES_TCA_POLICIES_VCPE_VERSION = "policytypes/"
108 + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/1.0.0";
109 private static final String POLICYTYPES_TCA_POLICIES_VCPE_LATEST = "policytypes/"
110 + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/latest";
111 private static final String POLICYTYPES_TCA_POLICIES_VCPE_DEPLOYED = "policytypes/"
112 + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/deployed";
114 private static final String GUARD_POLICIES = "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies";
115 private static final String GUARD_POLICIES_VDNS_FL_LATEST =
116 "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.frequency.scaleout"
117 + "/versions/latest";
118 private static final String GUARD_POLICIES_VDNS_FL_DEPLOYED =
119 "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.frequency.scaleout"
120 + "/versions/deployed";
121 private static final String GUARD_POLICIES_VDNS_MINMAX_LATEST =
122 "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.minmax.scaleout"
123 + "/versions/latest";
124 private static final String GUARD_POLICIES_VDNS_FL_VERSION = "policytypes/"
125 + "onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.frequency.scaleout/versions/1";
126 private static final String GUARD_POLICIES_VDNS_MINMAX_VERSION =
127 "policytypes/" + "onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.minmax.scaleout/versions/1";
129 private static final String OPS_POLICIES =
130 "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies";
131 private static final String OPS_POLICIES_VCPE_LATEST =
132 "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.restart"
133 + "/versions/latest";
134 private static final String OPS_POLICIES_VCPE_DEPLOYED =
135 "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.restart"
136 + "/versions/deployed";
137 private static final String OPS_POLICIES_VDNS_LATEST =
138 "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.scaleout"
139 + "/versions/latest";
140 private static final String OPS_POLICIES_VFIREWALL_LATEST =
141 "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.modifyconfig"
142 + "/versions/latest";
143 private static final String OPS_POLICIES_VCPE_VERSION = "policytypes/"
144 + "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.restart/versions/1";
145 private static final String OPS_POLICIES_VDNS_VERSION = "policytypes/"
146 + "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.scaleout/versions/1";
147 private static final String OPS_POLICIES_VFIREWALL_VERSION = "policytypes/"
148 + "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.modifyconfig/versions/1";
149 private static final String POLICIES = "policies";
151 private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
154 private static final String[] TOSCA_POLICY_RESOURCE_NAMES = {
155 "policies/vCPE.policy.monitoring.input.tosca.json",
156 "policies/vCPE.policy.monitoring.input.tosca.yaml",
157 "policies/vDNS.policy.monitoring.input.tosca.json",
158 "policies/vDNS.policy.monitoring.input.tosca.yaml",
159 "policies/vFirewall.policy.monitoring.input.tosca.json",
160 "policies/vFirewall.policy.monitoring.input.tosca.yaml"
163 private static final String[] TOSCA_POLICIES_RESOURCE_NAMES = {
164 "policies/vCPE.policies.optimization.input.tosca.json",
165 "policies/vCPE.policies.optimization.input.tosca.yaml"
168 private static final String[] TOSCA_PPOLICYTYPE_RESOURCE_NAMES = {
169 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
170 "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
171 "policytypes/onap.policies.Optimization.yaml",
172 "policytypes/onap.policies.controlloop.Operational.yaml",
173 "policytypes/onap.policies.controlloop.guard.Blacklist.yaml",
174 "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml",
175 "policytypes/onap.policies.controlloop.guard.MinMax.yaml",
176 "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
177 "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
178 "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
179 "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
180 "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
181 "policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
182 "policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
183 "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
184 "policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
185 "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"
188 private static final String[] LEGACY_GUARD_POLICY_RESOURCE_NAMES = {
189 "policies/vDNS.policy.guard.frequency.input.json",
190 "policies/vDNS.policy.guard.minmax.input.json"
193 private static final String[] LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES = {
194 "policies/vCPE.policy.operational.input.json",
195 "policies/vDNS.policy.operational.input.json",
196 "policies/vFirewall.policy.operational.input.json"
199 private static PolicyModelsProviderParameters providerParams;
200 private static ApiParameterGroup apiParamGroup;
201 private static PolicyProvider policyProvider;
202 private static PolicyTypeProvider policyTypeProvider;
206 private static final StandardCoder standardCoder = new StandardCoder();
208 private static int apiPort;
209 private static Main apiMain;
211 private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
214 * Initializes parameters.
216 * @throws PfModelException the PfModel parsing exception
219 public static void setupParameters() throws PfModelException {
220 providerParams = new PolicyModelsProviderParameters();
221 providerParams.setDatabaseDriver("org.h2.Driver");
222 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
223 providerParams.setDatabaseUser("policy");
224 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
225 providerParams.setPersistenceUnit("ToscaConceptTest");
226 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList());
227 ParameterService.register(apiParamGroup, true);
229 policyTypeProvider = new PolicyTypeProvider();
230 policyProvider = new PolicyProvider();
234 * Set up test environemnt.
236 * @throws Exception on test setup exceptions
239 public static void beforeStartApiService() throws Exception {
240 apiPort = NetworkUtil.allocPort();
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 apiMain = new Main(apiConfigParameters);
255 * Method for cleanup after each test.
258 public static void teardown() throws Exception {
259 policyTypeProvider.close();
260 policyProvider.close();
262 if (apiMain != null) {
268 * Clear the database before each test.
270 * @throws Exception on clearing exceptions
273 public void beforeClearDatabase() throws Exception {
274 Response rawResponse = readResource(POLICYTYPES, APP_JSON);
275 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
277 for (ToscaEntityKey policyTypeKey : response.getPolicyTypesAsMap().keySet()) {
278 String getPoliciesPath =
279 "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion() + "/policies";
281 Response rawPolicyResponse = readResource(getPoliciesPath, APP_JSON);
282 if (Response.Status.OK.getStatusCode() == rawPolicyResponse.getStatus()) {
283 ToscaServiceTemplate policyResponse = rawPolicyResponse.readEntity(ToscaServiceTemplate.class);
285 for (ToscaEntityKey policyKey : policyResponse.getToscaTopologyTemplate().getPoliciesAsMap().keySet()) {
286 String deletePolicyPath =
287 "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion()
288 + "/policies/" + policyKey.getName() + "/versions/" + policyKey.getVersion();
289 deleteResource(deletePolicyPath, APP_JSON);
293 String deletePolicyTypePath =
294 "policytypes/" + policyTypeKey.getName() + "/versions/" + policyTypeKey.getVersion();
295 deleteResource(deletePolicyTypePath, APP_JSON);
300 public void testApiStatisticsConstructorIsPrivate() {
303 final Constructor<ApiStatisticsManager> constructor = ApiStatisticsManager.class.getDeclaredConstructor();
304 assertTrue(Modifier.isPrivate(constructor.getModifiers()));
305 constructor.setAccessible(true);
306 constructor.newInstance();
308 } catch (final Exception exp) {
309 assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
314 public void testCreatePolicyTypes() throws Exception {
315 for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) {
316 Response rawResponse = createResource(POLICYTYPES, resrcName);
317 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
318 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
319 assertNotNull(response);
320 assertFalse(response.getPolicyTypes().isEmpty());
323 // Send a policy type with a null value to trigger an error
324 Response rawResponse = readResource(POLICYTYPES, APP_JSON);
325 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
326 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
327 String firstPolicyType = response.getPolicyTypes().keySet().iterator().next();
328 response.getPolicyTypes().put(firstPolicyType, null);
329 Response rawResponse2 = createResource(POLICYTYPES, standardCoder.encode(response));
330 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse2.getStatus());
331 ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
332 assertEquals("no policy types specified on service template", errorResponse.getErrorMessage());
336 public void testCreatePolicies() throws Exception {
339 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
340 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
341 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
344 new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
346 // Send a policy with no policy type trigger an error
347 String toscaPolicy = ResourceUtils
348 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
350 toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.cdap.tca.hi.lo.app", "");
351 TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
353 Response rawResponse2 =
354 createResource(POLICYTYPES_TCA_POLICIES, "src/test/resources/policies/BadTestPolicy.yaml");
355 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse2.getStatus());
356 ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
357 assertEquals("policy type id does not match", errorResponse.getErrorMessage());
361 public void testSimpleCreatePolicies() throws Exception {
362 testCreatePolicyTypes();
364 for (String resrcName : TOSCA_POLICIES_RESOURCE_NAMES) {
365 Response rawResponse = createResource(POLICIES, resrcName);
366 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
369 new File("src/test/resources/policies/BadTestPolicy.yaml").deleteOnExit();
371 // Send a policy with no policy type trigger an error
372 String toscaPolicy = ResourceUtils
373 .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]);
375 toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.cdap.tca.hi.lo.app", "");
376 TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml");
378 Response rawResponse2 = createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml");
379 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse2.getStatus());
380 ErrorResponse errorResponse = rawResponse2.readEntity(ErrorResponse.class);
381 assertEquals("policy type NULL:0.0.0 for policy onap.restart.tca:1.0.0 does not exist",
382 errorResponse.getErrorMessage());
386 public void testCreateGuardPolicies() throws Exception {
389 for (String resrcName : LEGACY_GUARD_POLICY_RESOURCE_NAMES) {
390 Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName);
391 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
396 public void testCreateOperationalPolicies() throws Exception {
399 for (String resrcName : LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES) {
400 Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName);
401 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
406 public void testHealthCheckSuccessJson() throws Exception {
407 testHealthCheckSuccess(APP_JSON);
411 public void testHealthCheckSuccessYaml() throws Exception {
412 testHealthCheckSuccess(APP_YAML);
415 private void testHealthCheckSuccess(String mediaType) throws Exception {
416 final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT, mediaType);
417 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
418 validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
422 public void testApiStatistics_200_Json() throws Exception {
423 testApiStatistics_200(APP_JSON);
427 public void testApiStatistics_200_Yaml() throws Exception {
428 testApiStatistics_200(APP_YAML);
431 private void testApiStatistics_200(String mediaType) throws Exception {
432 Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
433 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
434 validateStatisticsReport(report, 200);
435 updateApiStatistics();
436 invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
437 report = invocationBuilder.get(StatisticsReport.class);
438 validateStatisticsReport(report, 200);
439 ApiStatisticsManager.resetAllStatistics();
443 public void testReadPolicyTypesJson() throws Exception {
444 testReadPolicyTypes(APP_JSON);
448 public void testReadPolicyTypesYaml() throws Exception {
449 testReadPolicyTypes(APP_YAML);
452 private void testReadPolicyTypes(String mediaType) throws Exception {
455 Response rawResponse = readResource(POLICYTYPES, mediaType);
456 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
457 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
458 assertFalse(response.getPolicyTypes().isEmpty());
460 rawResponse = readResource(POLICYTYPES_TCA, mediaType);
461 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
463 rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType);
464 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
466 rawResponse = readResource(POLICYTYPES_TCA_LATEST, mediaType);
467 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
469 rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType);
470 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
472 rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType);
473 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
475 rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType);
476 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
480 public void testDeletePolicyTypeJson() throws Exception {
481 testDeletePolicyType(APP_JSON);
485 public void testDeletePolicyTypeYaml() throws Exception {
486 testDeletePolicyType(APP_YAML);
489 private void testDeletePolicyType(String mediaType) throws Exception {
492 Response rawResponse = deleteResource(POLICYTYPES_TCA_VERSION, mediaType);
493 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
495 rawResponse = readResource(POLICYTYPES_TCA_VERSION, mediaType);
496 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
498 rawResponse = deleteResource(POLICYTYPES_COLLECTOR_VERSION, mediaType);
499 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
501 rawResponse = readResource(POLICYTYPES_COLLECTOR_VERSION, mediaType);
502 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
504 rawResponse = readResource(POLICYTYPES_COLLECTOR, mediaType);
505 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
507 rawResponse = readResource(POLICYTYPES_COLLECTOR_LATEST, mediaType);
508 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
509 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
510 assertEquals("policy type with ID "
511 + "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server:null does not exist",
512 errorResponse.getErrorMessage());
516 public void testReadPoliciesJson() throws Exception {
517 testReadPolicies(APP_JSON);
521 public void testReadPoliciesYaml() throws Exception {
522 testReadPolicies(APP_YAML);
525 private void testReadPolicies(String mediaType) throws Exception {
526 testCreatePolicies();
528 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType);
529 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
531 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType);
532 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
534 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType);
535 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
537 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType);
538 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
542 public void testDeletePoliciesJson() throws Exception {
543 testDeletePolicies(APP_JSON);
547 public void testDeletePoliciesYaml() throws Exception {
548 testDeletePolicies(APP_YAML);
551 private void testDeletePolicies(String mediaType) throws Exception {
554 Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType);
555 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
556 ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
558 "policy with ID onap.restart.tca:1.0.0 and "
559 + "type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist",
560 error.getErrorMessage());
564 public void testDeletePolicyVersionJson() throws Exception {
565 testDeletePolicyVersion(APP_JSON);
569 public void testDeletePolicyVersionYaml() throws Exception {
570 testDeletePolicyVersion(APP_YAML);
573 private void testDeletePolicyVersion(String mediaType) throws Exception {
574 for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) {
575 Response rawResponse = createResource(POLICYTYPES, resrcName);
576 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
577 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
578 assertNotNull(response);
579 assertFalse(response.getPolicyTypes().isEmpty());
581 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
582 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
583 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
585 Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType);
586 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
588 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION, mediaType);
589 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
590 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
592 "policy with ID onap.restart.tca:1.0.0 and type "
593 + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist",
594 errorResponse.getErrorMessage());
596 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE, mediaType);
597 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
598 errorResponse = rawResponse.readEntity(ErrorResponse.class);
600 "policy with ID onap.restart.tca:null and type "
601 + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist",
602 errorResponse.getErrorMessage());
604 rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_LATEST, mediaType);
605 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
606 errorResponse = rawResponse.readEntity(ErrorResponse.class);
608 "policy with ID onap.restart.tca:null and type "
609 + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist",
610 errorResponse.getErrorMessage());
614 public void testGetAllVersionOfPolicyJson() throws Exception {
615 testGetAllVersionOfPolicy(APP_JSON);
619 public void testGetAllVersionOfPolicyYaml() throws Exception {
620 testGetAllVersionOfPolicy(APP_YAML);
623 private void testGetAllVersionOfPolicy(String mediaType) throws Exception {
624 for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) {
625 Response rawResponse = createResource(POLICYTYPES, resrcName);
626 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
627 ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class);
628 assertNotNull(response);
629 assertFalse(response.getPolicyTypes().isEmpty());
631 for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) {
632 Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName);
633 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
635 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES, mediaType);
636 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
640 public void testReadGuardPoliciesJson() throws Exception {
641 testReadGuardPolicies(APP_JSON);
645 public void testReadGuardPoliciesYaml() throws Exception {
646 testReadGuardPolicies(APP_YAML);
649 private void testReadGuardPolicies(String mediaType) throws Exception {
650 createGuardPolicies();
652 Response rawResponse = readResource(GUARD_POLICIES_VDNS_FL_LATEST, mediaType);
653 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
655 rawResponse = readResource(GUARD_POLICIES_VDNS_FL_VERSION, mediaType);
656 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
658 rawResponse = readResource(GUARD_POLICIES_VDNS_MINMAX_LATEST, mediaType);
659 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
661 rawResponse = readResource(GUARD_POLICIES_VDNS_MINMAX_VERSION, mediaType);
662 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
666 public void testReadOperationalPoliciesJson() throws Exception {
667 testReadOperationalPolicies(APP_JSON);
671 public void testReadOperationalPoliciesYaml() throws Exception {
672 testReadOperationalPolicies(APP_YAML);
675 private void testReadOperationalPolicies(String mediaType) throws Exception {
676 createOperationalPolicies();
678 Response rawResponse = readResource(OPS_POLICIES_VCPE_LATEST, mediaType);
679 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
681 rawResponse = readResource(OPS_POLICIES_VCPE_VERSION, mediaType);
682 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
684 rawResponse = readResource(OPS_POLICIES_VDNS_LATEST, mediaType);
685 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
687 rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, mediaType);
688 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
690 rawResponse = readResource(OPS_POLICIES_VFIREWALL_LATEST, mediaType);
691 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
693 rawResponse = readResource(OPS_POLICIES_VFIREWALL_VERSION, mediaType);
694 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
698 public void testDeleteGuardPolicyJson() throws Exception {
699 testDeleteGuardPolicy(APP_JSON);
703 public void testDeleteGuardPolicyYaml() throws Exception {
704 testDeleteGuardPolicy(APP_YAML);
707 private void testDeleteGuardPolicy(String mediaType) throws Exception {
708 createGuardPolicies();
710 Response rawResponse = deleteResource(GUARD_POLICIES_VDNS_FL_VERSION, mediaType);
711 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
715 public void testGetDeployedVersionsOfGuardPolicyJson() throws Exception {
716 testGetDeployedVersionsOfGuardPolicy(APP_JSON);
720 public void testGetDeployedVersionsOfGuardPolicyYaml() throws Exception {
721 testGetDeployedVersionsOfGuardPolicy(APP_YAML);
724 private void testGetDeployedVersionsOfGuardPolicy(String mediaType) throws Exception {
725 Response rawResponse = readResource(GUARD_POLICIES_VDNS_FL_DEPLOYED, mediaType);
726 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
727 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
729 "could not find policy with ID guard.frequency.scaleout and type "
730 + "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0 deployed in any pdp group",
731 errorResponse.getErrorMessage());
735 public void testDeleteOperationalPolicyJson() throws Exception {
736 testDeleteOperationalPolicy(APP_JSON);
740 public void testDeleteOperationalPolicyYaml() throws Exception {
741 testDeleteOperationalPolicy(APP_YAML);
744 private void testDeleteOperationalPolicy(String mediaType) throws Exception {
747 Response rawResponse = deleteResource(OPS_POLICIES_VCPE_VERSION, mediaType);
748 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus());
749 ErrorResponse error = rawResponse.readEntity(ErrorResponse.class);
750 assertEquals("no policy found for policy: operational.restart:1", error.getErrorMessage());
754 public void testGetDeployedVersionsOfPolicyJson() throws Exception {
755 testGetDeployedVersionsOfPolicy(APP_JSON);
759 public void testGetDeployedVersionsOfPolicyYaml() throws Exception {
760 testGetDeployedVersionsOfPolicy(APP_YAML);
763 private void testGetDeployedVersionsOfPolicy(String mediaType) throws Exception {
764 Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_DEPLOYED, mediaType);
765 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
766 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
768 "could not find policy with ID onap.restart.tca and type "
769 + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 deployed in any pdp group",
770 errorResponse.getErrorMessage());
774 public void testGetLatestVersionOfOperationalPolicyJson() throws Exception {
775 testGetLatestVersionOfOperationalPolicy(APP_JSON);
779 public void testGetLatestVersionOfOperationalPolicyYaml() throws Exception {
780 testGetLatestVersionOfOperationalPolicy(APP_YAML);
783 private void testGetLatestVersionOfOperationalPolicy(String mediaType) throws Exception {
784 Response rawResponse = readResource(OPS_POLICIES_VDNS_LATEST, mediaType);
785 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus());
786 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
787 assertEquals("no policy found for policy: operational.scaleout:null", errorResponse.getErrorMessage());
791 public void testGetSpecificVersionOfOperationalPolicyJson() throws Exception {
792 testGetSpecificVersionOfOperationalPolicy(APP_JSON);
796 public void testGetSpecificVersionOfOperationalPolicyYaml() throws Exception {
797 testGetSpecificVersionOfOperationalPolicy(APP_YAML);
800 private void testGetSpecificVersionOfOperationalPolicy(String mediaType) throws Exception {
801 Response rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, mediaType);
802 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus());
803 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
804 assertEquals("no policy found for policy: operational.scaleout:1", errorResponse.getErrorMessage());
808 public void testGetDeployedVersionsOfOperationalPolicyJson() throws Exception {
809 testGetDeployedVersionsOfOperationalPolicy(APP_JSON);
813 public void testGetDeployedVersionsOfOperationalPolicyYaml() throws Exception {
814 testGetDeployedVersionsOfOperationalPolicy(APP_YAML);
817 private void testGetDeployedVersionsOfOperationalPolicy(String mediaType) throws Exception {
818 Response rawResponse = readResource(OPS_POLICIES_VCPE_DEPLOYED, mediaType);
819 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus());
820 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
822 "could not find policy with ID operational.restart and type "
823 + "onap.policies.controlloop.Operational:1.0.0 deployed in any pdp group",
824 errorResponse.getErrorMessage());
828 public void testDeleteSpecificVersionOfOperationalPolicy() throws Exception {
829 createOperationalPolicies();
831 Response rawResponse = deleteResource(OPS_POLICIES_VDNS_VERSION, APP_JSON);
832 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
834 rawResponse = deleteResource(OPS_POLICIES_VDNS_VERSION, APP_YAML);
835 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus());
836 ErrorResponse errorResponse = rawResponse.readEntity(ErrorResponse.class);
837 assertEquals("no policy found for policy: operational.scaleout:1", errorResponse.getErrorMessage());
840 private Response createResource(String endpoint, String resourceName) throws Exception {
842 String mediaType = APP_JSON; // default media type
843 ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
844 if (resourceName.endsWith(".json")) {
846 standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
847 } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
848 mediaType = APP_YAML;
849 rawServiceTemplate = standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName),
850 ToscaServiceTemplate.class);
853 final Invocation.Builder invocationBuilder;
855 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
857 Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
858 return invocationBuilder.post(entity);
861 private Response createGuardPolicy(String endpoint, String resourceName) throws Exception {
863 String mediaType = APP_JSON; // default media type
864 LegacyGuardPolicyInput rawGuardPolicy = new LegacyGuardPolicyInput();
865 if (resourceName.endsWith(".json")) {
867 standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), LegacyGuardPolicyInput.class);
868 } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
869 mediaType = APP_YAML;
870 rawGuardPolicy = standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName),
871 LegacyGuardPolicyInput.class);
874 final Invocation.Builder invocationBuilder;
876 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
878 Entity<LegacyGuardPolicyInput> entity = Entity.entity(rawGuardPolicy, mediaType);
879 return invocationBuilder.post(entity);
882 private Response createOperationalPolicy(String endpoint, String resourceName) throws Exception {
884 String mediaType = APP_JSON; // default media type
885 LegacyOperationalPolicy rawOpsPolicy = new LegacyOperationalPolicy();
886 if (resourceName.endsWith(".json")) {
887 rawOpsPolicy = standardCoder.decode(ResourceUtils.getResourceAsString(resourceName),
888 LegacyOperationalPolicy.class);
889 } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
890 mediaType = APP_YAML;
891 rawOpsPolicy = standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName),
892 LegacyOperationalPolicy.class);
895 final Invocation.Builder invocationBuilder;
897 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
899 Entity<LegacyOperationalPolicy> entity = Entity.entity(rawOpsPolicy, mediaType);
900 return invocationBuilder.post(entity);
903 private Response readResource(String endpoint, String mediaType) throws Exception {
905 final Invocation.Builder invocationBuilder;
907 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
909 return invocationBuilder.get();
913 private Response deleteResource(String endpoint, String mediaType) throws Exception {
915 final Invocation.Builder invocationBuilder;
917 invocationBuilder = sendHttpsRequest(endpoint, mediaType);
919 return invocationBuilder.delete();
922 private void createPolicyTypes() throws Exception {
923 for (String resrcName : TOSCA_PPOLICYTYPE_RESOURCE_NAMES) {
924 Response rawResponse = createResource(POLICYTYPES, resrcName);
925 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
929 private void createGuardPolicies() throws Exception {
932 for (String resrcName : LEGACY_GUARD_POLICY_RESOURCE_NAMES) {
933 Response rawResponse = createGuardPolicy(GUARD_POLICIES, resrcName);
934 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
938 private void createOperationalPolicies() throws Exception {
941 for (String resrcName : LEGACY_OPERATIONAL_POLICY_RESOURCE_NAMES) {
942 Response rawResponse = createOperationalPolicy(OPS_POLICIES, resrcName);
943 assertEquals(Response.Status.OK.getStatusCode(), rawResponse.getStatus());
947 private Invocation.Builder sendHttpsRequest(final String endpoint, String mediaType) throws Exception {
949 final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
951 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
952 sc.init(null, noopTrustManager, new SecureRandom());
953 final ClientBuilder clientBuilder =
954 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
955 final Client client = clientBuilder.build();
956 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
957 client.register(feature);
959 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
960 if (APP_JSON.equalsIgnoreCase(mediaType)) {
961 client.register(GsonMessageBodyHandler.class);
962 } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
963 client.register(YamlMessageBodyHandler.class);
966 final WebTarget webTarget = client.target("https://localhost:" + apiPort + "/policy/api/v1/" + endpoint);
968 final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
970 if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
971 throw new IllegalStateException("cannot connect to port " + apiPort);
973 return invocationBuilder;
976 private void updateApiStatistics() {
978 ApiStatisticsManager.updateTotalApiCallCount();
979 ApiStatisticsManager.updateApiCallSuccessCount();
980 ApiStatisticsManager.updateApiCallFailureCount();
981 ApiStatisticsManager.updateTotalPolicyGetCount();
982 ApiStatisticsManager.updateTotalPolicyPostCount();
983 ApiStatisticsManager.updateTotalPolicyTypeGetCount();
984 ApiStatisticsManager.updateTotalPolicyTypePostCount();
985 ApiStatisticsManager.updatePolicyGetSuccessCount();
986 ApiStatisticsManager.updatePolicyGetFailureCount();
987 ApiStatisticsManager.updatePolicyPostSuccessCount();
988 ApiStatisticsManager.updatePolicyPostFailureCount();
989 ApiStatisticsManager.updatePolicyTypeGetSuccessCount();
990 ApiStatisticsManager.updatePolicyTypeGetFailureCount();
991 ApiStatisticsManager.updatePolicyTypePostSuccessCount();
992 ApiStatisticsManager.updatePolicyTypePostFailureCount();
995 private void validateStatisticsReport(final StatisticsReport report, final int code) {
997 assertEquals(code, report.getCode());
1000 private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
1001 final String message, final HealthCheckReport report) {
1003 assertEquals(name, report.getName());
1004 assertEquals(url, report.getUrl());
1005 assertEquals(healthy, report.isHealthy());
1006 assertEquals(code, report.getCode());
1007 assertEquals(message, report.getMessage());