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.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;
30 public class BlankTransactionServiceTrackerTest {
32 private BlankTransactionServiceTracker.BlankTransaction blankTx;
33 private BlankTransactionServiceTracker tracker;
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());
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();
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();
57 tracker.addingService(getMockServiceReference());
58 verify(blankTx, times(10)).hit();
62 public void testConflictingException() throws Exception {
64 tracker = new BlankTransactionServiceTracker(blankTx, maxAttempts, MoreExecutors.newDirectExecutorService());
66 final ConflictingVersionException ex = new ConflictingVersionException();
67 doThrow(ex).when(blankTx).hit();
69 tracker.addingService(getMockServiceReference());
70 verify(blankTx, times(maxAttempts)).hit();
73 private ServiceReference<ModuleFactory> getMockServiceReference() {
74 return mock(ServiceReference.class);