4d342a60ade2d1259fab1363a55dba431ec200cf
[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.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.doThrow;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import java.util.Collections;
21 import javax.management.ObjectName;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.config.api.ConflictingVersionException;
27 import org.opendaylight.controller.config.api.ModuleIdentifier;
28 import org.opendaylight.controller.config.api.ValidationException;
29 import org.opendaylight.controller.config.api.jmx.CommitStatus;
30 import org.opendaylight.controller.config.spi.ModuleFactory;
31 import org.osgi.framework.ServiceReference;
32
33 public class BlankTransactionServiceTrackerTest {
34
35     @Mock
36     private BlankTransactionServiceTracker.BlankTransaction blankTx;
37     private BlankTransactionServiceTracker tracker;
38
39     @Before
40     public void setUp() throws Exception {
41         MockitoAnnotations.initMocks(this);
42         doReturn(new CommitStatus(Collections.<ObjectName>emptyList(), Collections.<ObjectName>emptyList(), Collections.<ObjectName>emptyList())).when(blankTx).hit();
43         tracker = new BlankTransactionServiceTracker(blankTx);
44     }
45
46     @Test
47     public void testBlankTransaction() throws Exception {
48         tracker.addingService(getMockServiceReference());
49         tracker.modifiedService(getMockServiceReference(), null);
50         tracker.removedService(getMockServiceReference(), null);
51         verify(blankTx, times(3)).hit();
52     }
53
54     @Test
55     public void testValidationException() throws Exception {
56         IllegalArgumentException argumentException = new IllegalArgumentException();
57         ValidationException validationException = ValidationException.createForSingleException(new ModuleIdentifier("m", "i"), argumentException);
58         doThrow(validationException).when(blankTx).hit();
59         try {
60             tracker.addingService(getMockServiceReference());
61         } catch (Exception e) {
62             verify(blankTx, times(1)).hit();
63             assertNotNull(e.getCause());
64             assertSame(validationException, e.getCause());
65             return;
66         }
67
68         fail("Exception should have occurred for validation exception");
69     }
70
71     @Test
72     public void testConflictingException() throws Exception {
73         int maxAttempts = 2;
74         tracker = new BlankTransactionServiceTracker(blankTx, maxAttempts);
75
76         final ConflictingVersionException ex = new ConflictingVersionException();
77         doThrow(ex).when(blankTx).hit();
78         try {
79             tracker.addingService(getMockServiceReference());
80         } catch (Exception e) {
81             verify(blankTx, times(maxAttempts)).hit();
82             return;
83         }
84
85         fail("Exception should have occurred for conflicting exception");
86     }
87
88     private ServiceReference<ModuleFactory> getMockServiceReference() {
89         return mock(ServiceReference.class);
90     }
91 }