Get policy in CsarInstaller
[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
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.PolicyModelId;
40 import org.onap.clamp.loop.template.PolicyModelsRepository;
41 import org.onap.clamp.loop.template.PolicyModelsService;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.test.context.junit4.SpringRunner;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = Application.class)
48 public class PolicyModelServiceItCase {
49
50     @Autowired
51     PolicyModelsService policyModelsService;
52
53     @Autowired
54     PolicyModelsRepository policyModelsRepository;
55
56     private static final String POLICY_MODEL_TYPE_1 = "org.onap.testos";
57     private static final String POLICY_MODEL_TYPE_1_VERSION_1 = "1.0.0";
58
59     private static final String POLICY_MODEL_TYPE_2 = "org.onap.testos2";
60     private static final String POLICY_MODEL_TYPE_3 = "org.onap.testos3";
61     private static final String POLICY_MODEL_TYPE_2_VERSION_1 = "1.0.0";
62     private static final String POLICY_MODEL_TYPE_3_VERSION_1 = "1.0.0";
63     private static final String POLICY_MODEL_TYPE_2_VERSION_2 = "2.0.0";
64
65     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
66             String policyVariant, String createdBy) {
67         PolicyModel policyModel = new PolicyModel();
68         policyModel.setCreatedBy(createdBy);
69         policyModel.setPolicyAcronym(policyAcronym);
70         policyModel.setPolicyModelTosca(policyModelTosca);
71         policyModel.setPolicyModelType(policyType);
72         policyModel.setUpdatedBy(createdBy);
73         policyModel.setVersion(version);
74         return policyModel;
75     }
76
77     @Test
78     @Transactional
79     public void shouldCreatePolicyModel() {
80         // given
81         PolicyModel policyModel = getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", POLICY_MODEL_TYPE_1_VERSION_1, "TEST",
82                 "VARIANT", "user");
83
84         // when
85         PolicyModel actualPolicyModel = policyModelsService.saveOrUpdatePolicyModel(policyModel);
86
87         // then
88         assertThat(actualPolicyModel).isNotNull();
89         assertThat(actualPolicyModel).isEqualTo(policyModelsRepository
90                 .findById(new PolicyModelId(actualPolicyModel.getPolicyModelType(), actualPolicyModel.getVersion()))
91                 .get());
92         assertThat(actualPolicyModel.getPolicyModelType()).isEqualTo(policyModel.getPolicyModelType());
93         assertThat(actualPolicyModel.getCreatedBy()).isEqualTo("");
94         assertThat(actualPolicyModel.getCreatedDate()).isNotNull();
95         assertThat(actualPolicyModel.getPolicyAcronym()).isEqualTo(policyModel.getPolicyAcronym());
96         assertThat(actualPolicyModel.getPolicyModelTosca()).isEqualTo(policyModel.getPolicyModelTosca());
97         assertThat(actualPolicyModel.getUpdatedBy()).isEqualTo("");
98         assertThat(actualPolicyModel.getUpdatedDate()).isNotNull();
99         assertThat(actualPolicyModel.getVersion()).isEqualTo(policyModel.getVersion());
100
101         assertThat(policyModelsService.getPolicyModel(POLICY_MODEL_TYPE_1, POLICY_MODEL_TYPE_1_VERSION_1))
102                 .isEqualToIgnoringGivenFields(policyModel, "createdDate", "updatedDate", "createdBy", "updatedBy");
103     }
104
105     @Test
106     @Transactional
107     public void shouldReturnAllPolicyModelTypes() {
108         // given
109         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
110                 "VARIANT", "user");
111         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
112         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
113                 "VARIANT", "user");
114         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
115         List<String> policyModelTypesList = policyModelsService.getAllPolicyModelTypes();
116
117         assertThat(policyModelTypesList).contains(policyModel1.getPolicyModelType(), policyModel2.getPolicyModelType());
118     }
119
120     @Test
121     @Transactional
122     public void shouldReturnAllPolicyModels() {
123         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
124                 "VARIANT", "user");
125         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
126         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
127                 "VARIANT", "user");
128         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
129
130         assertThat(policyModelsService.getAllPolicyModels()).contains(policyModel1, policyModel2);
131     }
132
133     @Test
134     @Transactional
135     public void shouldReturnAllModelsByType() {
136         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
137                 "VARIANT", "user");
138         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
139         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
140                 "VARIANT", "user");
141         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
142
143         assertThat(policyModelsService.getAllPolicyModelsByType(POLICY_MODEL_TYPE_2)).contains(policyModel1,
144                 policyModel2);
145     }
146
147     @Test
148     @Transactional
149     public void shouldReturnSortedSet() {
150         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
151                 "VARIANT", "user");
152         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
153         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
154                 "VARIANT", "user");
155         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
156         PolicyModel policyModel3 = getPolicyModel(POLICY_MODEL_TYPE_3, "yaml", POLICY_MODEL_TYPE_3_VERSION_1, "TEST",
157                 "VARIANT", "user");
158         policyModelsService.saveOrUpdatePolicyModel(policyModel3);
159
160         SortedSet<PolicyModel> sortedSet = new TreeSet<>();
161         policyModelsService.getAllPolicyModels().forEach(sortedSet::add);
162         List<PolicyModel> listToCheck = sortedSet.stream().filter(
163             policy -> policy.equals(policyModel3) || policy.equals(policyModel2) || policy.equals(policyModel1))
164                 .collect(Collectors.toList());
165         assertThat(listToCheck.get(0)).isEqualByComparingTo(policyModel2);
166         assertThat(listToCheck.get(1)).isEqualByComparingTo(policyModel1);
167         assertThat(listToCheck.get(2)).isEqualByComparingTo(policyModel3);
168     }
169 }