abeecd1b20241506330f598e4e0b84e5a52ab605
[clamp.git] / src / test / java / org / onap / clamp / loop / PolicyModelServiceItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import java.util.List;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31 import java.util.stream.Collectors;
32 import javax.transaction.Transactional;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.clamp.clds.Application;
36 import org.onap.clamp.loop.template.PolicyModel;
37 import org.onap.clamp.loop.template.PolicyModelId;
38 import org.onap.clamp.loop.template.PolicyModelsRepository;
39 import org.onap.clamp.loop.template.PolicyModelsService;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.junit4.SpringRunner;
43
44 @RunWith(SpringRunner.class)
45 @SpringBootTest(classes = Application.class)
46 public class PolicyModelServiceItCase {
47
48     @Autowired
49     PolicyModelsService policyModelsService;
50
51     @Autowired
52     PolicyModelsRepository policyModelsRepository;
53
54     private static final String POLICY_MODEL_TYPE_1 = "org.onap.testos";
55     private static final String POLICY_MODEL_TYPE_1_VERSION_1 = "1.0.0";
56
57     private static final String POLICY_MODEL_TYPE_2 = "org.onap.testos2";
58     private static final String POLICY_MODEL_TYPE_3 = "org.onap.testos3";
59     private static final String POLICY_MODEL_TYPE_2_VERSION_1 = "1.0.0";
60     private static final String POLICY_MODEL_TYPE_3_VERSION_1 = "1.0.0";
61     private static final String POLICY_MODEL_TYPE_2_VERSION_2 = "2.0.0";
62
63     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
64             String policyVariant, String createdBy) {
65         PolicyModel policyModel = new PolicyModel();
66         policyModel.setCreatedBy(createdBy);
67         policyModel.setPolicyAcronym(policyAcronym);
68         policyModel.setPolicyModelTosca(policyModelTosca);
69         policyModel.setPolicyModelType(policyType);
70         policyModel.setUpdatedBy(createdBy);
71         policyModel.setVersion(version);
72         return policyModel;
73     }
74
75     /**
76      * This test the create policy Model.
77      */
78     @Test
79     @Transactional
80     public void shouldCreatePolicyModel() {
81         // given
82         PolicyModel policyModel = getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", POLICY_MODEL_TYPE_1_VERSION_1, "TEST",
83                 "VARIANT", "user");
84
85         // when
86         PolicyModel actualPolicyModel = policyModelsService.saveOrUpdatePolicyModel(policyModel);
87
88         // then
89         assertThat(actualPolicyModel).isNotNull();
90         assertThat(actualPolicyModel).isEqualTo(policyModelsRepository
91                 .findById(new PolicyModelId(actualPolicyModel.getPolicyModelType(), actualPolicyModel.getVersion()))
92                 .get());
93         assertThat(actualPolicyModel.getPolicyModelType()).isEqualTo(policyModel.getPolicyModelType());
94         assertThat(actualPolicyModel.getCreatedBy()).isEqualTo("Not found");
95         assertThat(actualPolicyModel.getCreatedDate()).isNotNull();
96         assertThat(actualPolicyModel.getPolicyAcronym()).isEqualTo(policyModel.getPolicyAcronym());
97         assertThat(actualPolicyModel.getPolicyModelTosca()).isEqualTo(policyModel.getPolicyModelTosca());
98         assertThat(actualPolicyModel.getUpdatedBy()).isEqualTo("Not found");
99         assertThat(actualPolicyModel.getUpdatedDate()).isNotNull();
100         assertThat(actualPolicyModel.getVersion()).isEqualTo(policyModel.getVersion());
101
102         assertThat(policyModelsService.getPolicyModel(POLICY_MODEL_TYPE_1, POLICY_MODEL_TYPE_1_VERSION_1))
103                 .isEqualToIgnoringGivenFields(policyModel, "createdDate", "updatedDate", "createdBy", "updatedBy");
104     }
105
106     @Test
107     @Transactional
108     public void shouldReturnAllPolicyModelTypes() {
109         // given
110         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
111                 "VARIANT", "user");
112         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
113         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
114                 "VARIANT", "user");
115         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
116         List<String> policyModelTypesList = policyModelsService.getAllPolicyModelTypes();
117
118         assertThat(policyModelTypesList).contains(policyModel1.getPolicyModelType(), policyModel2.getPolicyModelType());
119     }
120
121     @Test
122     @Transactional
123     public void shouldReturnAllPolicyModels() {
124         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
125                 "VARIANT", "user");
126         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
127         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
128                 "VARIANT", "user");
129         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
130
131         assertThat(policyModelsService.getAllPolicyModels()).contains(policyModel1, policyModel2);
132     }
133
134     @Test
135     @Transactional
136     public void shouldReturnAllModelsByType() {
137         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
138                 "VARIANT", "user");
139         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
140         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
141                 "VARIANT", "user");
142         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
143
144         assertThat(policyModelsService.getAllPolicyModelsByType(POLICY_MODEL_TYPE_2)).contains(policyModel1,
145                 policyModel2);
146     }
147
148     @Test
149     @Transactional
150     public void shouldReturnSortedSet() {
151         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
152                 "VARIANT", "user");
153         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
154         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
155                 "VARIANT", "user");
156         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
157         PolicyModel policyModel3 = getPolicyModel(POLICY_MODEL_TYPE_3, "yaml", POLICY_MODEL_TYPE_3_VERSION_1, "TEST",
158                 "VARIANT", "user");
159         policyModelsService.saveOrUpdatePolicyModel(policyModel3);
160
161         SortedSet<PolicyModel> sortedSet = new TreeSet<>();
162         policyModelsService.getAllPolicyModels().forEach(sortedSet::add);
163         List<PolicyModel> listToCheck = sortedSet.stream().filter(
164             policy -> policy.equals(policyModel3) || policy.equals(policyModel2) || policy.equals(policyModel1))
165                 .collect(Collectors.toList());
166         assertThat(listToCheck.get(0)).isEqualByComparingTo(policyModel2);
167         assertThat(listToCheck.get(1)).isEqualByComparingTo(policyModel1);
168         assertThat(listToCheck.get(2)).isEqualByComparingTo(policyModel3);
169     }
170 }