5b12eee177b9b10c453823a246547ed06dc60352
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.persistence.provider;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.util.List;
32 import java.util.Optional;
33 import javax.persistence.EntityNotFoundException;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
38 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
39 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
40 import org.onap.policy.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45
46 class AutomationCompositionProviderTest {
47
48     private static final String OBJECT_IS_NULL = "automationComposition is marked non-null but is null";
49
50     private static final String ID_NAME = "PMSHInstance1";
51     private static final String ID_VERSION = "1.0.1";
52     private static final String ID_NAME_NOT_EXTST = "not_exist";
53
54     private static final Coder CODER = new StandardCoder();
55     private static final String AUTOMATION_COMPOSITION_JSON =
56             "src/test/resources/providers/TestAutomationCompositions.json";
57
58     private AutomationCompositions inputAutomationCompositions;
59     private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
60     private final String originalJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
61
62     @BeforeEach
63     void beforeSetupDao() throws Exception {
64         inputAutomationCompositions = CODER.decode(originalJson, AutomationCompositions.class);
65         inputAutomationCompositionsJpa =
66                 ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
67                         JpaAutomationComposition::new, "automation compositions");
68     }
69
70     @Test
71     void testAutomationCompositionSave() {
72         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
73         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
74
75         assertThatThrownBy(() -> automationCompositionProvider.saveAutomationComposition(null))
76                 .hasMessageMatching(OBJECT_IS_NULL);
77
78         when(automationCompositionRepository.save(inputAutomationCompositionsJpa.get(0)))
79                 .thenReturn(inputAutomationCompositionsJpa.get(0));
80
81         var createdAutomationComposition = automationCompositionProvider
82                 .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0));
83
84         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), createdAutomationComposition);
85     }
86
87     @Test
88     void testGetAutomationCompositions() throws Exception {
89         var automationComposition0 = inputAutomationCompositions.getAutomationCompositionList().get(1);
90         var name = automationComposition0.getName();
91         var version = automationComposition0.getVersion();
92         var automationComposition1 = inputAutomationCompositions.getAutomationCompositionList().get(1);
93
94         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
95         when(automationCompositionRepository.getFiltered(eq(JpaAutomationComposition.class), any(), any()))
96                 .thenReturn(List.of(new JpaAutomationComposition(automationComposition0),
97                         new JpaAutomationComposition(automationComposition1)));
98         when(automationCompositionRepository.findById(automationComposition0.getKey().asIdentifier().asConceptKey()))
99                 .thenReturn(Optional.of(new JpaAutomationComposition(automationComposition0)));
100         when(automationCompositionRepository.getById(automationComposition0.getKey().asIdentifier().asConceptKey()))
101                 .thenReturn(new JpaAutomationComposition(automationComposition0));
102         when(automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version))
103                 .thenReturn(List.of(new JpaAutomationComposition(automationComposition0)));
104         when(automationCompositionRepository.findById(automationComposition1.getKey().asIdentifier().asConceptKey()))
105                 .thenReturn(Optional.of(new JpaAutomationComposition(automationComposition1)));
106
107         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
108         assertEquals(1, automationCompositionProvider.getAutomationCompositions(name, version).size());
109
110         var ac = automationCompositionProvider
111                 .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME, ID_VERSION))
112                 .orElse(new AutomationComposition());
113         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
114
115         ac = automationCompositionProvider.getAutomationComposition(new ToscaConceptIdentifier(ID_NAME, ID_VERSION));
116         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
117
118         when(automationCompositionRepository.getById(any())).thenThrow(EntityNotFoundException.class);
119
120         assertThatThrownBy(() -> automationCompositionProvider
121                 .getAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
122                         .hasMessageMatching("AutomationComposition not found");
123
124         ac = automationCompositionProvider.findAutomationComposition(ID_NAME, ID_VERSION)
125                 .orElse(new AutomationComposition());
126         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
127
128         assertThat(automationCompositionProvider
129                 .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION))).isEmpty();
130     }
131
132     @Test
133     void testDeleteAutomationComposition() {
134         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
135         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
136
137         assertThatThrownBy(
138                 () -> automationCompositionProvider.deleteAutomationComposition(ID_NAME_NOT_EXTST, ID_VERSION))
139                         .hasMessageMatching(".*.failed, automation composition does not exist");
140
141         var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
142         var name = automationComposition.getName();
143         var version = automationComposition.getVersion();
144
145         when(automationCompositionRepository.findById(new PfConceptKey(name, version)))
146                 .thenReturn(Optional.of(inputAutomationCompositionsJpa.get(0)));
147
148         AutomationComposition deletedAc = automationCompositionProvider.deleteAutomationComposition(name, version);
149         assertEquals(automationComposition, deletedAc);
150     }
151 }