2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.onap.policy.models.base.PfModelException;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.boot.test.context.SpringBootTest;
51 import org.springframework.boot.test.web.server.LocalServerPort;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
56 * Class to perform unit test of {@link ParticipantController}.
60 @ExtendWith(SpringExtension.class)
61 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
62 @ActiveProfiles({ "test", "default" })
63 class ParticipantControllerTest extends CommonRestController {
64 private static final String PARTICIPANTS_ENDPOINT = "participants";
67 private int randomServerPort;
69 private static final Coder CODER = new StandardCoder();
70 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
71 private static final String PARTICIPANT_JSON2 = "src/test/resources/providers/TestParticipant2.json";
73 private static final List<Participant> inputParticipants = new ArrayList<>();
74 private static final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
75 private static final String originalJson2 = ResourceUtils.getResourceAsString(PARTICIPANT_JSON2);
78 private ParticipantProvider participantProvider;
81 * Adds participants to the db from json file.
84 public static void setUpBeforeClass() throws CoderException {
85 inputParticipants.add(CODER.decode(originalJson, Participant.class));
86 inputParticipants.add(CODER.decode(originalJson2, Participant.class));
90 public void setUpPort() {
91 super.setHttpPrefix(randomServerPort);
96 super.testSwagger(PARTICIPANTS_ENDPOINT);
100 void testUnauthorizedQuery() {
101 assertUnauthorizedGet(PARTICIPANTS_ENDPOINT);
105 void testQueryParticipant() {
106 participantProvider.saveParticipant(inputParticipants.get(0));
107 UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
108 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participantId);
109 var response = invocationBuilder.buildGet().invoke();
110 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
111 var entityList = response.readEntity(ParticipantInformation.class);
112 assertNotNull(entityList);
116 void testBadQueryParticipant() {
117 participantProvider.saveParticipant(inputParticipants.get(0));
118 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + UUID.randomUUID());
119 var response = invocationBuilder.buildGet().invoke();
120 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
124 void getAllParticipants() {
125 inputParticipants.forEach(p -> {
126 participantProvider.saveParticipant(p);
128 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
129 var response = invocationBuilder.buildGet().invoke();
130 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
131 List<ParticipantInformation> entityList = response.readEntity(new GenericType<>() {});
132 assertThat(entityList).isNotEmpty();
133 var participantIds = entityList.stream().map(ParticipantInformation::getParticipant)
134 .map(Participant::getParticipantId).collect(Collectors.toSet());
135 inputParticipants.forEach(p -> {
136 assertThat(participantIds).contains(p.getParticipantId());
141 void testOrderParticipantReport() throws PfModelException {
142 participantProvider.saveParticipant(inputParticipants.get(0));
143 UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
144 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
147 var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
149 "", MediaType.APPLICATION_JSON));
150 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
154 void testBadOrderParticipantReport() throws PfModelException {
155 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
157 + UUID.randomUUID());
158 var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
160 "", MediaType.APPLICATION_JSON));
161 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
165 void testOrderAllParticipantReport() {
166 inputParticipants.forEach(p -> {
167 participantProvider.saveParticipant(p);
169 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
170 var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
172 "", MediaType.APPLICATION_JSON));
173 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());