2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.client;
26 import com.google.common.collect.ImmutableMap;
27 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import org.assertj.core.api.Assertions;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Matchers;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.clamp.clds.config.ClampProperties;
39 import org.onap.clamp.util.HttpConnectionManager;
43 @RunWith(MockitoJUnitRunner.class)
44 public class DcaeDispatcherServicesTest {
46 private static final String DEPLOYMENT_STATUS_URL = "http://portal.api.simpledemo.onap.org:30297/dcae-deployments/"
47 + "closedLoop_c9c8b281-6fbd-4702-ba13-affa90411152_deploymentId/"
48 + "operation/a97b46f6-d77c-42a1-9449-d5ae71e8f688";
49 private static final String DCAE_URL = "dcae_url";
50 private static final String DEPLOY_RESPONSE_STRING = "{\"links\":"
51 + "{\"status\":\"http://deployment-handler.onap:8443/dcae-deployments/"
52 + "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/"
53 + "operation/366eb098-7977-4966-ae82-abd2087edb10\"}}";
56 private ClampProperties clampProperties;
59 HttpConnectionManager httpConnectionManager;
62 DcaeDispatcherServices dcaeDispatcherServices;
64 private static final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\","
65 + "\"status\": \"processing\"}";
66 private static final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}";
73 ImmutableMap.<String, String>builder()
74 .put("dcae.dispatcher.retry.limit", "3")
75 .put("dcae.dispatcher.retry.interval", "0")
76 .put("dcae.dispatcher.url", DCAE_URL)
78 .forEach((property, value) -> {
79 Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any()))
81 Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value);
86 public void shouldReturnDcaeOperationSataus() throws IOException {
88 Mockito.when(httpConnectionManager.doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null))
89 .thenReturn(STATUS_RESPONSE_PROCESSING);
91 String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL);
94 Assertions.assertThat(operationStatus).isEqualTo("processing");
98 public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException {
100 Mockito.when(httpConnectionManager.doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET",
101 null, null, "DCAE", null, null))
102 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE);
104 String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
107 Assertions.assertThat(operationStatus).isEqualTo("succeeded");
108 Mockito.verify(httpConnectionManager, Mockito.times(3))
109 .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null);
114 public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException {
116 Mockito.when(httpConnectionManager
117 .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null))
118 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING,
119 STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING);
121 String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
124 Assertions.assertThat(operationStatus).isEqualTo("processing");
125 Mockito.verify(httpConnectionManager, Mockito.times(3))
126 .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null);
131 public void shouldTriggerDeploymentCreation() throws IOException {
133 String deploymentId = "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId";
134 String serviceTypeId = "e2ba40f7-bf42-41e7-acd7-48fd07586d90";
135 Mockito.when(clampProperties.getJsonTemplate("dcae.deployment.template"))
136 .thenReturn(new JsonObject());
138 Mockito.when(httpConnectionManager
139 .doGeneralHttpQuery(DCAE_URL
140 + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId",
142 "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}",
143 "application/json", "DCAE", null, null))
144 .thenReturn(DEPLOY_RESPONSE_STRING);
145 JsonObject blueprintInputJson = new JsonObject();
148 String operationStatus = dcaeDispatcherServices
149 .createNewDeployment(deploymentId, serviceTypeId, blueprintInputJson);
152 Assertions.assertThat(operationStatus).isEqualTo("http://deployment-handler.onap:8443/"
153 + "dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/"
154 + "operation/366eb098-7977-4966-ae82-abd2087edb10");