aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/policy/clamp/loop/LoopTemplatesServiceItCase.java
blob: d608075fca94d1d00517c24342406e7cccc02454 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 AT&T 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 static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.SortedSet;
import javax.transaction.Transactional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.policy.clamp.clds.Application;
import org.onap.policy.clamp.loop.template.LoopElementModel;
import org.onap.policy.clamp.loop.template.LoopTemplate;
import org.onap.policy.clamp.loop.template.LoopTemplateLoopElementModel;
import org.onap.policy.clamp.loop.template.LoopTemplatesService;
import org.onap.policy.clamp.loop.template.LoopType;
import org.onap.policy.clamp.loop.template.PolicyModel;
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 LoopTemplatesServiceItCase {

    @Autowired
    LoopTemplatesService loopTemplatesService;

    private static final String POLICY_MODEL_TYPE_1 = "org.onap.test";
    private static final String VERSION = "1.0.0";

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

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

    private LoopTemplate getLoopTemplate(String name, String blueprint, String createdBy, Integer maxInstancesAllowed) {
        LoopTemplate template =
                new LoopTemplate(name, blueprint, maxInstancesAllowed, null);
        template.addLoopElementModel(getLoopElementModel("yaml", "microService1", "MicroService",
                createdBy, getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", VERSION, "MS1", createdBy)));
        template.setAllowedLoopType(LoopType.OPEN);
        return template;
    }

    @Test
    @Transactional
    public void shouldSaveOrUpdateLoopTemplate() {
        LoopTemplate loopTemplate = getLoopTemplate("TemplateName", null, "xyz", -1);
        LoopTemplate actualLoopTemplate =
                loopTemplatesService.saveOrUpdateLoopTemplate(loopTemplate);

        assertNotNull(actualLoopTemplate);
        assertThat(loopTemplate.getName()).isEqualTo("TemplateName");
        assertThat(loopTemplate.getAllowedLoopType()).isEqualTo(LoopType.OPEN);
    }

    @Test
    @Transactional
    public void shouldReturnAllLoopTemplates() {
        LoopTemplate loopTemplate = getLoopTemplate("TemplateName", null, "xyz", -1);
        loopTemplatesService.saveOrUpdateLoopTemplate(loopTemplate);
        List<LoopTemplate> loopTemplateList = loopTemplatesService.getAllLoopTemplates();

        assertNotNull(loopTemplateList);
    }

    @Test
    @Transactional
    public void shouldReturnLoopTemplateNames() {
        LoopTemplate loopTemplate = getLoopTemplate("TemplateName", null, "xyz", -1);
        loopTemplatesService.saveOrUpdateLoopTemplate(loopTemplate);
        List<String> loopTemplateNames = loopTemplatesService.getLoopTemplateNames();

        assertNotNull(loopTemplateNames);
        assertEquals("TemplateName", loopTemplateNames.get(0));
    }

    @Test
    @Transactional
    public void shouldReturnLoopTemplate() {
        LoopTemplate loopTemplate = getLoopTemplate("TemplateName", null, "xyz", -1);
        loopTemplatesService.saveOrUpdateLoopTemplate(loopTemplate);
        LoopTemplate actualLoopTemplate = loopTemplatesService.getLoopTemplate("TemplateName");

        assertNotNull(actualLoopTemplate);
        assertThat(loopTemplate).isEqualTo(actualLoopTemplate);
        assertThat(loopTemplate.getName()).isEqualTo(actualLoopTemplate.getName());
        assertThat(loopTemplate.getMaximumInstancesAllowed())
                .isEqualTo(actualLoopTemplate.getMaximumInstancesAllowed());
        SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed =
                loopTemplate.getLoopElementModelsUsed();
        LoopTemplateLoopElementModel loopTemplateLoopElementModel = loopElementModelsUsed.first();
        assertThat(loopTemplateLoopElementModel.getLoopElementModel().getName())
                .isEqualTo("microService1");
        assertThat(loopTemplateLoopElementModel.getLoopTemplate().getName())
                .isEqualTo("TemplateName");
        assertNull(actualLoopTemplate.getBlueprint());
        assertNull(actualLoopTemplate.getModelService());
    }

    @Test
    @Transactional
    public void shouldDeleteLoopTemplate() {
        LoopTemplate loopTemplate = getLoopTemplate("TemplateName", null, "xyz", -1);
        loopTemplatesService.saveOrUpdateLoopTemplate(loopTemplate);
        loopTemplatesService.deleteLoopTemplate("TemplateName");
        LoopTemplate actualLoopTemplate = loopTemplatesService.getLoopTemplate("TemplateName");
        assertNull(actualLoopTemplate);
    }

}