1 package org.onap.clamp.policy.downloader;
3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2020 AT&T Intellectual Property. All rights
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END============================================
21 * ===================================================================
25 import static org.assertj.core.api.Assertions.assertThat;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonSyntaxException;
29 import java.io.IOException;
30 import java.time.Instant;
31 import java.util.List;
32 import javax.transaction.Transactional;
33 import org.json.simple.parser.ParseException;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.clamp.clds.Application;
37 import org.onap.clamp.clds.util.JsonUtils;
38 import org.onap.clamp.loop.template.PolicyModel;
39 import org.onap.clamp.loop.template.PolicyModelId;
40 import org.onap.clamp.loop.template.PolicyModelsRepository;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.ActiveProfiles;
44 import org.springframework.test.context.junit4.SpringRunner;
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = Application.class)
48 @ActiveProfiles(profiles = "clamp-default,clamp-policy-controller")
49 public class PolicyEngineControllerTestItCase {
52 PolicyEngineController policyController;
55 PolicyModelsRepository policyModelsRepository;
58 * This method tests a fake synchronization with the emulator.
60 * @throws JsonSyntaxException In case of issues
61 * @throws IOException In case of issues
62 * @throws InterruptedException In case of issues
66 public void synchronizeAllPoliciesTest() throws JsonSyntaxException, IOException, InterruptedException {
67 policyController.synchronizeAllPolicies();
68 Instant firstExecution = policyController.getLastInstantExecuted();
69 assertThat(firstExecution).isNotNull();
70 List<PolicyModel> policyModelsList = policyModelsRepository.findAll();
71 assertThat(policyModelsList.size()).isGreaterThanOrEqualTo(5);
72 assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.operational.common.Drools",
74 assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.operational.common.Apex",
76 assertThat(policyModelsList)
77 .contains(new PolicyModel("onap.policies.controlloop.guard.common.FrequencyLimiter", null, "1.0.0"));
78 assertThat(policyModelsList)
79 .contains(new PolicyModel("onap.policies.controlloop.guard.common.Blacklist", null, "1.0.0"));
80 assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.guard.common.MinMax", null, "2.0.0"));
82 // Re-do it to check that there is no issue with duplicate key
83 policyController.synchronizeAllPolicies();
84 Instant secondExecution = policyController.getLastInstantExecuted();
85 assertThat(secondExecution).isNotNull();
87 assertThat(firstExecution).isBefore(secondExecution);
92 public void downloadPdpGroupsTest() throws JsonSyntaxException, IOException, InterruptedException, ParseException {
93 PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
94 policyModelsRepository.saveAndFlush(policyModel1);
95 PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0");
96 policyModelsRepository.saveAndFlush(policyModel2);
98 policyController.downloadPdpGroups();
100 List<PolicyModel> policyModelsList = policyModelsRepository.findAll();
101 assertThat(policyModelsList.size()).isGreaterThanOrEqualTo(2);
103 PolicyModel policy1 = policyModelsRepository
104 .getOne(new PolicyModelId("onap.policies.monitoring.test", "1.0.0"));
105 PolicyModel policy2 = policyModelsRepository
106 .getOne(new PolicyModelId("onap.policies.controlloop.Operational", "1.0.0"));
108 String expectedRes1 = "{\"supportedPdpGroups\":[{\"monitoring\":[\"xacml\"]}]}";
109 JsonObject expectedJson1 = JsonUtils.GSON.fromJson(expectedRes1, JsonObject.class);
110 assertThat(policy1.getPolicyPdpGroup()).isEqualTo(expectedJson1);
111 String expectedRes2 = "{\"supportedPdpGroups\":[{\"controlloop\":[\"apex\",\"drools\"]}]}";
112 JsonObject expectedJson2 = JsonUtils.GSON.fromJson(expectedRes2, JsonObject.class);
113 assertThat(policy2.getPolicyPdpGroup()).isEqualTo(expectedJson2);