03b2e506d7fd244e18d47cda4e5a32dc60604d72
[clamp.git] / src / test / java / org / onap / clamp / policy / downloader / PolicyEngineControllerTestItCase.java
1 package org.onap.clamp.policy.downloader;
2 /*-
3  * ============LICENSE_START=======================================================
4  * ONAP CLAMP
5  * ================================================================================
6  * Copyright (C) 2020 AT&T Intellectual Property. All rights
7  *                             reserved.
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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  * ===================================================================
22  *
23  */
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
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;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = Application.class)
48 @ActiveProfiles(profiles = "clamp-default,clamp-policy-controller")
49 public class PolicyEngineControllerTestItCase {
50
51     @Autowired
52     PolicyEngineController policyController;
53
54     @Autowired
55     PolicyModelsRepository policyModelsRepository;
56
57     /**
58      * This method tests a fake synchronization with the emulator.
59      *
60      * @throws JsonSyntaxException  In case of issues
61      * @throws IOException          In case of issues
62      * @throws InterruptedException In case of issues
63      */
64     @Test
65     @Transactional
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",
73                 null, "1.0.0"));
74         assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.operational.common.Apex",
75                 null, "1.0.0"));
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)
81                 .contains(new PolicyModel("onap.policies.controlloop.guard.common.MinMax", null, "2.0.0"));
82
83         // Re-do it to check that there is no issue with duplicate key
84         policyController.synchronizeAllPolicies();
85         Instant secondExecution = policyController.getLastInstantExecuted();
86         assertThat(secondExecution).isNotNull();
87
88         assertThat(firstExecution).isBefore(secondExecution);
89     }
90
91     @Test
92     @Transactional
93     public void downloadPdpGroupsTest() throws JsonSyntaxException, IOException, InterruptedException, ParseException {
94         PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
95         policyModelsRepository.saveAndFlush(policyModel1);
96         PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0");
97         policyModelsRepository.saveAndFlush(policyModel2);
98
99         policyController.downloadPdpGroups();
100
101         List<PolicyModel> policyModelsList = policyModelsRepository.findAll();
102         assertThat(policyModelsList.size()).isGreaterThanOrEqualTo(2);
103
104         PolicyModel policy1 = policyModelsRepository
105                 .getOne(new PolicyModelId("onap.policies.monitoring.test", "1.0.0"));
106         PolicyModel policy2 = policyModelsRepository
107                 .getOne(new PolicyModelId("onap.policies.controlloop.Operational", "1.0.0"));
108
109         String expectedRes1 = "{\"supportedPdpGroups\":[{\"monitoring\":[\"xacml\"]}]}";
110         JsonObject expectedJson1 = JsonUtils.GSON.fromJson(expectedRes1, JsonObject.class);
111         assertThat(policy1.getPolicyPdpGroup()).isEqualTo(expectedJson1);
112         String expectedRes2 = "{\"supportedPdpGroups\":[{\"controlloop\":[\"apex\",\"drools\"]}]}";
113         JsonObject expectedJson2 = JsonUtils.GSON.fromJson(expectedRes2, JsonObject.class);
114         assertThat(policy2.getPolicyPdpGroup()).isEqualTo(expectedJson2);
115
116     }
117 }