aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/policy/clamp/loop/LoopRepositoriesItCase.java
blob: e18dd24756c592b79a6d821576f0958b0b92f734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.policy.clamp.loop;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.time.Instant;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.policy.clamp.clds.Application;
import org.onap.policy.clamp.loop.log.LogType;
import org.onap.policy.clamp.loop.log.LoopLog;
import org.onap.policy.clamp.loop.log.LoopLogRepository;
import org.onap.policy.clamp.loop.service.Service;
import org.onap.policy.clamp.loop.service.ServicesRepository;
import org.onap.policy.clamp.loop.template.LoopElementModel;
import org.onap.policy.clamp.loop.template.LoopElementModelsRepository;
import org.onap.policy.clamp.loop.template.LoopTemplate;
import org.onap.policy.clamp.loop.template.LoopTemplatesRepository;
import org.onap.policy.clamp.loop.template.PolicyModel;
import org.onap.policy.clamp.loop.template.PolicyModelId;
import org.onap.policy.clamp.loop.template.PolicyModelsRepository;
import org.onap.policy.clamp.policy.microservice.MicroServicePolicy;
import org.onap.policy.clamp.policy.microservice.MicroServicePolicyService;
import org.onap.policy.clamp.policy.operational.OperationalPolicy;
import org.onap.policy.clamp.policy.operational.OperationalPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class LoopRepositoriesItCase {

    private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();

    @Autowired
    private LoopsRepository loopRepository;

    @Autowired
    private MicroServicePolicyService microServicePolicyService;

    @Autowired
    private OperationalPolicyService operationalPolicyService;

    @Autowired
    private LoopLogRepository loopLogRepository;

    @Autowired
    private LoopTemplatesRepository loopTemplateRepository;

    @Autowired
    private LoopElementModelsRepository microServiceModelsRepository;

    @Autowired
    private PolicyModelsRepository policyModelsRepository;

    @Autowired
    private ServicesRepository servicesRepository;

    private Service getService(String serviceDetails, String resourceDetails) {
        return new Service(serviceDetails, resourceDetails);
    }

    private OperationalPolicy getOperationalPolicy(String configJson, String name, PolicyModel policyModel) {
        return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class), policyModel,
                null, null, null);
    }

    private LoopElementModel getLoopElementModel(String yaml, String name, String policyType, String createdBy,
                                                 PolicyModel policyModel) {
        LoopElementModel model = new LoopElementModel(name, policyType, yaml);
        model.addPolicyModel(policyModel);
        return model;
    }

    private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version,
                                       String policyAcronym) {
        return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
    }

    private LoopTemplate getLoopTemplates(String name, String blueprint, String createdBy,
                                          Integer maxInstancesAllowed) {
        LoopTemplate template = new LoopTemplate(name, blueprint, maxInstancesAllowed, null);
        template.addLoopElementModel(getLoopElementModel("yaml", "microService1", "org.onap.policy.drools", createdBy,
                getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools")));
        template.addLoopElementModel(getLoopElementModel("yaml", "oppolicy1", "org.onap.policy.drools.legacy",
                createdBy, getPolicyModel("org.onap.policy.drools.legacy", "yaml", "1.0.0", "DroolsLegacy")));
        loopTemplateRepository.save(template);
        return template;
    }

    private Loop getLoop(String name, String blueprint, String globalPropertiesJson,
                         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
        Loop loop = new Loop();
        loop.setName(name);
        loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
        loop.setLastComputedState(LoopState.DESIGN);
        loop.setDcaeDeploymentId(dcaeId);
        loop.setDcaeDeploymentStatusUrl(dcaeUrl);
        loop.setLoopTemplate(getLoopTemplates("templateName", "yaml", "toto", 1));
        return loop;
    }

    private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String jsonProperties,
                                                     boolean shared, PolicyModel policyModel) {
        MicroServicePolicy microService = new MicroServicePolicy(name, policyModel, shared,
                gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
        microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
        return microService;
    }

    private LoopLog getLoopLog(LogType type, String message, Loop loop) {
        return new LoopLog(message, type, "CLAMP", loop);
    }

    /**
     * This method does a crud test and save a loop template and a loop object in db.
     */
    @Test
    @Transactional
    public void crudTest() {
        // Setup
        Loop loopTest = getLoop("ControlLoopTest", "yamlcontent", "{\"testname\":\"testvalue\"}",
                "123456789", "https://dcaetest.org", "UUID-blueprint");
        OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest",
                getPolicyModel("org.onap.policy.drools.legacy", "yaml", "1.0.0", "DroolsLegacy"));
        loopTest.addOperationalPolicy(opPolicy);
        MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
                "{\"param1\":\"value1\"}", true, getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools"));
        loopTest.addMicroServicePolicy(microServicePolicy);
        LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
        loopTest.addLog(loopLog);
        Service service = getService(
                "{\"name\": \"vLoadBalancerMS\", \"UUID\": \"63cac700-ab9a-4115-a74f-7eac85e3fce0\"}", "{\"CP\": {}}");
        loopTest.setModelService(service);

        // Attempt to save into the database the entire loop
        Loop loopInDb = loopRepository.save(loopTest);
        assertThat(loopInDb).isNotNull();
        assertThat(loopRepository.findById(loopInDb.getName()).get()).isNotNull();
        assertThat(loopInDb.getCreatedDate()).isNotNull();
        assertThat(loopInDb.getUpdatedDate()).isNotNull();
        assertThat(loopInDb.getUpdatedDate()).isEqualTo(loopInDb.getCreatedDate());
        assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
        // Autogen id so now set the ID in the previous model so that we can compare the
        // objects
        loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());

        assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
                "createdBy", "updatedBy");
        assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
        assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
        assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
        assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
        assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
        assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
        assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
        assertThat(microServiceModelsRepository.existsById(
                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
                .isEqualTo(true);
        assertThat(policyModelsRepository.existsById(new PolicyModelId(
                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
                        .first().getPolicyModelType(),
                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
                        .first().getVersion()))).isEqualTo(true);

        // Now attempt to read from database
        Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
        assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
                "createdBy", "updatedBy");
        assertThat(loopInDbRetrieved).isEqualToComparingOnlyGivenFields(loopInDb, "createdDate", "updatedDate",
                "createdBy", "updatedBy");
        assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
        assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
                .isEqualToIgnoringGivenFields(opPolicy, "createdDate", "updatedDate", "createdBy", "updatedBy");
        Assertions.assertThat(
                ((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedDate())
                .isNotNull();
        Assertions.assertThat(
                ((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedDate())
                .isNotNull();
        Assertions.assertThat(
                ((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedBy())
                .isNotNull();
        Assertions.assertThat(
                ((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedBy())
                .isNotNull();

        assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
                .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy",
                        "updatedBy");

        // Attempt an update
        ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
        loopInDbRetrieved.setLastComputedState(LoopState.RUNNING);
        Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved);
        // Loop loopInDbRetrievedUpdated =
        // loopRepository.findById(loopTest.getName()).get();
        assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
                .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
        // UpdatedDate should have been changed
        assertThat(loopInDbRetrievedUpdated.getUpdatedDate()).isNotEqualTo(loopInDbRetrievedUpdated.getCreatedDate());
        // createdDate should have NOT been changed
        assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate());
        // other audit are the same
        assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo("Not found");
        assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo("Not found");

        // Attempt to delete the object and check it has well been cascaded

        loopRepository.delete(loopInDbRetrieved);
        assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
        assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
        assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
        assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
        assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
        assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
        assertThat(microServiceModelsRepository.existsById(
                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
                .isEqualTo(true);

        assertThat(policyModelsRepository.existsById(new PolicyModelId(
                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
                        .first().getPolicyModelType(),
                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
                        .first().getVersion()))).isEqualTo(true);

    }
}