2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 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.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;
27 import jakarta.ws.rs.client.Entity;
28 import jakarta.ws.rs.core.GenericType;
29 import jakarta.ws.rs.core.MediaType;
30 import jakarta.ws.rs.core.Response;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.UUID;
34 import java.util.stream.Collectors;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.onap.policy.clamp.acm.runtime.main.rest.ParticipantController;
40 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
41 import org.onap.policy.clamp.models.acm.concepts.Participant;
42 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
43 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.common.utils.resources.ResourceUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.web.server.LocalServerPort;
51 import org.springframework.test.context.ActiveProfiles;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
55 * Class to perform unit test of {@link ParticipantController}.
59 @ExtendWith(SpringExtension.class)
60 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
61 @ActiveProfiles({ "test", "default" })
62 class ParticipantControllerTest extends CommonRestController {
63 private static final String PARTICIPANTS_ENDPOINT = "participants";
66 private int randomServerPort;
68 private static final Coder CODER = new StandardCoder();
69 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
70 private static final String PARTICIPANT_JSON2 = "src/test/resources/providers/TestParticipant2.json";
72 private static final List<Participant> inputParticipants = new ArrayList<>();
73 private static final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
74 private static final String originalJson2 = ResourceUtils.getResourceAsString(PARTICIPANT_JSON2);
77 private ParticipantProvider participantProvider;
80 * Adds participants to the db from json file.
83 public static void setUpBeforeClass() throws CoderException {
84 inputParticipants.add(CODER.decode(originalJson, Participant.class));
85 inputParticipants.add(CODER.decode(originalJson2, Participant.class));
89 public void setUpPort() {
90 super.setHttpPrefix(randomServerPort);
95 super.testSwagger(PARTICIPANTS_ENDPOINT);
99 void testUnauthorizedQuery() {
100 assertUnauthorizedGet(PARTICIPANTS_ENDPOINT);
104 void testQueryParticipant() {
105 participantProvider.saveParticipant(inputParticipants.get(0));
106 var participantId = participantProvider.getParticipants().get(0).getParticipantId();
107 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participantId);
108 try (var response = invocationBuilder.buildGet().invoke()) {
109 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
110 var entityList = response.readEntity(ParticipantInformation.class);
111 assertNotNull(entityList);
116 void testBadQueryParticipant() {
117 participantProvider.saveParticipant(inputParticipants.get(0));
118 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + UUID.randomUUID());
119 try (var response = invocationBuilder.buildGet().invoke()) {
120 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
125 void getAllParticipants() {
126 inputParticipants.forEach(p -> participantProvider.saveParticipant(p));
127 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
128 try (var response = invocationBuilder.buildGet().invoke()) {
129 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
130 List<ParticipantInformation> entityList = response.readEntity(new GenericType<>() {});
131 assertThat(entityList).isNotEmpty();
133 entityList.stream().map(ParticipantInformation::getParticipant).map(Participant::getParticipantId)
134 .collect(Collectors.toSet());
135 inputParticipants.forEach(p -> assertThat(participantIds).contains(p.getParticipantId()));
140 void testOrderParticipantReport() {
141 participantProvider.saveParticipant(inputParticipants.get(0));
142 var participantId = participantProvider.getParticipants().get(0).getParticipantId();
143 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
146 try (var response = invocationBuilder.header("Content-Length", 0)
147 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
148 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
153 void testBadOrderParticipantReport() {
154 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
156 + UUID.randomUUID());
157 try (var response = invocationBuilder.header("Content-Length", 0)
158 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
159 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
164 void testOrderAllParticipantReport() {
165 inputParticipants.forEach(p -> {
166 participantProvider.saveParticipant(p);
168 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
169 try (var response = invocationBuilder.header("Content-Length", 0)
170 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
171 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());