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.assertTrue;
26 import static org.mockserver.model.HttpRequest.request;
27 import static org.mockserver.model.HttpResponse.response;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockserver.integration.ClientAndServer;
33 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
34 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
35 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.StandardCoder;
40 * Class to perform unit test of {@link ClampHttpClient}.
43 public class ClampHttpClientTest {
45 private static final String LOOP = "pmsh_loop";
46 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
48 private static ClientAndServer mockServer;
49 private static ParticipantDcaeParameters parameters;
50 public static final Coder CODER = new StandardCoder();
56 public static void setUp() {
57 CommonTestData commonTestData = new CommonTestData();
59 parameters = commonTestData.toObject(
60 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
61 ParticipantDcaeParameters.class);
63 mockServer = ClientAndServer.startClientAndServer(parameters.getClampClientParameters().getPort());
65 mockServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
66 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED)).withStatusCode(200));
68 mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
69 .respond(response().withStatusCode(202));
71 mockServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
72 .respond(response().withStatusCode(202));
76 public static void stopServer() {
82 public void test_getstatus() throws Exception {
83 try (ClampHttpClient client = new ClampHttpClient(parameters.getClampClientParameters())) {
85 Loop status = client.getstatus(LOOP);
87 String json = CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED);
88 Loop loop = CODER.convert(json, Loop.class);
90 assertThat(ClampHttpClient.getStatusCode(status)).isEqualTo(ClampHttpClient.getStatusCode(loop));
92 } catch (Exception e) {
98 public void test_deploy() throws Exception {
99 try (ClampHttpClient client = new ClampHttpClient(parameters.getClampClientParameters())) {
101 assertTrue(client.deploy(LOOP));
103 } catch (Exception e) {
104 fail(e.getMessage());
109 public void test_undeploy() throws Exception {
110 try (ClampHttpClient client = new ClampHttpClient(parameters.getClampClientParameters())) {
112 assertTrue(client.undeploy(LOOP));
114 } catch (Exception e) {
115 fail(e.getMessage());
120 public void test_getStatusCodeNull() {
121 assertThat(ClampHttpClient.getStatusCode(null)).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);
125 public void test_getStatusEmptyMap() {
126 assertThat(ClampHttpClient.getStatusCode(new Loop())).isEqualTo(ClampHttpClient.STATUS_NOT_FOUND);