47b6d100c975de02bd5583e5f6b47aa99556bcad
[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 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.Collections;
18 import javax.management.ObjectName;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.config.api.ConflictingVersionException;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.opendaylight.controller.config.api.ValidationException;
26 import org.opendaylight.controller.config.api.jmx.CommitStatus;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28 import org.osgi.framework.ServiceReference;
29
30 public class BlankTransactionServiceTrackerTest {
31     @Mock
32     private BlankTransactionServiceTracker.BlankTransaction blankTx;
33     private BlankTransactionServiceTracker tracker;
34
35     @Before
36     public void setUp() throws Exception {
37         MockitoAnnotations.initMocks(this);
38         doReturn(new CommitStatus(Collections.<ObjectName>emptyList(), Collections.<ObjectName>emptyList(),
39                 Collections.<ObjectName>emptyList())).when(blankTx).hit();
40         tracker = new BlankTransactionServiceTracker(blankTx, 10, MoreExecutors.newDirectExecutorService());
41     }
42
43     @Test
44     public void testBlankTransaction() throws Exception {
45         tracker.addingService(getMockServiceReference());
46         tracker.modifiedService(getMockServiceReference(), null);
47         tracker.removedService(getMockServiceReference(), null);
48         verify(blankTx, times(3)).hit();
49     }
50
51     @Test
52     public void testValidationException() throws Exception {
53         IllegalArgumentException argumentException = new IllegalArgumentException();
54         ValidationException validationException = ValidationException.createForSingleException(new ModuleIdentifier("m", "i"), argumentException);
55         doThrow(validationException).when(blankTx).hit();
56
57         tracker.addingService(getMockServiceReference());
58         verify(blankTx, times(10)).hit();
59     }
60
61     @Test
62     public void testConflictingException() throws Exception {
63         int maxAttempts = 2;
64         tracker = new BlankTransactionServiceTracker(blankTx, maxAttempts, MoreExecutors.newDirectExecutorService());
65
66         final ConflictingVersionException ex = new ConflictingVersionException();
67         doThrow(ex).when(blankTx).hit();
68
69         tracker.addingService(getMockServiceReference());
70         verify(blankTx, times(maxAttempts)).hit();
71     }
72
73     private ServiceReference<ModuleFactory> getMockServiceReference() {
74         return mock(ServiceReference.class);
75     }
76 }