cafaf051d991b8cbc6d9d02fe62f697faa48a998
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023, 2025 OpenInfra Foundation Europe. All rights reserved.
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.contract;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import jakarta.ws.rs.client.Entity;
26 import jakarta.ws.rs.core.Response;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
32 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
35 import org.springframework.boot.test.web.server.LocalServerPort;
36 import org.springframework.test.context.ActiveProfiles;
37 import org.springframework.test.context.junit.jupiter.SpringExtension;
38
39 @ExtendWith(SpringExtension.class)
40 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
41 @ActiveProfiles({ "test", "stub" })
42 class InstantiationControllerStubTest extends CommonRestController {
43
44     private static final String COMMISSIONING_ENDPOINT = "compositions";
45     private static final String INSTANTIATION_ENDPOINT = "instances";
46     private static final String COMPOSITION_ID = "1aeed185-a98b-45b6-af22-8d5d20485ea3";
47     private static final String INSTANCE_ID = "709c62b3-8918-41b9-a747-d21eb79c6c23";
48     private static final String ROLLBACK = "rollback";
49
50     @LocalServerPort
51     private int randomServerPort;
52
53     @BeforeEach
54     void setUpPort() {
55         super.setHttpPrefix(randomServerPort);
56     }
57
58     @Test
59     void testQuery() {
60         var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
61                 + "/" + COMPOSITION_ID
62                 + "/" + INSTANTIATION_ENDPOINT);
63         var response = invocationBuilder.get();
64         assertThat(Response.Status.OK.getStatusCode()).isEqualTo(response.getStatus());
65     }
66
67     @Test
68     void testGet() {
69         var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
70                 + "/" + COMPOSITION_ID
71                 + "/" + INSTANTIATION_ENDPOINT
72                 + "/" + INSTANCE_ID);
73         var response = invocationBuilder.get();
74         assertThat(Response.Status.OK.getStatusCode()).isEqualTo(response.getStatus());
75     }
76
77     @Test
78     void testPut() {
79         var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
80                 + "/" + COMPOSITION_ID
81                 + "/" + INSTANTIATION_ENDPOINT
82                 + "/" + INSTANCE_ID);
83         var put = invocationBuilder.put(Entity.json(new AcInstanceStateUpdate()));
84         assertThat(Response.Status.ACCEPTED.getStatusCode()).isEqualTo(put.getStatus());
85         put.close();
86     }
87
88     @Test
89     void testPost() {
90         var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
91                 + "/" + COMPOSITION_ID
92                 + "/" + INSTANTIATION_ENDPOINT);
93         var respPost = invocationBuilder.post(Entity.json(new AutomationComposition()));
94         assertThat(Response.Status.OK.getStatusCode()).isEqualTo(respPost.getStatus());
95         respPost.close();
96     }
97
98     @Test
99     void testDelete() {
100         var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
101                 + "/" + COMPOSITION_ID
102                 + "/" + INSTANTIATION_ENDPOINT
103                 + "/" + INSTANCE_ID);
104         var delete = invocationBuilder.delete();
105         assertThat(Response.Status.OK.getStatusCode()).isEqualTo(delete.getStatus());
106         delete.close();
107     }
108
109     @Test
110     void testRollback() {
111         var invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT
112             + "/" + COMPOSITION_ID
113             + "/" + INSTANTIATION_ENDPOINT
114             + "/" + INSTANCE_ID
115             + "/" + ROLLBACK);
116         var respPost = invocationBuilder.post(Entity.json(new AcInstanceStateUpdate()));
117         assertThat(Response.Status.ACCEPTED.getStatusCode()).isEqualTo(respPost.getStatus());
118         respPost.close();
119     }
120
121     @Test
122     void testGetInstances() {
123         var invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT);
124         var response = invocationBuilder.get();
125         assertThat(Response.Status.OK.getStatusCode()).isEqualTo(response.getStatus());
126     }
127 }