2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.manager.impl.osgi;
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;
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;
33 public class BlankTransactionServiceTrackerTest {
36 private BlankTransactionServiceTracker.BlankTransaction blankTx;
37 private BlankTransactionServiceTracker tracker;
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);
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();
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();
60 tracker.addingService(getMockServiceReference());
61 } catch (Exception e) {
62 verify(blankTx, times(1)).hit();
63 assertNotNull(e.getCause());
64 assertSame(validationException, e.getCause());
68 fail("Exception should have occurred for validation exception");
72 public void testConflictingException() throws Exception {
74 tracker = new BlankTransactionServiceTracker(blankTx, maxAttempts);
76 final ConflictingVersionException ex = new ConflictingVersionException();
77 doThrow(ex).when(blankTx).hit();
79 tracker.addingService(getMockServiceReference());
80 } catch (Exception e) {
81 verify(blankTx, times(maxAttempts)).hit();
85 fail("Exception should have occurred for conflicting exception");
88 private ServiceReference<ModuleFactory> getMockServiceReference() {
89 return mock(ServiceReference.class);