5f76d6579d5a1362921ec541fb2d8b2f6876db7a
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 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.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Properties;
29 import java.util.TreeSet;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.properties.exception.PropertyException;
33
34 public class DistributedLockPropertiesTest {
35
36     private Properties props;
37
38     /**
39      * Populates {@link #props}.
40      */
41     @Before
42     public void setUp() {
43         props = new Properties();
44
45         props.setProperty(DistributedLockProperties.DB_DRIVER, "my driver");
46         props.setProperty(DistributedLockProperties.DB_URL, "my url");
47         props.setProperty(DistributedLockProperties.DB_USER, "my user");
48         props.setProperty(DistributedLockProperties.DB_PASS, "my pass");
49         props.setProperty(DistributedLockProperties.TRANSIENT_ERROR_CODES, "10,-20,,,30");
50         props.setProperty(DistributedLockProperties.EXPIRE_CHECK_SEC, "100");
51         props.setProperty(DistributedLockProperties.RETRY_SEC, "200");
52         props.setProperty(DistributedLockProperties.MAX_RETRIES, "300");
53     }
54
55     @Test
56     public void test() throws PropertyException {
57         DistributedLockProperties dlp = new DistributedLockProperties(props);
58
59         assertEquals("my driver", dlp.getDbDriver());
60         assertEquals("my url", dlp.getDbUrl());
61         assertEquals("my user", dlp.getDbUser());
62         assertEquals("my pass", dlp.getDbPwd());
63         assertEquals("10,-20,,,30", dlp.getErrorCodeStrings());
64         assertEquals("[-20, 10, 30]", new TreeSet<>(dlp.getTransientErrorCodes()).toString());
65         assertEquals(100, dlp.getExpireCheckSec());
66         assertEquals(200, dlp.getRetrySec());
67         assertEquals(300, dlp.getMaxRetries());
68
69         assertTrue(dlp.isTransient(10));
70         assertTrue(dlp.isTransient(-20));
71         assertTrue(dlp.isTransient(30));
72
73         assertFalse(dlp.isTransient(-10));
74
75         // invalid value
76         props.setProperty(DistributedLockProperties.TRANSIENT_ERROR_CODES, "10,abc,30");
77
78         assertThatThrownBy(() -> new DistributedLockProperties(props)).isInstanceOf(PropertyException.class)
79                         .hasMessageContaining(DistributedLockProperties.TRANSIENT_ERROR_CODES);
80     }
81 }