Merge "Logging improvements"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / ServiceInstantiationCommandTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.job.command;
22
23
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;
41
42 import java.util.UUID;
43
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;
53
54 public class ServiceInstantiationCommandTest {
55     @Mock
56     private AsyncInstantiationBusinessLogic asyncInstantiationBusinessLogic;
57
58     @Mock
59     private MsoInterface msoInterface;
60
61     @Mock
62     private AuditService auditService;
63
64     @Mock
65     private ServiceInstantiation serviceInstantiation;
66
67     @Mock
68     private HttpResponse<RequestReferencesContainer> msoResponse;
69
70     @Mock
71     private RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper;
72
73     @Mock
74     private AaiResponse aaiResponse;
75
76     @Mock
77     private RequestReferencesContainer requestReferencesContainer;
78
79
80     private UUID uuid = UUID.randomUUID();
81
82
83     private ServiceInstantiationCommand serviceInstantiationCommand;
84
85     @BeforeMethod
86     public void setUp() {
87         initMocks(this);
88         serviceInstantiationCommand = new ServiceInstantiationCommand(asyncInstantiationBusinessLogic, auditService, msoInterface, uuid, serviceInstantiation, "sampleUserId");
89     }
90
91
92     @Test
93     public void shouldProperlyHandleMaxRetriesException() {
94         when(asyncInstantiationBusinessLogic.generateServiceInstantiationRequest(uuid, serviceInstantiation, "sampleUserId")).thenThrow(new MaxRetriesException("", 2));
95
96         NextCommand call = serviceInstantiationCommand.call();
97
98         assertThat(call.getCommand(), is(nullValue()));
99         assertThat(call.getStatus(), is(equalTo(Job.JobStatus.FAILED)));
100
101         verify(asyncInstantiationBusinessLogic).handleFailedInstantiation(uuid);
102     }
103
104     @Test
105     public void shouldProperlyHandleInvalidAAIResponseException() {
106         doThrow(new InvalidAAIResponseException(aaiResponse)).when(asyncInstantiationBusinessLogic).generateServiceInstantiationRequest(uuid, serviceInstantiation, "sampleUserId");
107
108         NextCommand call = serviceInstantiationCommand.call();
109
110         assertThat(call.getCommand(), is(serviceInstantiationCommand));
111         assertThat(call.getStatus(), is(equalTo(Job.JobStatus.IN_PROGRESS)));
112     }
113
114
115     @Test
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);
122
123         NextCommand call = serviceInstantiationCommand.call();
124
125         assertThat(call.getCommand(), is(nullValue()));
126         assertThat(call.getStatus(), is(equalTo(Job.JobStatus.FAILED)));
127
128         verify(auditService).setFailedAuditStatusFromMso(uuid, null, 500, requestReferencesContainer.toString());
129     }
130
131
132     @Test
133     public void shouldProperlyUpdateServiceStatusAndReturnInProgressCommand() {
134         RequestReferences requestReferences = createRequestReferences();
135
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);
142
143
144         NextCommand call = serviceInstantiationCommand.call();
145
146         assertThat(call.getCommand(), instanceOf(InProgressStatusCommand.class));
147         assertThat(call.getStatus(), is(equalTo(Job.JobStatus.IN_PROGRESS)));
148
149     }
150
151     private RequestReferences createRequestReferences() {
152         RequestReferences requestReferences = new RequestReferences();
153         requestReferences.setInstanceId("sampleInstanceId");
154         requestReferences.setRequestId("sampleRequestId");
155         return requestReferences;
156     }
157 }