Bug 5947: Increasing code coverage - mdsal-common-api
[mdsal.git] / common / mdsal-common-api / src / test / java / org / opendaylight / mdsal / common / api / BasicExceptionTests.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.mdsal.common.api;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13
14 import javax.annotation.Nonnull;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.common.api.clustering.CandidateAlreadyRegisteredException;
17 import org.opendaylight.mdsal.common.api.clustering.GenericEntity;
18 import org.opendaylight.yangtools.concepts.Path;
19 import org.opendaylight.yangtools.yang.common.RpcError;
20
21 public class BasicExceptionTests {
22
23     private static final RpcError RPC_ERROR = mock(RpcError.class);
24
25     @Test(expected = TransactionCommitFailedException.class)
26     public void transactionCommitFailedExceptionTest() throws Exception {
27         throw new TransactionCommitFailedException("test", RPC_ERROR);
28     }
29
30     @Test(expected = TransactionCommitDeadlockException.class)
31     public void transactionCommitDeadlockExceptionTest() throws Exception {
32         throw new TransactionCommitDeadlockException(TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER
33                 .get().getMessage(), RPC_ERROR);
34     }
35
36     @Test(expected = TransactionChainClosedException.class)
37     public void transactionChainClosedExceptionTest() throws Exception {
38         throw new TransactionChainClosedException("test");
39     }
40
41     @Test(expected = TransactionChainClosedException.class)
42     public void transactionChainClosedExceptionWithNullCauseTest() throws Exception {
43         throw new TransactionChainClosedException("test", null);
44     }
45
46     @Test(expected = ReadFailedException.class)
47     public void readFailedExceptionTest() throws Exception {
48         throw new ReadFailedException("test", RPC_ERROR);
49     }
50
51     @Test(expected = ReadFailedException.class)
52     public void readFailedExceptionWithThrowableTest() throws Exception {
53
54         throw new ReadFailedException("test", ReadFailedException.MAPPER.apply(
55                 new NullPointerException()).getCause(), RPC_ERROR);
56     }
57
58     @Test(expected = OptimisticLockFailedException.class)
59     public void optimisticLockFailedExceptionTest() throws Exception {
60         throw new OptimisticLockFailedException("test");
61     }
62
63     @Test(expected = DataStoreUnavailableException.class)
64     public void dataStoreUnavailableExceptionTest() throws Exception {
65         throw new DataStoreUnavailableException("test", null);
66     }
67
68     @Test(expected = DataValidationFailedException.class)
69     public void dataValidationFailedExceptionTest() throws Exception {
70         final TestClass testClass = new TestClass();
71         final DataValidationFailedException dataValidationFailedException =
72                 new DataValidationFailedException(TestClass.class, testClass, "test");
73
74         assertEquals(testClass, dataValidationFailedException.getPath());
75         assertEquals(TestClass.class, dataValidationFailedException.getPathType());
76
77         throw dataValidationFailedException;
78     }
79
80     @Test(expected = CandidateAlreadyRegisteredException.class)
81     public void candidateAlreadyRegisteredExceptionTest() throws Exception {
82         final GenericEntity genericEntity = mock(GenericEntity.class);
83         doReturn("testEntity").when(genericEntity).toString();
84         CandidateAlreadyRegisteredException candidateAlreadyRegisteredException =
85                 new CandidateAlreadyRegisteredException(genericEntity);
86         assertEquals(genericEntity, candidateAlreadyRegisteredException.getEntity());
87
88         throw candidateAlreadyRegisteredException;
89     }
90
91     private final class TestClass implements Path {
92         @Override
93         public boolean contains(@Nonnull Path other) {
94             return false;
95         }
96     }
97 }