7adc88d09fe484ac837c426dc2616a4fdd46588b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.controlloop.participant.simulator.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
39 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
40 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43
44 /**
45  * Class to perform unit test of {@link TestSimulationRestController}.
46  */
47 public class TestSimulationRestController extends CommonParticipantRestServer {
48     private static final String PARTICIPANTS_ENDPOINT = "participants";
49     private static final String ELEMENTS_ENDPOINT = "elements";
50     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
51     private static final String TOPIC = "my-topic";
52     static CommonTestData commonTestData = new CommonTestData();
53     private static RestController restController;
54
55     /**
56      * Setup before class, instantiate SimulationProvider.
57      *
58      */
59     @BeforeClass
60     public static void setUpBeforeClass() throws Exception {
61         CommonParticipantRestServer.setUpBeforeClass();
62     }
63
64     @AfterClass
65     public static void teardownAfterClass() {
66         CommonParticipantRestServer.teardownAfterClass();
67     }
68
69     @Test
70     public void testSwagger() throws Exception {
71         super.testSwagger(ELEMENTS_ENDPOINT);
72     }
73
74     @Test
75     public void testQuery_Unauthorized() throws Exception {
76         Invocation.Builder invocationBuilder = super.sendNoAuthRequest(ELEMENTS_ENDPOINT);
77         Response rawresp = invocationBuilder.buildGet().invoke();
78         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
79     }
80
81     @Test
82     public void testQueryParticipants() throws Exception {
83         Participant participant = new Participant();
84         ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
85         participant.setDefinition(participantId);
86         participant.setName(participantId.getName());
87         participant.setVersion(participantId.getVersion());
88
89         // GET REST call for querying the participants
90         Invocation.Builder invocationBuilder =
91                 super.sendRequest(PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName()
92                 + "/" + participant.getVersion());
93
94         Response rawresp = invocationBuilder.buildGet().invoke();
95         // Response is not OK, as handling is not done
96         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
97     }
98
99     @Test
100     public void testQueryControlLoopElements() throws Exception {
101         // GET REST call for querying the controlLoop elements
102         Invocation.Builder invocationBuilder =
103                 super.sendRequest(ELEMENTS_ENDPOINT + "/" + "PMSHInstance0" + "/" + "1.0.0");
104
105         Response rawresp = invocationBuilder.buildGet().invoke();
106         // Response is not OK, as handling is not done
107         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
108     }
109
110     @Test
111     public void testUpdateParticipant() throws Exception {
112         Participant participant = new Participant();
113         ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
114         participant.setDefinition(participantId);
115         participant.setName(participantId.getName());
116         participant.setVersion(participantId.getVersion());
117
118         List<Participant> participants = new ArrayList<>();
119         participants.add(participant);
120         assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
121         // Change the state of the participant to PASSIVE from UNKNOWN
122         participants.get(0).setParticipantState(ParticipantState.PASSIVE);
123         Entity<Participant> entParticipant = Entity.entity(participants.get(0), MediaType.APPLICATION_JSON);
124
125         // PUT REST call for updating Participant
126         Invocation.Builder invocationBuilder = sendRequest(PARTICIPANTS_ENDPOINT);
127         Response rawresp = invocationBuilder.put(entParticipant);
128         TypedSimpleResponse<Participant> resp = rawresp.readEntity(TypedSimpleResponse.class);
129         // Response is not OK, as handling is not done
130         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
131     }
132
133     @Test
134     public void testUpdateControlLoopElement() throws Exception {
135         ControlLoop controlLoop = TestListenerUtils.createControlLoop();
136         List<ControlLoopElement> controlLoopElements = controlLoop.getElements();
137
138         // Check the initial state on the ControlLoopElement, which is UNINITIALISED
139         assertEquals(ControlLoopOrderedState.UNINITIALISED, controlLoopElements.get(0).getOrderedState());
140
141         // Change the state of the ControlLoopElement to PASSIVE from UNINITIALISED
142         controlLoopElements.get(0).setOrderedState(ControlLoopOrderedState.PASSIVE);
143         Entity<ControlLoopElement> entClElement = Entity.entity(controlLoopElements.get(0), MediaType.APPLICATION_JSON);
144
145         // PUT REST call for updating ControlLoopElement
146         Invocation.Builder invocationBuilder = sendRequest(ELEMENTS_ENDPOINT);
147         Response rawresp = invocationBuilder.put(entClElement);
148         TypedSimpleResponse<ControlLoopElement> resp = rawresp.readEntity(TypedSimpleResponse.class);
149         // Response is not OK, as handling is not done
150         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
151     }
152 }
153