Fix Eclipse warnings in config-manager
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / BlankTransactionServiceTrackerTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.config.manager.impl.osgi;
10
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.Collections;
19 import javax.management.ObjectName;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.config.api.ConflictingVersionException;
25 import org.opendaylight.controller.config.api.ModuleIdentifier;
26 import org.opendaylight.controller.config.api.ValidationException;
27 import org.opendaylight.controller.config.api.jmx.CommitStatus;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.osgi.framework.ServiceReference;
30
31 public class BlankTransactionServiceTrackerTest {
32     @Mock
33     private BlankTransactionServiceTracker.BlankTransaction blankTx;
34     private BlankTransactionServiceTracker tracker;
35
36     @Before
37     public void setUp() throws Exception {
38         MockitoAnnotations.initMocks(this);
39         doReturn(new CommitStatus(Collections.<ObjectName>emptyList(), Collections.<ObjectName>emptyList(),
40                 Collections.<ObjectName>emptyList())).when(blankTx).hit();
41         tracker = new BlankTransactionServiceTracker(blankTx, 10, MoreExecutors.newDirectExecutorService());
42     }
43
44     @Test
45     public void testBlankTransaction() throws Exception {
46         tracker.addingService(getMockServiceReference());
47         tracker.modifiedService(getMockServiceReference(), null);
48         tracker.removedService(getMockServiceReference(), null);
49         verify(blankTx, times(3)).hit();
50     }
51
52     @Test
53     public void testValidationException() throws Exception {
54         IllegalArgumentException argumentException = new IllegalArgumentException();
55         ValidationException validationException = ValidationException.createForSingleException(new ModuleIdentifier("m", "i"), argumentException);
56         doThrow(validationException).when(blankTx).hit();
57
58         tracker.addingService(getMockServiceReference());
59         verify(blankTx, times(10)).hit();
60     }
61
62     @Test
63     public void testConflictingException() throws Exception {
64         int maxAttempts = 2;
65         tracker = new BlankTransactionServiceTracker(blankTx, maxAttempts, MoreExecutors.newDirectExecutorService());
66
67         final ConflictingVersionException ex = new ConflictingVersionException();
68         doThrow(ex).when(blankTx).hit();
69
70         tracker.addingService(getMockServiceReference());
71         verify(blankTx, times(maxAttempts)).hit();
72     }
73
74     private static ServiceReference<ModuleFactory> getMockServiceReference() {
75         return mock(ServiceReference.class);
76     }
77 }