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;
42 @RunWith(MockitoJUnitRunner.class)
43 public class DcaeDispatcherServicesTest {
45 private static final String DEPLOYMENT_STATUS_URL = "http://portal.api.simpledemo.onap.org:30297/dcae-deployments/"
46 + "closedLoop_c9c8b281-6fbd-4702-ba13-affa90411152_deploymentId/"
47 + "operation/a97b46f6-d77c-42a1-9449-d5ae71e8f688";
48 private static final String DCAE_URL = "dcae_url";
49 private static final String DEPLOY_RESPONSE_STRING = "{\"links\":"
50 + "{\"status\":\"http://deployment-handler.onap:8443/dcae-deployments/"
51 + "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/"
52 + "operation/366eb098-7977-4966-ae82-abd2087edb10\"}}";
55 private ClampProperties clampProperties;
58 DcaeHttpConnectionManager dcaeHttpConnectionManager;
61 DcaeDispatcherServices dcaeDispatcherServices;
63 private static final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\","
64 + "\"status\": \"processing\"}";
65 private static final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}";
72 ImmutableMap.<String, String>builder()
73 .put("dcae.dispatcher.retry.limit", "3")
74 .put("dcae.dispatcher.retry.interval", "0")
75 .put("dcae.dispatcher.url", DCAE_URL)
77 .forEach((property, value) -> {
78 Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any()))
80 Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value);
85 public void shouldReturnDcaeOperationSataus() throws IOException {
87 Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null))
88 .thenReturn(STATUS_RESPONSE_PROCESSING);
90 String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL);
93 Assertions.assertThat(operationStatus).isEqualTo("processing");
97 public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException {
99 Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET",
101 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE);
103 String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
106 Assertions.assertThat(operationStatus).isEqualTo("succeeded");
107 Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
108 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
113 public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException {
115 Mockito.when(dcaeHttpConnectionManager
116 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null))
117 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING,
118 STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING);
120 String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
123 Assertions.assertThat(operationStatus).isEqualTo("processing");
124 Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
125 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
130 public void shouldTriggerDeploymentCreation() throws IOException {
132 String deploymentId = "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId";
133 String serviceTypeId = "e2ba40f7-bf42-41e7-acd7-48fd07586d90";
134 Mockito.when(clampProperties.getJsonTemplate("dcae.deployment.template"))
135 .thenReturn(new JsonObject());
137 Mockito.when(dcaeHttpConnectionManager
138 .doDcaeHttpQuery(DCAE_URL
139 + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId",
141 "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}",
143 .thenReturn(DEPLOY_RESPONSE_STRING);
144 JsonObject blueprintInputJson = new JsonObject();
147 String operationStatus = dcaeDispatcherServices
148 .createNewDeployment(deploymentId, serviceTypeId, blueprintInputJson);
151 Assertions.assertThat(operationStatus).isEqualTo("http://deployment-handler.onap:8443/"
152 + "dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/"
153 + "operation/366eb098-7977-4966-ae82-abd2087edb10");