4842a77a8b5dc74f3bb41602f3cf6b2fcc59294e
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.participant;
22
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
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;
53
54 /**
55  * Class to perform unit test of {@link ParticipantController}.
56  *
57  */
58
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";
64
65     @LocalServerPort
66     private int randomServerPort;
67
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";
71
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);
75
76     @Autowired
77     private ParticipantProvider participantProvider;
78
79     /**
80      * Adds participants to the db from json file.
81      */
82     @BeforeAll
83     public static void setUpBeforeClass() throws CoderException {
84         inputParticipants.add(CODER.decode(originalJson, Participant.class));
85         inputParticipants.add(CODER.decode(originalJson2, Participant.class));
86     }
87
88     @BeforeEach
89     public void setUpPort() {
90         super.setHttpPrefix(randomServerPort);
91     }
92
93     @Test
94     void testSwagger() {
95         super.testSwagger(PARTICIPANTS_ENDPOINT);
96     }
97
98     @Test
99     void testUnauthorizedQuery() {
100         assertUnauthorizedGet(PARTICIPANTS_ENDPOINT);
101     }
102
103     @Test
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);
112         }
113     }
114
115     @Test
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());
121         }
122     }
123
124     @Test
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();
132             var participantIds =
133                     entityList.stream().map(ParticipantInformation::getParticipant).map(Participant::getParticipantId)
134                             .collect(Collectors.toSet());
135             inputParticipants.forEach(p -> assertThat(participantIds).contains(p.getParticipantId()));
136         }
137     }
138
139     @Test
140     void testOrderParticipantReport() {
141         participantProvider.saveParticipant(inputParticipants.get(0));
142         var participantId = participantProvider.getParticipants().get(0).getParticipantId();
143         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
144             + "/"
145             + participantId);
146         try (var response = invocationBuilder.header("Content-Length", 0)
147                 .put(Entity.entity("", MediaType.APPLICATION_JSON))) {
148             assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
149         }
150     }
151
152     @Test
153     void testBadOrderParticipantReport() {
154         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
155             + "/"
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());
160         }
161     }
162
163     @Test
164     void testOrderAllParticipantReport() {
165         inputParticipants.forEach(p -> {
166             participantProvider.saveParticipant(p);
167         });
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());
172         }
173     }
174 }