Rework the submit operation
[clamp.git] / src / test / java / org / onap / clamp / clds / client / DcaeDispatcherServicesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 Nokia Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.client;
25
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;
40
41
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class DcaeDispatcherServicesTest {
45
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\"}}";
54
55     @Mock
56     private ClampProperties clampProperties;
57
58     @Mock
59     HttpConnectionManager httpConnectionManager;
60
61     @InjectMocks
62     DcaeDispatcherServices dcaeDispatcherServices;
63
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\"}";
67
68     /**
69      * Setup method.
70      */
71     @Before
72     public void setUp() {
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)
77                 .build()
78                 .forEach((property, value) -> {
79                     Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any()))
80                             .thenReturn(value);
81                     Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value);
82                 });
83     }
84
85     @Test
86     public void shouldReturnDcaeOperationSataus() throws IOException {
87         //given
88         Mockito.when(httpConnectionManager.doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null))
89                 .thenReturn(STATUS_RESPONSE_PROCESSING);
90         //when
91         String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL);
92
93         //then
94         Assertions.assertThat(operationStatus).isEqualTo("processing");
95     }
96
97     @Test
98     public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException {
99         //given
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);
103         //when
104         String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
105
106         //then
107         Assertions.assertThat(operationStatus).isEqualTo("succeeded");
108         Mockito.verify(httpConnectionManager, Mockito.times(3))
109                 .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null);
110
111     }
112
113     @Test
114     public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException {
115         //given
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);
120         //when
121         String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
122
123         //then
124         Assertions.assertThat(operationStatus).isEqualTo("processing");
125         Mockito.verify(httpConnectionManager, Mockito.times(3))
126                 .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null);
127
128     }
129
130     @Test
131     public void shouldTriggerDeploymentCreation() throws IOException {
132         //given
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());
137
138         Mockito.when(httpConnectionManager
139                 .doGeneralHttpQuery(DCAE_URL
140                                 + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId",
141                         "PUT",
142                         "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}",
143                         "application/json", "DCAE", null, null))
144                 .thenReturn(DEPLOY_RESPONSE_STRING);
145         JsonObject blueprintInputJson = new JsonObject();
146
147         //when
148         String operationStatus = dcaeDispatcherServices
149                 .createNewDeployment(deploymentId, serviceTypeId, blueprintInputJson);
150
151         //then
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");
155
156     }
157 }