761d9c795b2bcf41973999ee4d6dea6037deb6aa
[policy/clamp.git] /
1 /*-
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
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 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.provider.ParticipantProvider;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.resources.ResourceUtils;
47 import org.onap.policy.models.base.PfModelException;
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         UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
107         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participantId);
108         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     @Test
115     void testBadQueryParticipant() {
116         participantProvider.saveParticipant(inputParticipants.get(0));
117         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + UUID.randomUUID());
118         var response = invocationBuilder.buildGet().invoke();
119         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
120     }
121
122     @Test
123     void getAllParticipants() {
124         inputParticipants.forEach(p -> {
125             participantProvider.saveParticipant(p);
126         });
127         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
128         var response = invocationBuilder.buildGet().invoke();
129         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
130         List<ParticipantInformation> entityList = response.readEntity(new GenericType<>() {});
131         assertThat(entityList.size() == inputParticipants.size());
132     }
133
134     @Test
135     void testOrderParticipantReport() throws PfModelException {
136         participantProvider.saveParticipant(inputParticipants.get(0));
137         UUID participantId = participantProvider.getParticipants().get(0).getParticipantId();
138         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
139             + "/"
140             + participantId);
141         var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
142             +
143             "", MediaType.APPLICATION_JSON));
144         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
145     }
146
147     @Test
148     void testBadOrderParticipantReport() throws PfModelException {
149         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT
150             + "/"
151             + UUID.randomUUID());
152         var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
153             +
154             "", MediaType.APPLICATION_JSON));
155         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
156     }
157
158     @Test
159     void testOrderAllParticipantReport() {
160         inputParticipants.forEach(p -> {
161             participantProvider.saveParticipant(p);
162         });
163         var invocationBuilder = super.sendRequest(PARTICIPANTS_ENDPOINT);
164         var response = invocationBuilder.header("Content-Length", 0).put(Entity.entity(""
165             +
166             "", MediaType.APPLICATION_JSON));
167         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
168     }
169 }