7a86a49dcb3200041a080c1f5aba3bf4599e4355
[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.After;
31 import org.junit.Before;
32 import org.junit.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 public 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     /**
57      * Set up test Participant provider.
58      */
59     @Before
60     public void setupDao() throws Exception {
61
62         parameters = new PolicyModelsProviderParameters();
63         parameters.setDatabaseDriver("org.h2.Driver");
64         parameters.setName("PolicyProviderParameterGroup");
65         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
66         parameters.setDatabaseUrl("jdbc:h2:mem:participantProviderTestDb" + dbNameCounter.getAndIncrement());
67         parameters.setDatabaseUser("policy");
68         parameters.setDatabasePassword("P01icY");
69         parameters.setPersistenceUnit("ToscaConceptTest");
70
71         participantProvider = new ParticipantProvider(parameters);
72         inputParticipants.add(CODER.decode(originalJson, Participant.class));
73
74     }
75
76     @After
77     public void teardown() {
78         participantProvider.close();
79     }
80
81     @Test
82     public void testParticipantCreate() throws Exception {
83         assertThatThrownBy(() -> {
84             participantProvider.createParticipants(null);
85         }).hasMessageMatching(LIST_IS_NULL);
86
87         List<Participant> createdParticipants = new ArrayList<>();
88         createdParticipants.addAll(participantProvider
89             .createParticipants(inputParticipants));
90
91         assertEquals(createdParticipants.get(0),
92             inputParticipants.get(0));
93     }
94
95
96     @Test
97     public void testGetControlLoops() throws Exception {
98
99         List<Participant> getResponse;
100
101         //Return empty list when no data present in db
102         getResponse = participantProvider.getParticipants(null, null);
103         assertThat(getResponse).isEmpty();
104
105         participantProvider.createParticipants(inputParticipants);
106         String name = inputParticipants.get(0).getName();
107         String version = inputParticipants.get(0).getVersion();
108         assertEquals(1, participantProvider.getParticipants(name, version).size());
109
110         assertThat(participantProvider.getParticipants("invalid_name",
111             "1.0.1")).isEmpty();
112
113         assertThatThrownBy(() -> {
114             participantProvider.getFilteredParticipants(null);
115         }).hasMessageMatching("filter is marked .*ull but is null");
116
117         final ToscaTypedEntityFilter<Participant> filter = ToscaTypedEntityFilter.<Participant>builder()
118             .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
119         assertEquals(1, participantProvider.getFilteredParticipants(filter).size());
120     }
121
122     @Test
123     public void testUpdateParticipant() throws Exception {
124         assertThatThrownBy(() -> {
125             participantProvider.updateParticipants(null);
126         }).hasMessageMatching("participants is marked .*ull but is null");
127
128         participantProvider.createParticipants(inputParticipants);
129         updateParticipants = inputParticipants.get(0);
130         updateParticipants.setParticipantState(ParticipantState.ACTIVE);
131         List<Participant> participantList = new ArrayList<>();
132         participantList.add(updateParticipants);
133         List<Participant> updateResponse = new ArrayList<>();
134         updateResponse = participantProvider.updateParticipants(participantList);
135
136         assertEquals(ParticipantState.ACTIVE, updateResponse.get(0).getParticipantState());
137     }
138
139     @Test
140     public void testDeleteParticipant() throws Exception {
141         assertThatThrownBy(() -> {
142             participantProvider.deleteParticipant("Invalid_name", "1.0.1");
143         }).hasMessageMatching(".*.failed, participant does not exist");
144
145         Participant deletedParticipant;
146         List<Participant> participantList = participantProvider.createParticipants(inputParticipants);
147         String name = inputParticipants.get(0).getName();
148         String version = inputParticipants.get(0).getVersion();
149
150         deletedParticipant = participantProvider.deleteParticipant(name, version);
151         assertEquals(participantList.get(0), deletedParticipant);
152
153     }
154 }