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 java.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.core.GenericType;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.onap.policy.clamp.acm.runtime.main.rest.ParticipantController;
39 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
40 import org.onap.policy.clamp.models.acm.concepts.Participant;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantInformation;
42 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
43 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
44 import org.onap.policy.clamp.models.acm.persistence.provider.ProviderUtils;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.common.utils.resources.ResourceUtils;
49 import org.onap.policy.models.base.PfModelException;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.web.server.LocalServerPort;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit.jupiter.SpringExtension;
57 * Class to perform unit test of {@link ParticipantController}.
61 @ExtendWith(SpringExtension.class)
62 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
63 @ActiveProfiles({ "test", "default" })
64 class ParticipantControllerTest extends CommonRestController {
65 private static final String PARTICIPANTS_ENDPOINT = "participants";
68 private int randomServerPort;
70 private static final Coder CODER = new StandardCoder();
71 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
72 private static final String PARTICIPANT_JSON2 = "src/test/resources/providers/TestParticipant2.json";
73 private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
75 private static final List<Participant> inputParticipants = new ArrayList<>();
76 private static List<JpaParticipant> jpaParticipantList;
77 private static final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
78 private static final String originalJson2 = ResourceUtils.getResourceAsString(PARTICIPANT_JSON2);
81 private ParticipantProvider participantProvider;
84 * Adds participants to the db from json file.
87 public static void setUpBeforeClass() throws CoderException {
88 inputParticipants.add(CODER.decode(originalJson, Participant.class));
89 inputParticipants.add(CODER.decode(originalJson2, Participant.class));
90 jpaParticipantList = ProviderUtils.getJpaAndValidateList(
91 inputParticipants, JpaParticipant::new, "participant");
95 public void setUpPort() {
96 super.setHttpPrefix(randomServerPort);
101 super.testSwagger(PARTICIPANTS_ENDPOINT);
105 void testUnauthorizedQuery() {
106 assertUnauthorizedGet(PARTICIPANTS_ENDPOINT);
110 void testQueryParticipant() {
111 participantProvider.saveParticipant(inputParticipants.get(0));
112 UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
113 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participantId);
114 var response = invocationBuilder.buildGet().invoke();
115 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
116 var entityList = response.readEntity(ParticipantInformation.class);
117 assertNotNull(entityList);
121 void testBadQueryParticipant() {
122 participantProvider.saveParticipant(inputParticipants.get(0));
123 UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
124 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + UUID.randomUUID());
125 var response = invocationBuilder.buildGet().invoke();
126 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
130 void getAllParticipants() {
131 inputParticipants.forEach(p -> {
132 participantProvider.saveParticipant(p);
134 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
135 var response = invocationBuilder.buildGet().invoke();
136 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
137 List<ParticipantInformation> entityList = response.readEntity(new GenericType<>() {});
138 assertThat(entityList.size() == inputParticipants.size());
142 void testOrderParticipantReport() throws PfModelException {
143 participantProvider.saveParticipant(inputParticipants.get(0));
144 UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
145 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
148 var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
150 "", MediaType.APPLICATION_JSON));
151 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
155 void testBadOrderParticipantReport() throws PfModelException {
156 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
158 + UUID.randomUUID());
159 var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
161 "", MediaType.APPLICATION_JSON));
162 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
166 void testOrderAllParticipantReport() {
167 inputParticipants.forEach(p -> {
168 participantProvider.saveParticipant(p);
170 var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
171 var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
173 "", MediaType.APPLICATION_JSON));
174 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());