681ca96ceebd2c5a4a06a532296730495adca16b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.controlloop.models.controlloop.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.Assert.assertEquals;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
40
41 class ParticipantProviderTest {
42
43     private static final Coder CODER = new StandardCoder();
44     private static final String PARTICIPANT_JSON =
45             "src/test/resources/providers/TestParticipant.json";
46     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
47
48     private static AtomicInteger dbNameCounter = new AtomicInteger();
49
50     private PolicyModelsProviderParameters parameters;
51     private ParticipantProvider participantProvider;
52     private List<Participant> inputParticipants = new ArrayList<>();
53     private Participant updateParticipants;
54     private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
55
56     @BeforeEach
57     void beforeSetupDao() throws Exception {
58
59         parameters = new PolicyModelsProviderParameters();
60         parameters.setDatabaseDriver("org.h2.Driver");
61         parameters.setName("PolicyProviderParameterGroup");
62         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
63         parameters.setDatabaseUrl("jdbc:h2:mem:participantProviderTestDb" + dbNameCounter.getAndIncrement());
64         parameters.setDatabaseUser("policy");
65         parameters.setDatabasePassword("P01icY");
66         parameters.setPersistenceUnit("ToscaConceptTest");
67
68         participantProvider = new ParticipantProvider(parameters);
69         inputParticipants.add(CODER.decode(originalJson, Participant.class));
70
71     }
72
73     @AfterEach
74     void teardown() {
75         participantProvider.close();
76     }
77
78     @Test
79     void testParticipantCreate() throws Exception {
80         assertThatThrownBy(() -> {
81             participantProvider.createParticipants(null);
82         }).hasMessageMatching(LIST_IS_NULL);
83
84         List<Participant> createdParticipants = new ArrayList<>();
85         createdParticipants.addAll(participantProvider
86                 .createParticipants(inputParticipants));
87
88         assertEquals(createdParticipants.get(0),
89                 inputParticipants.get(0));
90     }
91
92
93     @Test
94     void testGetControlLoops() throws Exception {
95
96         List<Participant> getResponse;
97
98         //Return empty list when no data present in db
99         getResponse = participantProvider.getParticipants(null, null);
100         assertThat(getResponse).isEmpty();
101
102         participantProvider.createParticipants(inputParticipants);
103         String name = inputParticipants.get(0).getName();
104         String version = inputParticipants.get(0).getVersion();
105         assertEquals(1, participantProvider.getParticipants(name, version).size());
106
107         assertThat(participantProvider.getParticipants("invalid_name",
108                 "1.0.1")).isEmpty();
109
110         assertThatThrownBy(() -> {
111             participantProvider.getFilteredParticipants(null);
112         }).hasMessageMatching("filter is marked .*ull but is null");
113
114         final ToscaTypedEntityFilter<Participant> filter = ToscaTypedEntityFilter.<Participant>builder()
115                 .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
116         assertEquals(1, participantProvider.getFilteredParticipants(filter).size());
117     }
118
119     @Test
120     void testUpdateParticipant() throws Exception {
121         assertThatThrownBy(() -> {
122             participantProvider.updateParticipants(null);
123         }).hasMessageMatching("participants is marked .*ull but is null");
124
125         participantProvider.createParticipants(inputParticipants);
126         updateParticipants = inputParticipants.get(0);
127         updateParticipants.setParticipantState(ParticipantState.ACTIVE);
128         List<Participant> participantList = new ArrayList<>();
129         participantList.add(updateParticipants);
130         List<Participant> updateResponse = new ArrayList<>();
131         updateResponse = participantProvider.updateParticipants(participantList);
132
133         assertEquals(ParticipantState.ACTIVE, updateResponse.get(0).getParticipantState());
134     }
135
136     @Test
137     void testDeleteParticipant() throws Exception {
138         assertThatThrownBy(() -> {
139             participantProvider.deleteParticipant("Invalid_name", "1.0.1");
140         }).hasMessageMatching(".*.failed, participant does not exist");
141
142         Participant deletedParticipant;
143         List<Participant> participantList = participantProvider.createParticipants(inputParticipants);
144         String name = inputParticipants.get(0).getName();
145         String version = inputParticipants.get(0).getVersion();
146
147         deletedParticipant = participantProvider.deleteParticipant(name, version);
148         assertEquals(participantList.get(0), deletedParticipant);
149
150     }
151 }