6e375222eda764b09a05b28f7bb8aa67f761e9dd
[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.assertTrue;
26 import static org.mockserver.model.HttpRequest.request;
27 import static org.mockserver.model.HttpResponse.response;
28
29 import javax.ws.rs.core.MediaType;
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockserver.integration.ClientAndServer;
35 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
37 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
38 import org.onap.policy.common.utils.coder.Coder;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.springframework.test.context.junit.jupiter.SpringExtension;
41
42 /**
43  * Class to perform unit test of {@link ClampHttpClient}.
44  *
45  */
46 @ExtendWith(SpringExtension.class)
47 class ClampHttpClientTest {
48
49     private static final String LOOP = "pmsh_loop";
50     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
51
52     private static ClientAndServer mockServer;
53     private static ParticipantDcaeParameters parameters;
54     public static final Coder CODER = new StandardCoder();
55
56     /**
57      * Set up.
58      */
59     @BeforeAll
60     public static void setUp() {
61         CommonTestData commonTestData = new CommonTestData();
62
63         parameters = commonTestData.toObject(
64                 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
65                 ParticipantDcaeParameters.class);
66
67         mockServer = ClientAndServer.startClientAndServer(parameters.getClampClientParameters().getPort());
68
69         mockServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
70                 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED)).withStatusCode(200)
71                         .withHeader("Content-Type", MediaType.APPLICATION_JSON));
72
73         mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
74                 .respond(response().withStatusCode(202));
75
76         mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
77                 .respond(response().withStatusCode(202));
78     }
79
80     @AfterAll
81     public static void stopServer() {
82         mockServer.stop();
83         mockServer = null;
84     }
85
86     @Test
87     void test_getstatus() throws Exception {
88         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
89
90             Loop status = client.getstatus(LOOP);
91
92             String json = CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED);
93             Loop loop = CODER.convert(json, Loop.class);
94
95             assertThat(ClampHttpClient.getStatusCode(status)).isEqualTo(ClampHttpClient.getStatusCode(loop));
96
97         } catch (Exception e) {
98             fail(e.getMessage());
99         }
100     }
101
102     @Test
103     void test_deploy() throws Exception {
104         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
105
106             assertTrue(client.deploy(LOOP));
107
108         } catch (Exception e) {
109             fail(e.getMessage());
110         }
111     }
112
113     @Test
114     void test_undeploy() throws Exception {
115         try (ClampHttpClient client = new ClampHttpClient(parameters)) {
116
117             assertTrue(client.undeploy(LOOP));
118
119         } catch (Exception e) {
120             fail(e.getMessage());
121         }
122     }
123
124     @Test
125     void test_getStatusCodeNull() {
126         assertThat(ClampHttpClient.getStatusCode(null)).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
127     }
128
129     @Test
130     void test_getStatusEmptyMap() {
131         assertThat(ClampHttpClient.getStatusCode(new Loop())).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
132     }
133 }