94aa1427a44d77f3b8caec3e4452e0d0d6212bc0
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / 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.onap.so.bpmn.infrastructure.pnf.delegate;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.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 import org.camunda.bpm.engine.ProcessEngineServices;
31 import org.camunda.bpm.engine.RuntimeService;
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.camunda.bpm.engine.runtime.MessageCorrelationBuilder;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.InOrder;
37
38 public class InformDmaapClientTest {
39     @Before
40     public void setUp() {
41         informDmaapClient = new InformDmaapClient();
42         dmaapClientTest = new DmaapClientTestImpl();
43         informDmaapClient.setDmaapClient(dmaapClientTest);
44         delegateExecution = mockDelegateExecution();
45     }
46
47     private InformDmaapClient informDmaapClient;
48
49     private DmaapClientTestImpl dmaapClientTest;
50
51     private DelegateExecution delegateExecution;
52
53     private MessageCorrelationBuilder messageCorrelationBuilder;
54
55     @Test
56     public void shouldSendListenerToDmaapClient() {
57         // when
58         informDmaapClient.execute(delegateExecution);
59         // then
60         assertThat(dmaapClientTest.getPnfCorrelationId()).isEqualTo("testPnfCorrelationId");
61         assertThat(dmaapClientTest.getInformConsumer()).isNotNull();
62         verifyZeroInteractions(messageCorrelationBuilder);
63     }
64
65     @Test
66     public void shouldSendListenerToDmaapClientAndSendMessageToCamunda() {
67         // when
68         informDmaapClient.execute(delegateExecution);
69         dmaapClientTest.getInformConsumer().run();
70         // then
71         assertThat(dmaapClientTest.getPnfCorrelationId()).isEqualTo("testPnfCorrelationId");
72         InOrder inOrder = inOrder(messageCorrelationBuilder);
73         inOrder.verify(messageCorrelationBuilder).processInstanceBusinessKey("testBusinessKey");
74         inOrder.verify(messageCorrelationBuilder).correlateWithResult();
75     }
76
77     private DelegateExecution mockDelegateExecution() {
78         DelegateExecution delegateExecution = mock(DelegateExecution.class);
79         when(delegateExecution.getVariable(eq(ExecutionVariableNames.PNF_CORRELATION_ID)))
80                 .thenReturn("testPnfCorrelationId");
81         when(delegateExecution.getProcessBusinessKey()).thenReturn("testBusinessKey");
82         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
83         when(delegateExecution.getProcessEngineServices()).thenReturn(processEngineServices);
84         RuntimeService runtimeService = mock(RuntimeService.class);
85         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
86         messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
87         when(runtimeService.createMessageCorrelation(any())).thenReturn(messageCorrelationBuilder);
88         when(messageCorrelationBuilder.processInstanceBusinessKey(any())).thenReturn(messageCorrelationBuilder);
89         return delegateExecution;
90     }
91 }