90e8bfc006ad08cb040b073a5c023d5ff66020f1
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distributed.locking;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.sql.SQLException;
28 import org.apache.commons.dbcp2.BasicDataSource;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.properties.exception.PropertyException;
33 import org.onap.policy.drools.persistence.SystemPersistence;
34
35 /**
36  * Partially tests DistributedLockingFeature; most of the methods are tested via
37  * {@link TargetLockTest}.
38  */
39 public class DistributedLockingFeatureTest {
40     private static final String EXPECTED = "expected exception";
41
42     private BasicDataSource dataSrc;
43
44     @BeforeClass
45     public static void setUpBeforeClass() throws Exception {
46         SystemPersistence.manager.setConfigurationDir("src/test/resources");
47     }
48
49     @Before
50     public void setUp() throws Exception {
51         dataSrc = mock(BasicDataSource.class);
52     }
53
54     @Test
55     public void testGetSequenceNumber() {
56         assertEquals(1000, new DistributedLockingFeature().getSequenceNumber());
57     }
58
59     @Test(expected = DistributedLockingFeatureException.class)
60     public void testAfterStart_PropEx() {
61         new DistributedLockingFeatureImpl(new PropertyException("prop", "val")).afterStart(null);
62     }
63
64     @Test(expected = DistributedLockingFeatureException.class)
65     public void testAfterStart_InterruptEx() {
66         new DistributedLockingFeatureImpl(new InterruptedException(EXPECTED)).afterStart(null);
67     }
68
69     @Test(expected = DistributedLockingFeatureException.class)
70     public void testAfterStart_OtherEx() {
71         new DistributedLockingFeatureImpl(new RuntimeException(EXPECTED)).afterStart(null);
72     }
73
74     @Test
75     public void testCleanLockTable() throws Exception {
76         when(dataSrc.getConnection()).thenThrow(new SQLException(EXPECTED));
77
78         new DistributedLockingFeatureImpl().afterStart(null);
79     }
80
81     /**
82      * Feature that overrides {@link #makeDataSource()}.
83      */
84     private class DistributedLockingFeatureImpl extends DistributedLockingFeature {
85         /**
86          * Exception to throw when {@link #makeDataSource()} is invoked.
87          */
88         private final Exception makeEx;
89
90         public DistributedLockingFeatureImpl() {
91             makeEx = null;
92         }
93
94         public DistributedLockingFeatureImpl(Exception ex) {
95             this.makeEx = ex;
96         }
97
98         @Override
99         protected BasicDataSource makeDataSource() throws Exception {
100             if (makeEx != null) {
101                 throw makeEx;
102             }
103
104             return dataSrc;
105         }
106     }
107 }