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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.dcae.httpclient;
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;
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;
44 * Class to perform unit test of {@link ClampHttpClient}.
47 @ExtendWith(SpringExtension.class)
48 class ClampHttpClientTest {
50 private static final String LOOP = "pmsh_loop";
51 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
53 private static ClientAndServer mockServer;
54 private static ParticipantDcaeParameters parameters;
55 public static final Coder CODER = new StandardCoder();
61 public static void setUp() {
62 CommonTestData commonTestData = new CommonTestData();
64 parameters = commonTestData.getParticipantDcaeParameters();
66 mockServer = ClientAndServer.startClientAndServer(parameters.getClampClientParameters().getPort());
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));
72 mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
73 .respond(response().withStatusCode(202));
75 mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
76 .respond(response().withStatusCode(202));
80 public static void stopServer() {
86 void test_getstatus() throws Exception {
87 try (ClampHttpClient client = new ClampHttpClient(parameters)) {
89 Loop status = client.getstatus(LOOP);
91 String json = CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED);
92 Loop loop = CODER.convert(json, Loop.class);
94 assertThat(ClampHttpClient.getStatusCode(status)).isEqualTo(ClampHttpClient.getStatusCode(loop));
96 } catch (Exception e) {
102 void test_create() throws Exception {
103 try (ClampHttpClient client = new ClampHttpClient(parameters)) {
105 assertThat(client.create(LOOP, null)).isNull();
107 } catch (Exception e) {
108 fail(e.getMessage());
113 void test_deploy() throws Exception {
114 try (ClampHttpClient client = new ClampHttpClient(parameters)) {
116 assertTrue(client.deploy(LOOP));
118 } catch (Exception e) {
119 fail(e.getMessage());
124 void test_undeploy() throws Exception {
125 try (ClampHttpClient client = new ClampHttpClient(parameters)) {
127 assertTrue(client.undeploy(LOOP));
129 } catch (Exception e) {
130 fail(e.getMessage());
135 void test_getStatusCodeNull() {
136 assertThat(ClampHttpClient.getStatusCode(null)).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
140 void test_getStatusEmptyMap() {
141 assertThat(ClampHttpClient.getStatusCode(new Loop())).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
145 void test_stop() throws Exception {
146 try (ClampHttpClient client = new ClampHttpClient(parameters)) {
148 assertFalse(client.stop(LOOP));
150 } catch (Exception e) {
151 fail(e.getMessage());
156 void test_delete() throws Exception {
157 try (ClampHttpClient client = new ClampHttpClient(parameters)) {
159 assertFalse(client.delete(LOOP));
161 } catch (Exception e) {
162 fail(e.getMessage());