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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
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;
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;
41 class ParticipantProviderTest {
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";
48 private static AtomicInteger dbNameCounter = new AtomicInteger();
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);
57 void beforeSetupDao() throws Exception {
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");
68 participantProvider = new ParticipantProvider(parameters);
69 inputParticipants.add(CODER.decode(originalJson, Participant.class));
75 participantProvider.close();
79 void testParticipantCreate() throws Exception {
80 assertThatThrownBy(() -> {
81 participantProvider.createParticipants(null);
82 }).hasMessageMatching(LIST_IS_NULL);
84 List<Participant> createdParticipants = new ArrayList<>();
85 createdParticipants.addAll(participantProvider
86 .createParticipants(inputParticipants));
88 assertEquals(createdParticipants.get(0),
89 inputParticipants.get(0));
94 void testGetControlLoops() throws Exception {
96 List<Participant> getResponse;
98 //Return empty list when no data present in db
99 getResponse = participantProvider.getParticipants(null, null);
100 assertThat(getResponse).isEmpty();
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());
107 assertThat(participantProvider.getParticipants("invalid_name",
110 assertThatThrownBy(() -> {
111 participantProvider.getFilteredParticipants(null);
112 }).hasMessageMatching("filter is marked .*ull but is null");
114 final ToscaTypedEntityFilter<Participant> filter = ToscaTypedEntityFilter.<Participant>builder()
115 .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
116 assertEquals(1, participantProvider.getFilteredParticipants(filter).size());
120 void testUpdateParticipant() throws Exception {
121 assertThatThrownBy(() -> {
122 participantProvider.updateParticipants(null);
123 }).hasMessageMatching("participants is marked .*ull but is null");
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);
133 assertEquals(ParticipantState.ACTIVE, updateResponse.get(0).getParticipantState());
137 void testDeleteParticipant() throws Exception {
138 assertThatThrownBy(() -> {
139 participantProvider.deleteParticipant("Invalid_name", "1.0.1");
140 }).hasMessageMatching(".*.failed, participant does not exist");
142 Participant deletedParticipant;
143 List<Participant> participantList = participantProvider.createParticipants(inputParticipants);
144 String name = inputParticipants.get(0).getName();
145 String version = inputParticipants.get(0).getVersion();
147 deletedParticipant = participantProvider.deleteParticipant(name, version);
148 assertEquals(participantList.get(0), deletedParticipant);