2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Ericsson
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.workflow.activator;
24 import static org.junit.Assert.assertTrue;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.mockito.Matchers;
32 import org.mockito.Mockito;
33 import org.onap.appc.transactionrecorder.TransactionRecorder;
34 import org.osgi.framework.Bundle;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceReference;
37 import org.osgi.framework.FrameworkUtil;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest(FrameworkUtil.class)
44 public class TransactionAbortedMarkerTest {
46 private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
47 private final Bundle bundleService = Mockito.mock(Bundle.class);
48 private final ServiceReference sref = Mockito.mock(ServiceReference.class);
49 private final TransactionRecorder transactionRecorder = Mockito.mock(TransactionRecorder.class);
52 public ExpectedException expectedEx = ExpectedException.none();
55 public void testRunTransactionRecorderServiceException() throws TransactionRecorderServiceNotFoundException {
56 ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
57 TransactionAbortedMarker tamt = Mockito.spy(new TransactionAbortedMarker(executor));
58 Mockito.doThrow(new TransactionRecorderServiceNotFoundException(null)).when(tamt).lookupTransactionRecorder();
60 Mockito.verify(executor).schedule(tamt, 30, TimeUnit.SECONDS);
64 public void testRuntimeException() throws TransactionRecorderServiceNotFoundException {
65 ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
66 TransactionAbortedMarker tamt = Mockito.spy(new TransactionAbortedMarker(executor));
67 Mockito.doThrow(new RuntimeException()).when(tamt).lookupTransactionRecorder();
68 expectedEx.expect(RuntimeException.class);
73 public void testLookupTransactionRecorder() throws TransactionRecorderServiceNotFoundException {
74 PowerMockito.mockStatic(FrameworkUtil.class);
75 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
76 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
77 PowerMockito.when(bundleContext.getServiceReference(TransactionRecorder.class.getName())).thenReturn(sref);
78 PowerMockito.when(bundleContext.<TransactionRecorder>getService(sref)).thenReturn(transactionRecorder);
79 ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
80 TransactionAbortedMarker tamt = Mockito.spy(new TransactionAbortedMarker(executor));
81 assertTrue(tamt.lookupTransactionRecorder() instanceof TransactionRecorder);
85 public void testLookupTransactionRecorderExceptionFlow() throws TransactionRecorderServiceNotFoundException {
86 PowerMockito.mockStatic(FrameworkUtil.class);
87 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
88 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
89 PowerMockito.when(bundleContext.getServiceReference(TransactionRecorder.class.getName())).thenReturn(sref);
90 PowerMockito.when(bundleContext.<TransactionRecorder>getService(sref)).thenReturn(null);
91 ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
92 TransactionAbortedMarker tamt = Mockito.spy(new TransactionAbortedMarker(executor));
93 expectedEx.expect(TransactionRecorderServiceNotFoundException.class);
94 expectedEx.expectMessage("Cannot find service org.onap.appc.transactionrecorder.TransactionRecorder");
95 tamt.lookupTransactionRecorder();