aboutsummaryrefslogtreecommitdiffstats
path: root/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProviderTest.java
blob: 4b8617b3d7d7380ac7017dedc125784bf62374ae (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
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Optional;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ControlLoopRepository;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplateRepository;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplatesRepository;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;

class ControlLoopProviderTest {

    private static final String LIST_IS_NULL = "controlLoops is marked .*ull but is null";
    private static final String OBJECT_IS_NULL = "controlLoop is marked non-null but is null";

    private static final String ID_NAME = "PMSHInstance1";
    private static final String ID_VERSION = "1.0.1";
    private static final String ID_NAME_NOT_EXTST = "not_exist";
    private static final String ID_NAME_NOT_VALID = "not_valid";

    private static final Coder CODER = new StandardCoder();
    private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
    private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";

    private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();

    private ControlLoops inputControlLoops;
    private List<JpaControlLoop> inputControlLoopsJpa;
    private String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);

    @BeforeEach
    void beforeSetupDao() throws Exception {
        inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
        inputControlLoopsJpa = ProviderUtils.getJpaAndValidateList(inputControlLoops.getControlLoopList(),
                JpaControlLoop::new, "control loops");
    }

    @Test
    void testControlLoopsSave() throws Exception {
        var controlLoopRepository = mock(ControlLoopRepository.class);
        var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
                mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));

        assertThatThrownBy(() -> {
            controlLoopProvider.saveControlLoops(null);
        }).hasMessageMatching(LIST_IS_NULL);

        when(controlLoopRepository.saveAll(inputControlLoopsJpa)).thenReturn(inputControlLoopsJpa);

        var createdControlLoops = new ControlLoops();
        createdControlLoops
                .setControlLoopList(controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList()));

        assertEquals(inputControlLoops, createdControlLoops);

        when(controlLoopRepository.saveAll(any())).thenThrow(IllegalArgumentException.class);

        assertThatThrownBy(() -> {
            controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList());
        }).hasMessageMatching("Error in save ControlLoops");
    }

    @Test
    void testControlLoopSave() throws Exception {
        var controlLoopRepository = mock(ControlLoopRepository.class);
        var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
                mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));

        assertThatThrownBy(() -> {
            controlLoopProvider.saveControlLoop(null);
        }).hasMessageMatching(OBJECT_IS_NULL);

        when(controlLoopRepository.save(inputControlLoopsJpa.get(0))).thenReturn(inputControlLoopsJpa.get(0));

        var createdControlLoop = controlLoopProvider.saveControlLoop(inputControlLoops.getControlLoopList().get(0));

        assertEquals(inputControlLoops.getControlLoopList().get(0), createdControlLoop);

        when(controlLoopRepository.save(any())).thenThrow(IllegalArgumentException.class);

        assertThatThrownBy(() -> {
            controlLoopProvider.saveControlLoop(inputControlLoops.getControlLoopList().get(0));
        }).hasMessageMatching("Error in save controlLoop");
    }

    @Test
    void testGetControlLoops() throws Exception {
        var controlLoopRepository = mock(ControlLoopRepository.class);
        var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
                mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));

        // Return empty list when no data present in db
        List<ControlLoop> getResponse = controlLoopProvider.getControlLoops();
        assertThat(getResponse).isEmpty();

        controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList());

        var controlLoop0 = inputControlLoops.getControlLoopList().get(1);
        var name = controlLoop0.getName();
        var version = controlLoop0.getVersion();
        var controlLoop1 = inputControlLoops.getControlLoopList().get(1);

        when(controlLoopRepository.getFiltered(eq(JpaControlLoop.class), any(), any()))
                .thenReturn(List.of(new JpaControlLoop(controlLoop0), new JpaControlLoop(controlLoop1)));
        when(controlLoopRepository.findById(controlLoop0.getKey().asIdentifier().asConceptKey()))
                .thenReturn(Optional.of(new JpaControlLoop(controlLoop0)));
        when(controlLoopRepository.getById(controlLoop0.getKey().asIdentifier().asConceptKey()))
                .thenReturn(new JpaControlLoop(controlLoop0));
        when(controlLoopRepository.getFiltered(JpaControlLoop.class, name, version))
                .thenReturn(List.of(new JpaControlLoop(controlLoop0)));
        when(controlLoopRepository.findById(controlLoop1.getKey().asIdentifier().asConceptKey()))
                .thenReturn(Optional.of(new JpaControlLoop(controlLoop1)));

        assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());

        var cl = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME, ID_VERSION)).get();
        assertEquals(inputControlLoops.getControlLoopList().get(1), cl);

        cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME, ID_VERSION));
        assertEquals(inputControlLoops.getControlLoopList().get(1), cl);

        when(controlLoopRepository.getById(any())).thenThrow(EntityNotFoundException.class);

        assertThatThrownBy(() -> {
            controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION));
        }).hasMessageMatching("ControlLoop not found");

        cl = controlLoopProvider.findControlLoop(ID_NAME, ID_VERSION).get();
        assertEquals(inputControlLoops.getControlLoopList().get(1), cl);

        assertThat(controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
                .isEmpty();

        when(controlLoopRepository.findById(any())).thenThrow(IllegalArgumentException.class);

        assertThatThrownBy(() -> {
            controlLoopProvider.findControlLoop(ID_NAME_NOT_VALID, ID_VERSION);
        }).hasMessageMatching("Not valid parameter");
    }

    @Test
    void testDeleteControlLoop() throws Exception {
        var controlLoopRepository = mock(ControlLoopRepository.class);
        var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
                mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));

        assertThatThrownBy(() -> {
            controlLoopProvider.deleteControlLoop(ID_NAME_NOT_EXTST, ID_VERSION);
        }).hasMessageMatching(".*.failed, control loop does not exist");

        var controlLoop = inputControlLoops.getControlLoopList().get(0);
        var name = controlLoop.getName();
        var version = controlLoop.getVersion();

        when(controlLoopRepository.findById(new PfConceptKey(name, version)))
                .thenReturn(Optional.of(inputControlLoopsJpa.get(0)));

        ControlLoop deletedCl = controlLoopProvider.deleteControlLoop(name, version);
        assertEquals(controlLoop, deletedCl);
    }

    @Test
    void testDeleteAllInstanceProperties() throws Exception {
        var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
                mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
        var toscaServiceTemplate = testControlLoopRead();
        controlLoopProvider.deleteInstanceProperties(controlLoopProvider.saveInstanceProperties(toscaServiceTemplate),
                controlLoopProvider.getAllNodeTemplates());
        assertThat(controlLoopProvider.getControlLoops()).isEmpty();
    }

    @Test
    void testSaveAndDeleteInstanceProperties() throws Exception {
        var toscaNodeTemplatesRepository = mock(ToscaNodeTemplatesRepository.class);
        var toscaNodeTemplateRepository = mock(ToscaNodeTemplateRepository.class);
        var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
                toscaNodeTemplateRepository, toscaNodeTemplatesRepository);
        var toscaServiceTest = testControlLoopRead();

        controlLoopProvider.saveInstanceProperties(toscaServiceTest);
        verify(toscaNodeTemplatesRepository).save(any());

        var name = "org.onap.policy.controlloop.PolicyControlLoopParticipant";
        var version = "2.3.1";
        var elem = toscaServiceTest.getToscaTopologyTemplate().getNodeTemplates().get(name);
        when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version))
                .thenReturn(List.of(new JpaToscaNodeTemplate(elem)));

        var filtered = controlLoopProvider.getNodeTemplates(name, version);
        verify(toscaNodeTemplateRepository).getFiltered(JpaToscaNodeTemplate.class, name, version);

        controlLoopProvider.deleteInstanceProperties(controlLoopProvider.saveInstanceProperties(toscaServiceTest),
                filtered);

        verify(toscaNodeTemplateRepository).delete(any());
    }

    @Test
    void testGetNodeTemplates() throws Exception {
        var toscaNodeTemplateRepository = mock(ToscaNodeTemplateRepository.class);
        var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
                toscaNodeTemplateRepository, mock(ToscaNodeTemplatesRepository.class));

        var toscaNodeTemplate0 = new JpaToscaNodeTemplate(new PfConceptKey(ID_NAME, ID_VERSION));
        var toscaNodeTemplate1 = new JpaToscaNodeTemplate(new PfConceptKey("PMSHInstance2", ID_VERSION));

        when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, null, null))
                .thenReturn(List.of(toscaNodeTemplate0, toscaNodeTemplate1));
        when(toscaNodeTemplateRepository.findAll()).thenReturn(List.of(toscaNodeTemplate0, toscaNodeTemplate1));
        when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, ID_NAME, ID_VERSION))
                .thenReturn(List.of(toscaNodeTemplate0));

        // Getting all nodes
        var listNodes = controlLoopProvider.getAllNodeTemplates();
        assertNotNull(listNodes);
        assertThat(listNodes).hasSize(2);

        listNodes = controlLoopProvider.getNodeTemplates(ID_NAME, ID_VERSION);
        assertNotNull(listNodes);
        assertThat(listNodes).hasSize(1);

        listNodes = controlLoopProvider.getAllNodeTemplates();
        assertNotNull(listNodes);
        assertThat(listNodes).hasSize(2);

        var nodeTemplateFilter =
                ToscaTypedEntityFilter.<ToscaNodeTemplate>builder().name(ID_NAME).version(ID_VERSION).build();

        listNodes = controlLoopProvider.getFilteredNodeTemplates(nodeTemplateFilter);
        assertNotNull(listNodes);
        assertThat(listNodes).hasSize(1);

        assertThatThrownBy(() -> {
            controlLoopProvider.getFilteredNodeTemplates(null);
        }).hasMessageMatching("filter is marked non-null but is null");
    }

    private static ToscaServiceTemplate testControlLoopRead() {
        return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
    }

    private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
        var controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
        return yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
    }
}