2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.distributed.locking;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
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;
36 * Partially tests DistributedLockingFeature; most of the methods are tested via
37 * {@link TargetLockTest}.
39 public class DistributedLockingFeatureTest {
40 private static final String EXPECTED = "expected exception";
42 private BasicDataSource dataSrc;
45 public static void setUpBeforeClass() throws Exception {
46 SystemPersistence.manager.setConfigurationDir("src/test/resources");
50 public void setUp() throws Exception {
51 dataSrc = mock(BasicDataSource.class);
55 public void testGetSequenceNumber() {
56 assertEquals(1000, new DistributedLockingFeature().getSequenceNumber());
59 @Test(expected = DistributedLockingFeatureException.class)
60 public void testAfterStart_PropEx() {
61 new DistributedLockingFeatureImpl(new PropertyException("prop", "val")).afterStart(null);
64 @Test(expected = DistributedLockingFeatureException.class)
65 public void testAfterStart_InterruptEx() {
66 new DistributedLockingFeatureImpl(new InterruptedException(EXPECTED)).afterStart(null);
69 @Test(expected = DistributedLockingFeatureException.class)
70 public void testAfterStart_OtherEx() {
71 new DistributedLockingFeatureImpl(new RuntimeException(EXPECTED)).afterStart(null);
75 public void testCleanLockTable() throws Exception {
76 when(dataSrc.getConnection()).thenThrow(new SQLException(EXPECTED));
78 new DistributedLockingFeatureImpl().afterStart(null);
82 * Feature that overrides {@link #makeDataSource()}.
84 private class DistributedLockingFeatureImpl extends DistributedLockingFeature {
86 * Exception to throw when {@link #makeDataSource()} is invoked.
88 private final Exception makeEx;
90 public DistributedLockingFeatureImpl() {
94 public DistributedLockingFeatureImpl(Exception ex) {
99 protected BasicDataSource makeDataSource() throws Exception {
100 if (makeEx != null) {