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.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.common.collect.ImmutableMap;
29 import java.io.IOException;
30 import org.assertj.core.api.Assertions;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Matchers;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.clamp.clds.config.ClampProperties;
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 DcaeHttpConnectionManager dcaeHttpConnectionManager;
62 DcaeDispatcherServices dcaeDispatcherServices;
64 private final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\",\"status\": \"processing\"}";
65 private final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}";
69 ImmutableMap.<String, String>builder()
70 .put("dcae.dispatcher.retry.limit", "3")
71 .put("dcae.dispatcher.retry.interval", "0")
72 .put("dcae.dispatcher.url", DCAE_URL)
74 .forEach((property, value) -> {
75 Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any()))
77 Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value);
82 public void shouldReturnDcaeOperationSataus() throws IOException {
84 Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null))
85 .thenReturn(STATUS_RESPONSE_PROCESSING);
87 String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL);
90 Assertions.assertThat(operationStatus).isEqualTo("processing");
94 public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException {
96 Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET",
98 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE);
100 String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
103 Assertions.assertThat(operationStatus).isEqualTo("succeeded");
104 Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
105 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
110 public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException {
112 Mockito.when(dcaeHttpConnectionManager
113 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null))
114 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING,
115 STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING);
117 String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
120 Assertions.assertThat(operationStatus).isEqualTo("processing");
121 Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
122 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
127 public void shouldTriggerDeploymentCreation() throws IOException {
129 String deploymentID = "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId";
130 String serviceTypeId = "e2ba40f7-bf42-41e7-acd7-48fd07586d90";
131 Mockito.when(clampProperties.getJsonTemplate("dcae.deployment.template"))
132 .thenReturn(new ObjectMapper().readTree("{}"));
134 Mockito.when(dcaeHttpConnectionManager
135 .doDcaeHttpQuery(DCAE_URL
136 + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId",
138 "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}",
140 .thenReturn(DEPLOY_RESPONSE_STRING);
141 JsonNode blueprintInputJson = new ObjectMapper().readTree("{}");
144 String operationStatus = dcaeDispatcherServices
145 .createNewDeployment(deploymentID, serviceTypeId, blueprintInputJson);
148 Assertions.assertThat(operationStatus).isEqualTo("http://deployment-handler.onap:8443/"
149 + "dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId/"
150 + "operation/366eb098-7977-4966-ae82-abd2087edb10");