Dmaap micro service jar
[appc.git] / services / appc-dmaap-service / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / demo / impl / TestWorkerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  * file except in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.listener.demo.impl;
20
21 import static org.mockito.Matchers.anyObject;
22 import static org.mockito.Mockito.when;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.times;
25
26 import org.json.JSONObject;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mockito;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.appc.listener.EventHandler;
33 import org.onap.appc.listener.demo.model.CommonMessage.CommonHeader;
34 import org.onap.appc.listener.demo.model.IncomingMessage;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest(ProviderOperations.class)
41 public class TestWorkerImpl {
42
43   private WorkerImpl workerImplSpy;
44   private IncomingMessage message;
45   private EventHandler dmaap;
46
47   @Before
48   public void setUp() {
49     message = Mockito.mock(IncomingMessage.class);
50     dmaap = Mockito.mock(EventHandler.class);
51     workerImplSpy = Mockito.spy(new WorkerImpl(message, dmaap));
52   }
53
54   @Test
55   public void testRun() throws APPCException {
56     CommonHeader commonHeader = Mockito.mock(CommonHeader.class);
57     when(message.getHeader()).thenReturn(commonHeader);
58     when(commonHeader.getRequestID()).thenReturn("requestId");
59     PowerMockito.mockStatic(ProviderOperations.class);
60     PowerMockito.when(ProviderOperations.topologyDG(anyObject())).thenReturn(true);
61     workerImplSpy.run();
62     verify(workerImplSpy, times(1)).run();
63   }
64
65   @Test
66   public void testRunElseCase() throws APPCException {
67     CommonHeader commonHeader = Mockito.mock(CommonHeader.class);
68     when(message.getHeader()).thenReturn(commonHeader);
69     when(commonHeader.getRequestID()).thenReturn("requestId");
70     when(message.toJson()).thenReturn(new JSONObject());
71     PowerMockito.mockStatic(ProviderOperations.class);
72     PowerMockito.when(ProviderOperations.topologyDG(anyObject())).thenReturn(false);
73     workerImplSpy.run();
74     verify(workerImplSpy, times(1)).run();
75   }
76
77   @Test
78   public void testRunWithException() throws APPCException {
79     CommonHeader commonHeader = Mockito.mock(CommonHeader.class);
80     when(message.getHeader()).thenReturn(commonHeader);
81     when(commonHeader.getRequestID()).thenReturn("requestId");
82     workerImplSpy.run();
83     verify(workerImplSpy, times(1)).run();
84   }
85 }