3f502ff787ed099029fb63e549784655c572233d
[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(8);
72         assertThat(policyModelsList).contains(new PolicyModel("onap.policies.Monitoring", null, "1.0.0"));
73         assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0"));
74
75         // Re-do it to check that there is no issue with duplicate key
76         policyController.synchronizeAllPolicies();
77         Instant secondExecution = policyController.getLastInstantExecuted();
78         assertThat(secondExecution).isNotNull();
79
80         assertThat(firstExecution).isBefore(secondExecution);
81     }
82
83     @Test
84     @Transactional
85     public void downloadPdpGroupsTest() throws JsonSyntaxException, IOException, InterruptedException, ParseException {
86         PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
87         policyModelsRepository.saveAndFlush(policyModel1);
88         PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0");
89         policyModelsRepository.saveAndFlush(policyModel2);
90
91         policyController.downloadPdpGroups();
92
93         List<PolicyModel> policyModelsList = policyModelsRepository.findAll();
94         assertThat(policyModelsList.size()).isGreaterThanOrEqualTo(2);
95
96         PolicyModel policy1 = policyModelsRepository
97                 .getOne(new PolicyModelId("onap.policies.monitoring.test", "1.0.0"));
98         PolicyModel policy2 = policyModelsRepository
99                 .getOne(new PolicyModelId("onap.policies.controlloop.Operational", "1.0.0"));
100
101         String expectedRes1 = "{\"supportedPdpGroups\":[{\"monitoring\":[\"xacml\"]}]}";
102         JsonObject expectedJson1 = JsonUtils.GSON.fromJson(expectedRes1, JsonObject.class);
103         assertThat(policy1.getPolicyPdpGroup()).isEqualTo(expectedJson1);
104         String expectedRes2 = "{\"supportedPdpGroups\":[{\"controlloop\":[\"apex\",\"drools\"]}]}";
105         JsonObject expectedJson2 = JsonUtils.GSON.fromJson(expectedRes2, JsonObject.class);
106         assertThat(policy2.getPolicyPdpGroup()).isEqualTo(expectedJson2);
107
108     }
109 }