Merge "Add template and tosca model entities and repositories"
[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
32 import javax.transaction.Transactional;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.clamp.clds.Application;
37 import org.onap.clamp.loop.template.PolicyModel;
38 import org.onap.clamp.loop.template.PolicyModelId;
39 import org.onap.clamp.loop.template.PolicyModelsRepository;
40 import org.onap.clamp.loop.template.PolicyModelsService;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.junit4.SpringRunner;
44
45 @RunWith(SpringRunner.class)
46 @SpringBootTest(classes = Application.class)
47 public class PolicyModelServiceItCase {
48
49     @Autowired
50     PolicyModelsService policyModelsService;
51
52     @Autowired
53     PolicyModelsRepository policyModelsRepository;
54
55     private static final String POLICY_MODEL_TYPE_1 = "org.onap.test";
56     private static final String POLICY_MODEL_TYPE_1_VERSION_1 = "1.0.0";
57
58     private static final String POLICY_MODEL_TYPE_2 = "org.onap.test2";
59     private static final String POLICY_MODEL_TYPE_2_VERSION_1 = "1.0.0";
60     private static final String POLICY_MODEL_TYPE_2_VERSION_2 = "2.0.0";
61
62     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
63             String policyVariant, String createdBy) {
64         PolicyModel policyModel = new PolicyModel();
65         policyModel.setCreatedBy(createdBy);
66         policyModel.setPolicyAcronym(policyAcronym);
67         policyModel.setPolicyModelTosca(policyModelTosca);
68         policyModel.setPolicyModelType(policyType);
69         policyModel.setPolicyVariant(policyVariant);
70         policyModel.setUpdatedBy(createdBy);
71         policyModel.setVersion(version);
72         return policyModel;
73     }
74
75     @Test
76     @Transactional
77     public void shouldCreatePolicyModel() {
78         // given
79         PolicyModel policyModel = getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", POLICY_MODEL_TYPE_1_VERSION_1, "TEST",
80                 "VARIANT", "user");
81
82         // when
83         PolicyModel actualPolicyModel = policyModelsService.saveOrUpdatePolicyModel(policyModel);
84
85         // then
86         assertThat(actualPolicyModel).isNotNull();
87         assertThat(actualPolicyModel).isEqualTo(policyModelsRepository
88                 .findById(new PolicyModelId(actualPolicyModel.getPolicyModelType(), actualPolicyModel.getVersion()))
89                 .get());
90         assertThat(actualPolicyModel.getPolicyModelType()).isEqualTo(policyModel.getPolicyModelType());
91         assertThat(actualPolicyModel.getCreatedBy()).isEqualTo("");
92         assertThat(actualPolicyModel.getCreatedDate()).isNotNull();
93         assertThat(actualPolicyModel.getPolicyAcronym()).isEqualTo(policyModel.getPolicyAcronym());
94         assertThat(actualPolicyModel.getPolicyModelTosca()).isEqualTo(policyModel.getPolicyModelTosca());
95         assertThat(actualPolicyModel.getPolicyVariant()).isEqualTo(policyModel.getPolicyVariant());
96         assertThat(actualPolicyModel.getUpdatedBy()).isEqualTo("");
97         assertThat(actualPolicyModel.getUpdatedDate()).isNotNull();
98         assertThat(actualPolicyModel.getVersion()).isEqualTo(policyModel.getVersion());
99
100         assertThat(policyModelsService.getPolicyModel(POLICY_MODEL_TYPE_1, POLICY_MODEL_TYPE_1_VERSION_1))
101                 .isEqualToIgnoringGivenFields(policyModel, "createdDate", "updatedDate", "createdBy", "updatedBy");
102     }
103
104     @Test
105     @Transactional
106     public void shouldReturnAllPolicyModelTypes() {
107         // given
108         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
109                 "VARIANT", "user");
110         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
111         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
112                 "VARIANT", "user");
113         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
114         List<String> policyModelTypesList = policyModelsService.getAllPolicyModelTypes();
115
116         assertThat(policyModelTypesList).containsOnly(policyModel1.getPolicyModelType(),
117                 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()).containsOnly(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)).containsOnly(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
157         SortedSet<PolicyModel> sortedSet = new TreeSet<>();
158         policyModelsService.getAllPolicyModels().forEach(sortedSet::add);
159         assertThat(sortedSet).containsExactly(policyModel2, policyModel1);
160     }
161 }