2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.job.command;
24 import io.joshworks.restclient.http.HttpResponse;
25 import org.mockito.Mock;
26 import org.onap.vid.aai.AaiResponse;
27 import org.onap.vid.aai.exceptions.InvalidAAIResponseException;
28 import org.onap.vid.changeManagement.RequestDetailsWrapper;
29 import org.onap.vid.domain.mso.RequestReferences;
30 import org.onap.vid.exceptions.MaxRetriesException;
31 import org.onap.vid.job.Job;
32 import org.onap.vid.job.NextCommand;
33 import org.onap.vid.model.RequestReferencesContainer;
34 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
35 import org.onap.vid.mso.MsoInterface;
36 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
37 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
38 import org.onap.vid.services.AuditService;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
42 import java.util.UUID;
44 import static org.hamcrest.CoreMatchers.instanceOf;
45 import static org.hamcrest.CoreMatchers.is;
46 import static org.hamcrest.CoreMatchers.nullValue;
47 import static org.hamcrest.MatcherAssert.assertThat;
48 import static org.hamcrest.core.IsEqual.equalTo;
49 import static org.mockito.Mockito.doThrow;
50 import static org.mockito.Mockito.verify;
51 import static org.mockito.Mockito.when;
52 import static org.mockito.MockitoAnnotations.initMocks;
54 public class ServiceInstantiationCommandTest {
56 private AsyncInstantiationBusinessLogic asyncInstantiationBusinessLogic;
59 private MsoInterface msoInterface;
62 private AuditService auditService;
65 private ServiceInstantiation serviceInstantiation;
68 private HttpResponse<RequestReferencesContainer> msoResponse;
71 private RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper;
74 private AaiResponse aaiResponse;
77 private RequestReferencesContainer requestReferencesContainer;
80 private UUID uuid = UUID.randomUUID();
83 private ServiceInstantiationCommand serviceInstantiationCommand;
88 serviceInstantiationCommand = new ServiceInstantiationCommand(asyncInstantiationBusinessLogic, auditService, msoInterface, uuid, serviceInstantiation, "sampleUserId");
93 public void shouldProperlyHandleMaxRetriesException() {
94 when(asyncInstantiationBusinessLogic.generateServiceInstantiationRequest(uuid, serviceInstantiation, "sampleUserId")).thenThrow(new MaxRetriesException("", 2));
96 NextCommand call = serviceInstantiationCommand.call();
98 assertThat(call.getCommand(), is(nullValue()));
99 assertThat(call.getStatus(), is(equalTo(Job.JobStatus.FAILED)));
101 verify(asyncInstantiationBusinessLogic).handleFailedInstantiation(uuid);
105 public void shouldProperlyHandleInvalidAAIResponseException() {
106 doThrow(new InvalidAAIResponseException(aaiResponse)).when(asyncInstantiationBusinessLogic).generateServiceInstantiationRequest(uuid, serviceInstantiation, "sampleUserId");
108 NextCommand call = serviceInstantiationCommand.call();
110 assertThat(call.getCommand(), is(serviceInstantiationCommand));
111 assertThat(call.getStatus(), is(equalTo(Job.JobStatus.IN_PROGRESS)));
116 public void shouldProperlyHandleInvalidSOResponse() {
117 when(asyncInstantiationBusinessLogic.generateServiceInstantiationRequest(uuid, serviceInstantiation, "sampleUserId")).thenReturn(requestDetailsWrapper);
118 when(asyncInstantiationBusinessLogic.getServiceInstantiationPath(serviceInstantiation)).thenReturn("samplePath");
119 when(msoInterface.post("samplePath", requestDetailsWrapper, RequestReferencesContainer.class)).thenReturn(msoResponse);
120 when(msoResponse.getStatus()).thenReturn(500);
121 when(msoResponse.getBody()).thenReturn(requestReferencesContainer);
123 NextCommand call = serviceInstantiationCommand.call();
125 assertThat(call.getCommand(), is(nullValue()));
126 assertThat(call.getStatus(), is(equalTo(Job.JobStatus.FAILED)));
128 verify(auditService).setFailedAuditStatusFromMso(uuid, null, 500, requestReferencesContainer.toString());
133 public void shouldProperlyUpdateServiceStatusAndReturnInProgressCommand() {
134 RequestReferences requestReferences = createRequestReferences();
136 when(asyncInstantiationBusinessLogic.generateServiceInstantiationRequest(uuid, serviceInstantiation, "sampleUserId")).thenReturn(requestDetailsWrapper);
137 when(asyncInstantiationBusinessLogic.getServiceInstantiationPath(serviceInstantiation)).thenReturn("samplePath");
138 when(msoInterface.post("samplePath", requestDetailsWrapper, RequestReferencesContainer.class)).thenReturn(msoResponse);
139 when(msoResponse.getStatus()).thenReturn(200);
140 when(msoResponse.getBody()).thenReturn(requestReferencesContainer);
141 when(requestReferencesContainer.getRequestReferences()).thenReturn(requestReferences);
144 NextCommand call = serviceInstantiationCommand.call();
146 assertThat(call.getCommand(), instanceOf(InProgressStatusCommand.class));
147 assertThat(call.getStatus(), is(equalTo(Job.JobStatus.IN_PROGRESS)));
151 private RequestReferences createRequestReferences() {
152 RequestReferences requestReferences = new RequestReferences();
153 requestReferences.setInstanceId("sampleInstanceId");
154 requestReferences.setRequestId("sampleRequestId");
155 return requestReferences;