f3aa176559c3c3d66837c785f86f32824416f621
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.dcae.httpclient;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.fail;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockserver.model.HttpRequest.request;
28 import static org.mockserver.model.HttpResponse.response;
29
30 import javax.ws.rs.core.MediaType;
31 import org.junit.jupiter.api.AfterAll;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockserver.integration.ClientAndServer;
36 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
38 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.springframework.test.context.junit.jupiter.SpringExtension;
42
43 /**
44  * Class to perform unit test of {@link ClampHttpClient}.
45  *
46  */
47 @ExtendWith(SpringExtension.class)
48 class ClampHttpClientTest {
49
50     private static final String LOOP = "pmsh_loop";
51     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
52
53     private static ClientAndServer mockServer;
54     private static ParticipantDcaeParameters parameters;
55     public static final Coder CODER = new StandardCoder();
56
57     /**
58      * Set up.
59      */
60     @BeforeAll
61     public static void setUp() {
62         CommonTestData commonTestData = new CommonTestData();
63
64         parameters = commonTestData.getParticipantDcaeParameters();
65
66         mockServer = ClientAndServer.startClientAndServer(parameters.getClampClientParameters().getPort());
67
68         mockServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
69                 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED)).withStatusCode(200)
70                         .withHeader("Content-Type", MediaType.APPLICATION_JSON));
71
72         mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
73                 .respond(response().withStatusCode(202));
74
75         mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
76                 .respond(response().withStatusCode(202));
77     }
78
79     @AfterAll
80     public static void stopServer() {
81         mockServer.stop();
82         mockServer = null;
83     }
84
85     @Test
86     void test_getstatus() throws Exception {
87         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
88
89             Loop status = client.getstatus(LOOP);
90
91             String json = CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED);
92             Loop loop = CODER.convert(json, Loop.class);
93
94             assertThat(ClampHttpClient.getStatusCode(status)).isEqualTo(ClampHttpClient.getStatusCode(loop));
95
96         } catch (Exception e) {
97             fail(e.getMessage());
98         }
99     }
100
101     @Test
102     void test_create() throws Exception {
103         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
104
105             assertThat(client.create(LOOP, null)).isNull();
106
107         } catch (Exception e) {
108             fail(e.getMessage());
109         }
110     }
111
112     @Test
113     void test_deploy() throws Exception {
114         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
115
116             assertTrue(client.deploy(LOOP));
117
118         } catch (Exception e) {
119             fail(e.getMessage());
120         }
121     }
122
123     @Test
124     void test_undeploy() throws Exception {
125         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
126
127             assertTrue(client.undeploy(LOOP));
128
129         } catch (Exception e) {
130             fail(e.getMessage());
131         }
132     }
133
134     @Test
135     void test_getStatusCodeNull() {
136         assertThat(ClampHttpClient.getStatusCode(null)).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
137     }
138
139     @Test
140     void test_getStatusEmptyMap() {
141         assertThat(ClampHttpClient.getStatusCode(new Loop())).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
142     }
143
144     @Test
145     void test_stop() throws Exception {
146         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
147
148             assertFalse(client.stop(LOOP));
149
150         } catch (Exception e) {
151             fail(e.getMessage());
152         }
153     }
154
155     @Test
156     void test_delete() throws Exception {
157         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
158
159             assertFalse(client.delete(LOOP));
160
161         } catch (Exception e) {
162             fail(e.getMessage());
163         }
164     }
165 }