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.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;
41 public 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 * Set up test Participant provider.
60 public void setupDao() throws Exception {
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");
71 participantProvider = new ParticipantProvider(parameters);
72 inputParticipants.add(CODER.decode(originalJson, Participant.class));
77 public void teardown() {
78 participantProvider.close();
82 public void testParticipantCreate() throws Exception {
83 assertThatThrownBy(() -> {
84 participantProvider.createParticipants(null);
85 }).hasMessageMatching(LIST_IS_NULL);
87 List<Participant> createdParticipants = new ArrayList<>();
88 createdParticipants.addAll(participantProvider
89 .createParticipants(inputParticipants));
91 assertEquals(createdParticipants.get(0),
92 inputParticipants.get(0));
97 public void testGetControlLoops() throws Exception {
99 List<Participant> getResponse;
101 //Return empty list when no data present in db
102 getResponse = participantProvider.getParticipants(null, null);
103 assertThat(getResponse).isEmpty();
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());
110 assertThat(participantProvider.getParticipants("invalid_name",
113 assertThatThrownBy(() -> {
114 participantProvider.getFilteredParticipants(null);
115 }).hasMessageMatching("filter is marked .*ull but is null");
117 final ToscaTypedEntityFilter<Participant> filter = ToscaTypedEntityFilter.<Participant>builder()
118 .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
119 assertEquals(1, participantProvider.getFilteredParticipants(filter).size());
123 public void testUpdateParticipant() throws Exception {
124 assertThatThrownBy(() -> {
125 participantProvider.updateParticipants(null);
126 }).hasMessageMatching("participants is marked .*ull but is null");
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);
136 assertEquals(ParticipantState.ACTIVE, updateResponse.get(0).getParticipantState());
140 public void testDeleteParticipant() throws Exception {
141 assertThatThrownBy(() -> {
142 participantProvider.deleteParticipant("Invalid_name", "1.0.1");
143 }).hasMessageMatching(".*.failed, participant does not exist");
145 Participant deletedParticipant;
146 List<Participant> participantList = participantProvider.createParticipants(inputParticipants);
147 String name = inputParticipants.get(0).getName();
148 String version = inputParticipants.get(0).getVersion();
150 deletedParticipant = participantProvider.deleteParticipant(name, version);
151 assertEquals(participantList.get(0), deletedParticipant);