c828f78a93b8300ab919f9bb6b9f3bf15526b011
[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.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;
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     DcaeHttpConnectionManager dcaeHttpConnectionManager;
60
61     @InjectMocks
62     DcaeDispatcherServices dcaeDispatcherServices;
63
64     private final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\",\"status\": \"processing\"}";
65     private final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}";
66
67     @Before
68     public void setUp() {
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)
73                 .build()
74                 .forEach((property, value) -> {
75                     Mockito.when(clampProperties.getStringValue(Matchers.matches(property), Matchers.any()))
76                             .thenReturn(value);
77                     Mockito.when(clampProperties.getStringValue(Matchers.matches(property))).thenReturn(value);
78                 });
79     }
80
81     @Test
82     public void shouldReturnDcaeOperationSataus() throws IOException {
83         //given
84         Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null))
85                 .thenReturn(STATUS_RESPONSE_PROCESSING);
86         //when
87         String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL);
88
89         //then
90         Assertions.assertThat(operationStatus).isEqualTo("processing");
91     }
92
93     @Test
94     public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException {
95         //given
96         Mockito.when(dcaeHttpConnectionManager.doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET",
97                 null, null))
98                 .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE);
99         //when
100         String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
101
102         //then
103         Assertions.assertThat(operationStatus).isEqualTo("succeeded");
104         Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
105                 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
106
107     }
108
109     @Test
110     public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException {
111         //given
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);
116         //when
117         String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(DEPLOYMENT_STATUS_URL);
118
119         //then
120         Assertions.assertThat(operationStatus).isEqualTo("processing");
121         Mockito.verify(dcaeHttpConnectionManager, Mockito.times(3))
122                 .doDcaeHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null);
123
124     }
125
126     @Test
127     public void shouldTriggerDeploymentCreation() throws IOException {
128         //given
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("{}"));
133
134         Mockito.when(dcaeHttpConnectionManager
135                 .doDcaeHttpQuery(DCAE_URL
136                                 + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId",
137                         "PUT",
138                         "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}",
139                         "application/json"))
140                 .thenReturn(DEPLOY_RESPONSE_STRING);
141         JsonNode blueprintInputJson = new ObjectMapper().readTree("{}");
142
143         //when
144         String operationStatus = dcaeDispatcherServices
145                 .createNewDeployment(deploymentID, serviceTypeId, blueprintInputJson);
146
147         //then
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");
151
152     }
153 }