1b01f544bcb992f642f846dc8c4b3529160a5b92
[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
40
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class DcaeDispatcherServicesTest {
44
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\"}}";
53
54     @Mock
55     private ClampProperties clampProperties;
56
57     @Mock
58     DcaeHttpConnectionManager dcaeHttpConnectionManager;
59
60     @InjectMocks
61     DcaeDispatcherServices dcaeDispatcherServices;
62
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\"}";
66
67     /**
68      * Setup method.
69      */
70     @Before
71     public void setUp() {
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)
76                 .build()
77                 .forEach((property, value) -> {
78                     Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any()))
79                             .thenReturn(value);
80                     Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value);
81                 });
82     }
83
84     @Test
85     public void shouldReturnDcaeOperationSataus() throws IOException {
86         //given
87         Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null))
88                 .thenReturn(STATUS_RESPONSE_PROCESSING);
89         //when
90         String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL);
91
92         //then
93         Assertions.assertThat(operationStatus).isEqualTo("processing");
94     }
95
96     @Test
97     public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException {
98         //given
99         Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET",
100                 null, null))
101                 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE);
102         //when
103         String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
104
105         //then
106         Assertions.assertThat(operationStatus).isEqualTo("succeeded");
107         Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
108                 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
109
110     }
111
112     @Test
113     public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException {
114         //given
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);
119         //when
120         String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
121
122         //then
123         Assertions.assertThat(operationStatus).isEqualTo("processing");
124         Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
125                 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
126
127     }
128
129     @Test
130     public void shouldTriggerDeploymentCreation() throws IOException {
131         //given
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());
136
137         Mockito.when(dcaeHttpConnectionManager
138                 .doDcaeHttpQuery(DCAE_URL
139                                 + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId",
140                         "PUT",
141                         "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}",
142                         "application/json"))
143                 .thenReturn(DEPLOY_RESPONSE_STRING);
144         JsonObject blueprintInputJson = new JsonObject();
145
146         //when
147         String operationStatus = dcaeDispatcherServices
148                 .createNewDeployment(deploymentId, serviceTypeId, blueprintInputJson);
149
150         //then
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");
154
155     }
156 }