1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.consulextend.expose;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.onap.msb.apiroute.wrapper.consulextend.expose.ServiceModifyIndexFilter;
23 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableService;
24 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
25 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
26 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
28 import com.orbitz.consul.model.ConsulResponse;
29 import com.orbitz.consul.model.health.ImmutableNode;
31 public class ServiceModifyIndexFilterTest {
34 public void testfilter() {
35 ServiceModifyIndexFilter filter = new ServiceModifyIndexFilter();
38 List<ServiceHealth> list0 = new ArrayList<ServiceHealth>();
40 // id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
41 Service service0 = ImmutableService.builder().id("huangleibo1").port(0).address("").service("huangleibo")
42 .addTags("").createIndex(1).modifyIndex(1).build();
43 ServiceHealth serviceHealth0 = ImmutableServiceHealth.builder().service(service0)
44 .node(ImmutableNode.builder().node("").address("").build()).build();
45 list0.add(serviceHealth0);
47 ConsulResponse<List<ServiceHealth>> object0 =
48 new ConsulResponse<List<ServiceHealth>>(list0, 1, true, BigInteger.valueOf(1));
50 // list-size:1,id:huangleibo1,name:huangleibo,modifyIndex:1;the first time:return true
51 Assert.assertTrue(filter.filter(object0));
53 // list-size:1,id:huangleibo1,name:huangleibo,modifyIndex:1;same index:return false
54 Assert.assertFalse(filter.filter(object0));
56 /////////////////////////////////////////////////////////////////////////////////
58 List<ServiceHealth> list1 = new ArrayList<ServiceHealth>();
60 // id:huangleibo2,name:huangleibo,modifyIndex:1,createIndex:1
61 Service service1 = ImmutableService.builder().id("huangleibo2").port(0).address("").service("huangleibo")
62 .addTags("").createIndex(1).modifyIndex(1).build();
63 ServiceHealth serviceHealth1 = ImmutableServiceHealth.builder().service(service1)
64 .node(ImmutableNode.builder().node("").address("").build()).build();
66 list1.add(serviceHealth0);
67 list1.add(serviceHealth1);
69 ConsulResponse<List<ServiceHealth>> object1 =
70 new ConsulResponse<List<ServiceHealth>>(list1, 1, true, BigInteger.valueOf(1));
73 // id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
74 // id:huangleibo2,name:huangleibo,modifyIndex:1,createIndex:1
75 // size different,return true
76 Assert.assertTrue(filter.filter(object1));
78 //////////////////////////////////////////////////////////////////////////
79 List<ServiceHealth> list2 = new ArrayList<ServiceHealth>();
81 // id:huangleibo3,name:huangleibo,modifyIndex:1,createIndex:1
82 ImmutableService service2 = ImmutableService.builder().id("huangleibo3").port(0).address("")
83 .service("huangleibo").addTags("").createIndex(1).modifyIndex(1).build();
84 ServiceHealth serviceHealth2 = ImmutableServiceHealth.builder().service(service2)
85 .node(ImmutableNode.builder().node("").address("").build()).build();
86 list2.add(serviceHealth0);
87 list2.add(serviceHealth2);
89 ConsulResponse<List<ServiceHealth>> object2 =
90 new ConsulResponse<List<ServiceHealth>>(list2, 1, true, BigInteger.valueOf(1));
93 // id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
94 // id:huangleibo3,name:huangleibo,modifyIndex:1,createIndex:1
95 // instance id different,return true
96 Assert.assertTrue(filter.filter(object2));
99 //////////////////////////////////////////////////////////////////////////
100 List<ServiceHealth> list3 = new ArrayList<ServiceHealth>();
102 // edit modifyindex 1 to 2
103 Service service3 = service2.withModifyIndex(2);
104 ServiceHealth serviceHealth3 = ImmutableServiceHealth.builder().service(service3)
105 .node(ImmutableNode.builder().node("").address("").build()).build();
106 list3.add(serviceHealth0);
107 list3.add(serviceHealth3);
109 ConsulResponse<List<ServiceHealth>> object3 =
110 new ConsulResponse<List<ServiceHealth>>(list3, 1, true, BigInteger.valueOf(1));
113 // id:huangleibo1,name:huangleibo,modifyIndex:1,createIndex:1
114 // id:huangleibo3,name:huangleibo,modifyIndex:2,createIndex:1
115 // modifyIndex different,return true
116 Assert.assertTrue(filter.filter(object3));
118 // the same content,return false
119 Assert.assertFalse(filter.filter(object3));