Modify the template model
[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.setUpdatedBy(createdBy);
70         policyModel.setVersion(version);
71         return policyModel;
72     }
73
74     @Test
75     @Transactional
76     public void shouldCreatePolicyModel() {
77         // given
78         PolicyModel policyModel = getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", POLICY_MODEL_TYPE_1_VERSION_1, "TEST",
79                 "VARIANT", "user");
80
81         // when
82         PolicyModel actualPolicyModel = policyModelsService.saveOrUpdatePolicyModel(policyModel);
83
84         // then
85         assertThat(actualPolicyModel).isNotNull();
86         assertThat(actualPolicyModel).isEqualTo(policyModelsRepository
87                 .findById(new PolicyModelId(actualPolicyModel.getPolicyModelType(), actualPolicyModel.getVersion()))
88                 .get());
89         assertThat(actualPolicyModel.getPolicyModelType()).isEqualTo(policyModel.getPolicyModelType());
90         assertThat(actualPolicyModel.getCreatedBy()).isEqualTo("");
91         assertThat(actualPolicyModel.getCreatedDate()).isNotNull();
92         assertThat(actualPolicyModel.getPolicyAcronym()).isEqualTo(policyModel.getPolicyAcronym());
93         assertThat(actualPolicyModel.getPolicyModelTosca()).isEqualTo(policyModel.getPolicyModelTosca());
94         assertThat(actualPolicyModel.getUpdatedBy()).isEqualTo("");
95         assertThat(actualPolicyModel.getUpdatedDate()).isNotNull();
96         assertThat(actualPolicyModel.getVersion()).isEqualTo(policyModel.getVersion());
97
98         assertThat(policyModelsService.getPolicyModel(POLICY_MODEL_TYPE_1, POLICY_MODEL_TYPE_1_VERSION_1))
99                 .isEqualToIgnoringGivenFields(policyModel, "createdDate", "updatedDate", "createdBy", "updatedBy");
100     }
101
102     @Test
103     @Transactional
104     public void shouldReturnAllPolicyModelTypes() {
105         // given
106         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
107                 "VARIANT", "user");
108         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
109         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
110                 "VARIANT", "user");
111         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
112         List<String> policyModelTypesList = policyModelsService.getAllPolicyModelTypes();
113
114         assertThat(policyModelTypesList).containsOnly(policyModel1.getPolicyModelType(),
115                 policyModel2.getPolicyModelType());
116     }
117
118     @Test
119     @Transactional
120     public void shouldReturnAllPolicyModels() {
121         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
122                 "VARIANT", "user");
123         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
124         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
125                 "VARIANT", "user");
126         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
127
128         assertThat(policyModelsService.getAllPolicyModels()).containsOnly(policyModel1, policyModel2);
129     }
130
131     @Test
132     @Transactional
133     public void shouldReturnAllModelsByType() {
134         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
135                 "VARIANT", "user");
136         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
137         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
138                 "VARIANT", "user");
139         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
140
141         assertThat(policyModelsService.getAllPolicyModelsByType(POLICY_MODEL_TYPE_2)).containsOnly(policyModel1,
142                 policyModel2);
143     }
144
145     @Test
146     @Transactional
147     public void shouldReturnSortedSet() {
148         PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST",
149                 "VARIANT", "user");
150         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
151         PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST",
152                 "VARIANT", "user");
153         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
154
155         SortedSet<PolicyModel> sortedSet = new TreeSet<>();
156         policyModelsService.getAllPolicyModels().forEach(sortedSet::add);
157         assertThat(sortedSet).containsExactly(policyModel2, policyModel1);
158     }
159 }