View Javadoc

1   package org.apache.helix.manager.zk;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.helix.PropertyPathConfig;
27  import org.apache.helix.PropertyType;
28  import org.apache.helix.TestHelper;
29  import org.apache.helix.ZNRecord;
30  import org.apache.helix.ZkUnitTestBase;
31  import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
32  import org.apache.log4j.Logger;
33  import org.testng.AssertJUnit;
34  import org.testng.annotations.AfterClass;
35  import org.testng.annotations.BeforeClass;
36  import org.testng.annotations.Test;
37  
38  
39  public class TestZKUtil extends ZkUnitTestBase
40  {
41    private static Logger LOG = Logger.getLogger(TestZKUtil.class);
42  
43    String clusterName = CLUSTER_PREFIX + "_" + getShortClassName();
44    ZkClient _zkClient;
45  
46    @BeforeClass()
47    public void beforeClass() throws IOException, Exception
48    {
49      _zkClient = new ZkClient(ZK_ADDR);
50      _zkClient.setZkSerializer(new ZNRecordSerializer());
51      if (_zkClient.exists("/" + clusterName))
52      {
53        _zkClient.deleteRecursive("/" + clusterName);
54      }
55  
56      boolean result = ZKUtil.isClusterSetup(clusterName, _zkClient);
57      AssertJUnit.assertFalse(result);
58      result = ZKUtil.isClusterSetup(null, _zkClient);
59      AssertJUnit.assertFalse(result);
60  
61      result = ZKUtil.isClusterSetup(null, null);
62      AssertJUnit.assertFalse(result);
63  
64      result = ZKUtil.isClusterSetup(clusterName, null);
65      AssertJUnit.assertFalse(result);
66  
67      TestHelper.setupEmptyCluster(_zkClient, clusterName);
68    }
69  
70    @AfterClass()
71    public void afterClass()
72    {
73      _zkClient.close();
74    }
75  
76    @Test()
77    public void testIsClusterSetup()
78    {
79      boolean result = ZKUtil.isClusterSetup(clusterName, _zkClient);
80      AssertJUnit.assertTrue(result);
81    }
82  
83    @Test()
84    public void testChildrenOperations()
85    {
86      List<ZNRecord> list = new ArrayList<ZNRecord>();
87      list.add(new ZNRecord("id1"));
88      list.add(new ZNRecord("id2"));
89      String path = PropertyPathConfig.getPath(PropertyType.CONFIGS, clusterName,
90          ConfigScopeProperty.PARTICIPANT.toString());
91      ZKUtil.createChildren(_zkClient, path, list);
92      list = ZKUtil.getChildren(_zkClient, path);
93      AssertJUnit.assertEquals(2, list.size());
94  
95      ZKUtil.dropChildren(_zkClient, path, list);
96      ZKUtil.dropChildren(_zkClient, path, new ZNRecord("id1"));
97      list = ZKUtil.getChildren(_zkClient, path);
98      AssertJUnit.assertEquals(0, list.size());
99  
100     ZKUtil.dropChildren(_zkClient, path, (List<ZNRecord>) null);
101   }
102 
103   @Test()
104   public void testUpdateIfExists()
105   {
106     String path = PropertyPathConfig.getPath(PropertyType.CONFIGS, clusterName,
107         ConfigScopeProperty.PARTICIPANT.toString(), "id3");
108     ZNRecord record = new ZNRecord("id4");
109     ZKUtil.updateIfExists(_zkClient, path, record, false);
110     AssertJUnit.assertFalse(_zkClient.exists(path));
111     _zkClient.createPersistent(path);
112     ZKUtil.updateIfExists(_zkClient, path, record, false);
113     AssertJUnit.assertTrue(_zkClient.exists(path));
114     record = _zkClient.<ZNRecord> readData(path);
115     AssertJUnit.assertEquals("id4", record.getId());
116   }
117 
118   @Test()
119   public void testSubtract()
120   {
121     String path = PropertyPathConfig.getPath(PropertyType.CONFIGS, clusterName,
122         ConfigScopeProperty.PARTICIPANT.toString(), "id5");
123     ZNRecord record = new ZNRecord("id5");
124     record.setSimpleField("key1", "value1");
125     _zkClient.createPersistent(path, record);
126     ZKUtil.subtract(_zkClient, path, record);
127     record = _zkClient.<ZNRecord> readData(path);
128     AssertJUnit.assertNull(record.getSimpleField("key1"));
129   }
130 
131   @Test()
132   public void testNullChildren()
133   {
134     String path = PropertyPathConfig.getPath(PropertyType.CONFIGS, clusterName,
135         ConfigScopeProperty.PARTICIPANT.toString(), "id6");
136     ZKUtil.createChildren(_zkClient, path, (List<ZNRecord>) null);
137   }
138 
139   @Test()
140   public void testCreateOrUpdate()
141   {
142     String path = PropertyPathConfig.getPath(PropertyType.CONFIGS, clusterName,
143         ConfigScopeProperty.PARTICIPANT.toString(), "id7");
144     ZNRecord record = new ZNRecord("id7");
145     ZKUtil.createOrUpdate(_zkClient, path, record, true, true);
146     record = _zkClient.<ZNRecord> readData(path);
147     AssertJUnit.assertEquals("id7", record.getId());
148   }
149 
150   @Test()
151   public void testCreateOrReplace()
152   {
153     String path = PropertyPathConfig.getPath(PropertyType.CONFIGS, clusterName,
154         ConfigScopeProperty.PARTICIPANT.toString(), "id8");
155     ZNRecord record = new ZNRecord("id8");
156     ZKUtil.createOrReplace(_zkClient, path, record, true);
157     record = _zkClient.<ZNRecord> readData(path);
158     AssertJUnit.assertEquals("id8", record.getId());
159     record = new ZNRecord("id9");
160     ZKUtil.createOrReplace(_zkClient, path, record, true);
161     record = _zkClient.<ZNRecord> readData(path);
162     AssertJUnit.assertEquals("id9", record.getId());
163   }
164 }