View Javadoc

1   package org.apache.helix;
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.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.helix.ZNRecord;
28  import org.testng.Assert;
29  import org.testng.AssertJUnit;
30  import org.testng.annotations.Test;
31  
32  
33  public class TestZNRecord
34  {
35  
36    @Test
37    public void testEquals()
38    {
39      ZNRecord record1 = new ZNRecord("id");
40      record1.setSimpleField("k1", "v1");
41      record1.setMapField("m1", new HashMap<String, String>());
42      record1.getMapField("m1").put("k1", "v1");
43      record1.setListField("l1", new ArrayList<String>());
44      record1.getListField("l1").add("v1");
45      ZNRecord record2 = new ZNRecord("id");
46      record2.setSimpleField("k1", "v1");
47      record2.setMapField("m1", new HashMap<String, String>());
48      record2.getMapField("m1").put("k1", "v1");
49      record2.setListField("l1", new ArrayList<String>());
50      record2.getListField("l1").add("v1");
51  
52      AssertJUnit.assertEquals(record1, record2);
53      record2.setSimpleField("k2", "v1");
54      AssertJUnit.assertNotSame(record1, record2);
55    }
56  
57    @Test
58    public void testMerge()
59    {
60      ZNRecord record = new ZNRecord("record1");
61  
62      // set simple field
63      record.setSimpleField("simpleKey1", "simpleValue1");
64  
65      // set list field
66      List<String> list1 = new ArrayList<String>();
67      list1.add("list1Value1");
68      list1.add("list1Value2");
69      record.setListField("listKey1", list1);
70  
71      // set map field
72      Map<String, String> map1 = new HashMap<String, String>();
73      map1.put("map1Key1", "map1Value1");
74      record.setMapField("mapKey1", map1);
75      // System.out.println(record);
76  
77      ZNRecord updateRecord = new ZNRecord("updateRecord");
78  
79      // set simple field
80      updateRecord.setSimpleField("simpleKey2", "simpleValue2");
81  
82      // set list field
83      List<String> newList1 = new ArrayList<String>();
84      newList1.add("list1Value1");
85      newList1.add("list1Value2");
86      newList1.add("list1NewValue1");
87      newList1.add("list1NewValue2");
88      updateRecord.setListField("listKey1", newList1);
89  
90      List<String> list2 = new ArrayList<String>();
91      list2.add("list2Value1");
92      list2.add("list2Value2");
93      updateRecord.setListField("listKey2", list2);
94  
95      // set map field
96      Map<String, String> newMap1 = new HashMap<String, String>();
97      newMap1.put("map1NewKey1", "map1NewValue1");
98      updateRecord.setMapField("mapKey1", newMap1);
99  
100     Map<String, String> map2 = new HashMap<String, String>();
101     map2.put("map2Key1", "map2Value1");
102     updateRecord.setMapField("mapKey2", map2);
103     // System.out.println(updateRecord);
104 
105     record.merge(updateRecord);
106     // System.out.println(record);
107 
108     ZNRecord expectRecord = new ZNRecord("record1");
109     expectRecord.setSimpleField("simpleKey1", "simpleValue1");
110     expectRecord.setSimpleField("simpleKey2", "simpleValue2");
111     List<String> expectList1 = new ArrayList<String>();
112     expectList1.add("list1Value1");
113     expectList1.add("list1Value2");
114     expectList1.add("list1Value1");
115     expectList1.add("list1Value2");
116     expectList1.add("list1NewValue1");
117     expectList1.add("list1NewValue2");
118     expectRecord.setListField("listKey1", expectList1);
119     List<String> expectList2 = new ArrayList<String>();
120     expectList2.add("list2Value1");
121     expectList2.add("list2Value2");
122     expectRecord.setListField("listKey2", expectList2);
123     Map<String, String> expectMap1 = new HashMap<String, String>();
124     expectMap1.put("map1Key1", "map1Value1");
125     expectMap1.put("map1NewKey1", "map1NewValue1");
126     expectRecord.setMapField("mapKey1", expectMap1);
127     Map<String, String> expectMap2 = new HashMap<String, String>();
128     expectMap2.put("map2Key1", "map2Value1");
129     expectRecord.setMapField("mapKey2", expectMap2);
130     Assert.assertEquals(record, expectRecord, "Should be equal.");
131   }
132 }