aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/test/java/org/onap/policy/clamp/loop/LoopServiceTestItCase.java
blob: 74092727cf5ad00d02bfb6827f48b534460792df (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2019 Nokia Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * 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.JsonObject;
import java.util.Set;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import org.assertj.core.api.Assertions;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.policy.clamp.clds.Application;
import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.clamp.loop.log.LogType;
import org.onap.policy.clamp.loop.log.LoopLog;
import org.onap.policy.clamp.loop.log.LoopLogService;
import org.onap.policy.clamp.loop.template.LoopTemplate;
import org.onap.policy.clamp.loop.template.PolicyModel;
import org.onap.policy.clamp.loop.template.PolicyModelsService;
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.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class LoopServiceTestItCase {

    private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
    private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";

    @Autowired
    LoopService loopService;

    @Autowired
    LoopsRepository loopsRepository;

    @Autowired
    MicroServicePolicyService microServicePolicyService;

    @Autowired
    OperationalPolicyService operationalPolicyService;

    @Autowired
    LoopLogService loopLogService;

    @Autowired
    PolicyModelsService policyModelsService;

    @Test
    @Transactional
    public void shouldCreateEmptyLoop() {
        // given
        String loopBlueprint = "blueprint";
        Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint);
        testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
        testLoop.setLastComputedState(LoopState.DESIGN);

        // when
        Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
                .isEqualTo("testValue");
    }

    @Test
    @Transactional
    public void shouldAddOperationalPolicyToLoop() {
        // given
        saveTestLoopToDb();
        OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);

        // when
        Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
                Lists.newArrayList(operationalPolicy));

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
        assertThat(savedPolicies).hasSize(1);
        assertThat(savedPolicies)
                .usingElementComparatorIgnoringFields("loop", "createdBy", "createdDate", "updatedBy", "updatedDate")
                .contains(operationalPolicy);
        OperationalPolicy savedPolicy = savedPolicies.iterator().next();
        Assertions.assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);

    }

    @Test
    @Transactional
    public void shouldAddMicroservicePolicyToLoop() {
        // given
        saveTestLoopToDb();
        PolicyModel policyModel = new PolicyModel("org.policies.policyModel1",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyModel1");
        policyModelsService.saveOrUpdatePolicyModel(policyModel);
        MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
                false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);

        // when
        Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
                Lists.newArrayList(microServicePolicy));

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
        assertThat(savedPolicies).hasSize(1);
        assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
                "createdBy", "updatedBy").containsExactly(microServicePolicy);
        assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);

    }

    @Test
    @Transactional
    //@Commit
    public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
        // given
        saveTestLoopToDb();
        PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
        policyModelsService.saveOrUpdatePolicyModel(policyModel1);
        PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
        policyModelsService.saveOrUpdatePolicyModel(policyModel2);
        MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1, false,
                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);

        loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
        MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2, false,
                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);

        // when
        firstMicroServicePolicy
                .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
        Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
                Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
        assertThat(savedPolicies).hasSize(2);
        assertThat(savedPolicies).contains(firstMicroServicePolicy);
        assertThat(savedPolicies).contains(secondMicroServicePolicy);
        assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
                "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
    }

    private void saveTestLoopToDb() {
        Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint");
        testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
        LoopTemplate template = new LoopTemplate();
        template.setName("testTemplate");
        testLoop.setLoopTemplate(template);
        loopService.saveOrUpdateLoop(testLoop);
    }

    @Test
    @Transactional
    public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
        // given
        saveTestLoopToDb();
        PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
        policyModelsService.saveOrUpdatePolicyModel(policyModel1);
        PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
        policyModelsService.saveOrUpdatePolicyModel(policyModel2);
        MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1,
                false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
        loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));

        MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2,
                false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);

        // when
        Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
                Lists.newArrayList(secondMicroServicePolicy));

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
        assertThat(savedPolicies).hasSize(1);
        assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
                "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);

    }

    @Test
    @Transactional
    public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
        // given
        saveTestLoopToDb();

        JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);

        OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
        loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));

        OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
                newJsonConfiguration, null, null, null, null);

        // when
        firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
        Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
                Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
        assertThat(savedPolicies).hasSize(2);
        assertThat(savedPolicies)
                .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
                .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
        Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
                .map(Loop::getName).collect(Collectors.toSet());
        assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
    }

    @Test
    @Transactional
    public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
        // given
        saveTestLoopToDb();

        OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
        loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));

        OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
                JsonUtils.GSON.fromJson("{}", JsonObject.class), null, null, "pdpGroup1", "pdpSubgroup1");

        // when
        Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
                Lists.newArrayList(secondOperationalPolicy));

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
        assertThat(savedPolicies).hasSize(1);
        assertThat(savedPolicies)
                .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
                .containsExactly(secondOperationalPolicy);
        OperationalPolicy savedPolicy = savedPolicies.iterator().next();
        Assertions.assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);

    }

    @Test
    @Transactional
    public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
        // given
        saveTestLoopToDb();
        String expectedJson = "{\"test\":\"test\"}";
        JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
        JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
        loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);

        // when
        Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);

        // then
        assertThat(actualLoop).isNotNull();
        assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
        JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
        assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
    }

    @Test
    @Transactional
    public void deleteAttempt() {
        saveTestLoopToDb();
        // Add log
        Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
        loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
        LoopTemplate template = new LoopTemplate();
        template.setName("testTemplate");
        loop.setLoopTemplate(template);
        loop = loopService.saveOrUpdateLoop(loop);
        // Add op policy
        OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
        loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));

        PolicyModel policyModel = new PolicyModel("org.policies.microPolicy",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "microPolicy");
        policyModelsService.saveOrUpdatePolicyModel(policyModel);
        // Add Micro service policy
        MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", policyModel,
                false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
        loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));

        // Verify it's there
        assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
        loopService.deleteLoop(EXAMPLE_LOOP_NAME);
        // Verify it's well deleted and has been cascaded, except for Microservice
        assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
        assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
        assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
        assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
    }

    @Test
    @Transactional
    public void testUpdateLoopState() {
        saveTestLoopToDb();
        Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
        loopService.updateLoopState(loop, "SUBMITTED");
        Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
        assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
    }

    @Test
    @Transactional
    public void testUpdateDcaeDeploymentFields() {
        saveTestLoopToDb();
        Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
        loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
                "https://deployment-handler.onap:8443");
        loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
        assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
        assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https://deployment-handler.onap:8443");
    }

    @Test
    @Transactional
    public void testUpdateMicroservicePolicy() {
        saveTestLoopToDb();
        assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
        PolicyModel policyModel = new PolicyModel("org.policies.policyName",
                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyName");
        policyModelsService.saveOrUpdatePolicyModel(policyModel);
        MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
                false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
        loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
        assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
    }

    private Loop createTestLoop(String loopName, String loopBlueprint) {
        return new Loop(loopName);
    }
}