Add tests
[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.JsonSyntaxException;
28
29 import java.io.IOException;
30 import java.time.Instant;
31 import java.util.List;
32
33 import javax.transaction.Transactional;
34
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.clamp.clds.Application;
38 import org.onap.clamp.loop.template.PolicyModel;
39 import org.onap.clamp.loop.template.PolicyModelsRepository;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.ActiveProfiles;
43 import org.springframework.test.context.junit4.SpringRunner;
44
45 @RunWith(SpringRunner.class)
46 @SpringBootTest(classes = Application.class)
47 @ActiveProfiles(profiles = "clamp-default,clamp-policy-controller")
48 public class PolicyEngineControllerTestItCase {
49
50     @Autowired
51     PolicyEngineController policyController;
52
53     @Autowired
54     PolicyModelsRepository policyModelsRepository;
55
56     @Test
57     @Transactional
58     public void synchronizeAllPoliciesTest() throws JsonSyntaxException, IOException, InterruptedException {
59         policyController.synchronizeAllPolicies();
60         Instant firstExecution = policyController.getLastInstantExecuted();
61         assertThat (firstExecution).isNotNull();
62         List<PolicyModel> policyModelsList = policyModelsRepository.findAll();
63         assertThat(policyModelsList.size()).isGreaterThanOrEqualTo(8);
64         assertThat(policyModelsList).contains(new PolicyModel("onap.policies.Monitoring", null, "1.0.0"));
65         assertThat(policyModelsList).contains(new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0"));
66
67         // Re-do it to check that there is no issue with duplicate key
68         policyController.synchronizeAllPolicies();
69         Instant secondExecution = policyController.getLastInstantExecuted();
70         assertThat (secondExecution).isNotNull();
71
72         assertThat(firstExecution).isBefore(secondExecution);
73     }
74
75 }