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