0b9d6067351bdf1a9c2a98f6942328b8571892a8
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.test.locking;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29
30 import org.apache.curator.test.TestingServer;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextException;
35 import org.onap.policy.apex.context.parameters.ContextParameters;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
38 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
39
40 public class CuratorManagerTest {
41     // Zookeeper test server
42     TestingServer zkTestServer;
43
44     @Before
45     public void beforeTest() throws Exception {
46         zkTestServer = new TestingServer(62181);
47     }
48
49     @After
50     public void afterTest() throws IOException {
51         zkTestServer.stop();
52     }
53
54     @Test
55     public void testCuratorManagerConfigProperty() {
56         final ContextParameters contextParameters = new ContextParameters();
57         contextParameters.setLockManagerParameters(new CuratorLockManagerParameters());
58
59         ((CuratorLockManagerParameters) contextParameters.getLockManagerParameters()).setZookeeperAddress(null);
60
61         try {
62             final CuratorLockManager curatorManager = new CuratorLockManager();
63             curatorManager.init(new AxArtifactKey());
64             assertNull(curatorManager);
65         } catch (final ContextException e) {
66             assertEquals(e.getMessage(),
67                     "could not set up Curator locking, check if the curator Zookeeper address parameter is set correctly");
68         }
69
70         ((CuratorLockManagerParameters) contextParameters.getLockManagerParameters()).setZookeeperAddress("zooby");
71
72         try {
73             final CuratorLockManager curatorManager = new CuratorLockManager();
74             curatorManager.init(new AxArtifactKey());
75             fail("Curator manager test should fail");
76         } catch (final ContextException e) {
77             assertEquals(e.getMessage(),
78                     "could not connect to Zookeeper server at \"zooby\", see error log for details");
79         }
80
81         ((CuratorLockManagerParameters) contextParameters.getLockManagerParameters())
82                 .setZookeeperAddress("localhost:62181");
83
84         try {
85             final CuratorLockManager curatorManager0 = new CuratorLockManager();
86             curatorManager0.init(new AxArtifactKey());
87             assertNotNull(curatorManager0);
88
89             final CuratorLockManager curatorManager1 = new CuratorLockManager();
90             curatorManager1.init(new AxArtifactKey());
91             assertNotNull(curatorManager1);
92
93             curatorManager0.shutdown();
94             curatorManager1.shutdown();
95         } catch (final ContextException e) {
96             assertNull(e);
97         }
98     }
99 }