2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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.acm.runtime.participant;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
28 import jakarta.ws.rs.client.Entity;
29 import jakarta.ws.rs.core.GenericType;
30 import jakarta.ws.rs.core.MediaType;
31 import jakarta.ws.rs.core.Response;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.UUID;
35 import java.util.stream.Collectors;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.onap.policy.clamp.acm.runtime.instantiation.AutomationCompositionInstantiationProvider;
41 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
42 import org.onap.policy.clamp.acm.runtime.main.rest.ParticipantController;
43 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
44 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
45 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
46 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
47 import org.onap.policy.clamp.models.acm.concepts.Participant;
48 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
49 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
50 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.common.utils.resources.ResourceUtils;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.boot.test.web.server.LocalServerPort;
59 import org.springframework.test.context.ActiveProfiles;
60 import org.springframework.test.context.junit.jupiter.SpringExtension;
63 * Class to perform unit test of {@link ParticipantController}.
67 @ExtendWith(SpringExtension.class)
68 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
69 @ActiveProfiles({ "test", "default" })
70 class ParticipantControllerTest extends CommonRestController {
71 private static final String PARTICIPANTS_ENDPOINT = "participants";
72 private static final int NUMBER_RECORDS = 10;
75 private AcDefinitionProvider acDefinitionProvider;
78 private AutomationCompositionInstantiationProvider instantiationProvider;
81 private int randomServerPort;
83 private static final Coder CODER = new StandardCoder();
84 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
85 private static final String PARTICIPANT_JSON2 = "src/test/resources/providers/TestParticipant2.json";
86 private static final String AC_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/acm/AutomationComposition.json";
87 private static final String NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
88 private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
89 private static AutomationComposition automationComposition = new AutomationComposition();
90 private static final List<Participant> inputParticipants = new ArrayList<>();
91 private static final String ORIGINAL_JSON = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
92 private static final String ORIGINAL_JSON2 = ResourceUtils.getResourceAsString(PARTICIPANT_JSON2);
95 private ParticipantProvider participantProvider;
98 * Adds participants to the db from json file.
101 public static void setUpBeforeClass() throws CoderException {
102 inputParticipants.add(CODER.decode(ORIGINAL_JSON, Participant.class));
103 inputParticipants.add(CODER.decode(ORIGINAL_JSON2, Participant.class));
104 serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
105 automationComposition =
106 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_CREATE_JSON, "Query");
110 public void setUpPort() {
111 super.setHttpPrefix(randomServerPort);
116 super.testSwagger(PARTICIPANTS_ENDPOINT);
120 void testUnauthorizedQuery() {
121 assertUnauthorizedGet(PARTICIPANTS_ENDPOINT);
125 void testQueryParticipant() {
126 var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
127 var replica = CommonTestData.createParticipantReplica(CommonTestData.getParticipantId());
128 participant.getReplicas().put(replica.getReplicaId(), replica);
129 participantProvider.saveParticipant(participant);
130 for (var i = 0; i < NUMBER_RECORDS; i++) {
131 createAcDefinitionInDB("QueryParticipant" + i);
134 validateParticipantPageable("?page=1&size=4", 4);
136 // not valid pagination
137 validateParticipantNotPageable("?page=0", NUMBER_RECORDS);
138 validateParticipantNotPageable("?size=5", NUMBER_RECORDS);
139 validateParticipantNotPageable("", NUMBER_RECORDS);
142 private void validateParticipantPageable(String url, int size) {
143 var participantInfo = getParticipantInformation(url);
144 assertThat(participantInfo.getAcNodeTemplateStateDefinitionMap()).hasSize(size);
145 assertThat(participantInfo.getAcElementInstanceMap()).hasSize(size);
148 private void validateParticipantNotPageable(String url, int size) {
149 var participantInfo = getParticipantInformation(url);
150 assertThat(participantInfo.getAcNodeTemplateStateDefinitionMap()).hasSizeGreaterThanOrEqualTo(size);
151 assertThat(participantInfo.getAcElementInstanceMap()).hasSizeGreaterThanOrEqualTo(size);
154 private ParticipantInformation getParticipantInformation(String url) {
155 var invocationBuilder = super.sendRequest(
156 PARTICIPANTS_ENDPOINT + "/" + CommonTestData.getParticipantId() + url);
157 try (var response = invocationBuilder.buildGet().invoke()) {
158 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
159 var participantInfo = response.readEntity(ParticipantInformation.class);
160 assertNotNull(participantInfo);
161 return participantInfo;
166 void testBadQueryParticipant() {
167 participantProvider.saveParticipant(inputParticipants.get(0));
168 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + UUID.randomUUID());
169 try (var response = invocationBuilder.buildGet().invoke()) {
170 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
175 void getAllParticipants() {
176 inputParticipants.forEach(participantProvider::saveParticipant);
177 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
178 try (var response = invocationBuilder.buildGet().invoke()) {
179 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
180 List<ParticipantInformation> entityList = response.readEntity(new GenericType<>() {
182 assertThat(entityList).isNotEmpty();
184 entityList.stream().map(ParticipantInformation::getParticipant).map(Participant::getParticipantId)
185 .collect(Collectors.toSet());
186 inputParticipants.forEach(p -> assertThat(participantIds).contains(p.getParticipantId()));
188 var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
189 var replica = CommonTestData.createParticipantReplica(CommonTestData.getParticipantId());
190 participant.getReplicas().put(replica.getReplicaId(), replica);
191 participantProvider.saveParticipant(participant);
192 for (var i = 0; i < NUMBER_RECORDS; i++) {
193 createAcDefinitionInDB("AllParticipants" + i);
196 validateAllParticipantsPageable("?page=1&size=4", 4);
198 // not valid pagination
199 validateAllParticipantsNotPageable("?page=0", NUMBER_RECORDS);
200 validateAllParticipantsNotPageable("?size=5", NUMBER_RECORDS);
201 validateAllParticipantsNotPageable("", NUMBER_RECORDS);
204 private void validateAllParticipantsNotPageable(String url, int size) {
205 var participantInfo = getFirstParticipantInformation(url);
206 assertThat(participantInfo.getAcNodeTemplateStateDefinitionMap()).hasSizeGreaterThanOrEqualTo(size);
207 assertThat(participantInfo.getAcElementInstanceMap()).hasSizeGreaterThanOrEqualTo(size);
210 private void validateAllParticipantsPageable(String url, int size) {
211 var participantInfo = getFirstParticipantInformation(url);
212 assertThat(participantInfo.getAcNodeTemplateStateDefinitionMap()).hasSize(size);
213 assertThat(participantInfo.getAcElementInstanceMap()).hasSize(size);
216 private ParticipantInformation getFirstParticipantInformation(String url) {
217 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + url);
218 try (var response = invocationBuilder.buildGet().invoke()) {
219 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
220 List<ParticipantInformation> entityList = response.readEntity(new GenericType<>() {});
221 var participantInfoOpt = entityList.stream()
222 .filter(p -> CommonTestData.getParticipantId().equals(p.getParticipant().getParticipantId()))
224 assertThat(participantInfoOpt).isPresent();
225 return participantInfoOpt.get();
229 private void createAcDefinitionInDB(String name) {
230 var serviceTemplateCreate = new ToscaServiceTemplate(serviceTemplate);
231 serviceTemplateCreate.setName(name);
232 var acmDefinition = CommonTestData.createAcDefinition(serviceTemplateCreate, AcTypeState.PRIMED);
233 acDefinitionProvider.updateAcDefinition(acmDefinition, NODE_TYPE);
234 var automationCompositionCreate = new AutomationComposition(automationComposition);
235 automationCompositionCreate.setCompositionId(acmDefinition.getCompositionId());
236 automationCompositionCreate.setName(acmDefinition.getServiceTemplate().getName());
237 var elements = new ArrayList<>(automationCompositionCreate.getElements().values());
238 automationCompositionCreate.getElements().clear();
239 for (var element : elements) {
240 element.setId(UUID.randomUUID());
241 automationCompositionCreate.getElements().put(element.getId(), element);
243 instantiationProvider
244 .createAutomationComposition(acmDefinition.getCompositionId(), automationCompositionCreate);
248 void testOrderParticipantReport() {
249 participantProvider.saveParticipant(inputParticipants.get(0));
250 var participantId = participantProvider.getParticipants().get(0).getParticipantId();
251 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
254 try (var response = invocationBuilder.header("Content-Length", 0)
255 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
256 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
261 void testBadOrderParticipantReport() {
262 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
264 + UUID.randomUUID());
265 try (var response = invocationBuilder.header("Content-Length", 0)
266 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
267 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
272 void testOrderAllParticipantReport() {
273 inputParticipants.forEach(participantProvider::saveParticipant);
274 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
275 try (var response = invocationBuilder.header("Content-Length", 0)
276 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
277 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());