1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend.expose;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.onap.msb.apiroute.wrapper.consulextend.expose.ServiceModifyIndexFilter;
25 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
26 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
27 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
28 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
30 import com.orbitz.consul.model.ConsulResponse;
31 import com.orbitz.consul.model.health.ImmutableNode;
33 public class ServiceModifyIndexFilterTest {
36 public void testfilter()
38 ServiceModifyIndexFilter filter = new ServiceModifyIndexFilter();
41 List<ServiceHealth> list0 = new ArrayList<ServiceHealth>();
43 //id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
44 Service service0 = ImmutableService.builder().id("huangleibo1").port(0).address("")
45 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
46 ServiceHealth serviceHealth0 = ImmutableServiceHealth.builder()
48 .node(ImmutableNode.builder().node("").address("").build())
50 list0.add(serviceHealth0);
52 ConsulResponse<List<ServiceHealth>> object0 = new ConsulResponse<List<ServiceHealth>>(list0,1,true,BigInteger.valueOf(1));
54 //list-size:1,id:huangleibo1,name:huangleibo,modifyIndex:1;the first time:return true
55 Assert.assertTrue(filter.filter(object0));
57 //list-size:1,id:huangleibo1,name:huangleibo,modifyIndex:1;same index:return false
58 Assert.assertFalse(filter.filter(object0));
60 /////////////////////////////////////////////////////////////////////////////////
62 List<ServiceHealth> list1 = new ArrayList<ServiceHealth>();
64 //id:huangleibo2,name:huangleibo,modifyIndex:1,createIndex:1
65 Service service1 = ImmutableService.builder().id("huangleibo2").port(0).address("")
66 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
67 ServiceHealth serviceHealth1 = ImmutableServiceHealth.builder()
69 .node(ImmutableNode.builder().node("").address("").build())
72 list1.add(serviceHealth0);
73 list1.add(serviceHealth1);
75 ConsulResponse<List<ServiceHealth>> object1 = new ConsulResponse<List<ServiceHealth>>(list1,1,true,BigInteger.valueOf(1));
78 //id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
79 //id:huangleibo2,name:huangleibo,modifyIndex:1,createIndex:1
80 //size different,return true
81 Assert.assertTrue(filter.filter(object1));
83 //////////////////////////////////////////////////////////////////////////
84 List<ServiceHealth> list2 = new ArrayList<ServiceHealth>();
86 //id:huangleibo3,name:huangleibo,modifyIndex:1,createIndex:1
87 ImmutableService service2 = ImmutableService.builder().id("huangleibo3").port(0).address("")
88 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
89 ServiceHealth serviceHealth2 = ImmutableServiceHealth.builder()
91 .node(ImmutableNode.builder().node("").address("").build())
93 list2.add(serviceHealth0);
94 list2.add(serviceHealth2);
96 ConsulResponse<List<ServiceHealth>> object2 = new ConsulResponse<List<ServiceHealth>>(list2,1,true,BigInteger.valueOf(1));
99 //id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
100 //id:huangleibo3,name:huangleibo,modifyIndex:1,createIndex:1
101 //instance id different,return true
102 Assert.assertTrue(filter.filter(object2));
105 //////////////////////////////////////////////////////////////////////////
106 List<ServiceHealth> list3 = new ArrayList<ServiceHealth>();
108 //edit modifyindex 1 to 2
109 Service service3 = service2.withModifyIndex(2);
110 ServiceHealth serviceHealth3 = ImmutableServiceHealth.builder()
112 .node(ImmutableNode.builder().node("").address("").build())
114 list3.add(serviceHealth0);
115 list3.add(serviceHealth3);
117 ConsulResponse<List<ServiceHealth>> object3 = new ConsulResponse<List<ServiceHealth>>(list3,1,true,BigInteger.valueOf(1));
120 //id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
121 //id:huangleibo3,name:huangleibo,modifyIndex:2,createIndex:1
122 //modifyIndex different,return true
123 Assert.assertTrue(filter.filter(object3));
125 //the same content,return false
126 Assert.assertFalse(filter.filter(object3));