Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / test / java / org / openecomp / mso / bpmn / infrastructure / pnf / delegate / InformDmaapClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.openecomp.mso.bpmn.infrastructure.pnf.delegate;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.inOrder;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verifyZeroInteractions;
29 import static org.mockito.Mockito.when;
30
31 import org.camunda.bpm.engine.ProcessEngineServices;
32 import org.camunda.bpm.engine.RuntimeService;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.engine.runtime.MessageCorrelationBuilder;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InOrder;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.test.context.junit4.SpringRunner;
41
42 @RunWith(SpringRunner.class)
43 @SpringBootTest(classes = {InformDmaapClient.class, DmaapClientTestImpl.class})
44 public class InformDmaapClientTest {
45
46     @Autowired
47     private InformDmaapClient informDmaapClient;
48
49     @Autowired
50     private DmaapClientTestImpl dmaapClientTest;
51
52     private DelegateExecution delegateExecution;
53
54     private MessageCorrelationBuilder messageCorrelationBuilder;
55
56     @Test
57     public void shouldSendListenerToDmaapClient() throws Exception {
58         // given
59         mockDelegateExecution();
60         // when
61         informDmaapClient.execute(delegateExecution);
62         // then
63         assertThat(dmaapClientTest.getCorrelationId()).isEqualTo("testCorrelationId");
64         assertThat(dmaapClientTest.getInformConsumer()).isNotNull();
65         verifyZeroInteractions(messageCorrelationBuilder);
66     }
67
68     @Test
69     public void shouldSendListenerToDmaapClientAndSendMessageToCamunda() throws Exception {
70         // given
71         mockDelegateExecution();
72         // when
73         informDmaapClient.execute(delegateExecution);
74         dmaapClientTest.getInformConsumer().run();
75         // then
76         assertThat(dmaapClientTest.getCorrelationId()).isEqualTo("testCorrelationId");
77         InOrder inOrder = inOrder(messageCorrelationBuilder);
78         inOrder.verify(messageCorrelationBuilder).processInstanceBusinessKey("testBusinessKey");
79         inOrder.verify(messageCorrelationBuilder).correlateWithResult();
80     }
81
82     private void mockDelegateExecution() {
83         delegateExecution = mock(DelegateExecution.class);
84         when(delegateExecution.getVariable(eq(ExecutionVariableNames.CORRELATION_ID))).thenReturn("testCorrelationId");
85         when(delegateExecution.getProcessBusinessKey()).thenReturn("testBusinessKey");
86         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
87         when(delegateExecution.getProcessEngineServices()).thenReturn(processEngineServices);
88         RuntimeService runtimeService = mock(RuntimeService.class);
89         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
90         messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
91         when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
92         when(messageCorrelationBuilder.processInstanceBusinessKey(any())).thenReturn(messageCorrelationBuilder);
93     }
94 }