77465fb681f009f9f736916e46f0d1120715e046
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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;
17
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.List;
21
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;
29
30 import com.orbitz.consul.model.ConsulResponse;
31 import com.orbitz.consul.model.health.ImmutableNode;
32
33 public class ServiceModifyIndexFilterTest {
34         
35         @Test
36         public void testfilter()
37         {
38                 ServiceModifyIndexFilter filter = new ServiceModifyIndexFilter();
39                 
40                 
41                 List<ServiceHealth> list0 = new ArrayList<ServiceHealth>();
42                 
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()
47                                 .service(service0)
48                                 .node(ImmutableNode.builder().node("").address("").build())
49                                 .build();       
50                 list0.add(serviceHealth0);
51                 
52                 ConsulResponse<List<ServiceHealth>> object0 = new ConsulResponse<List<ServiceHealth>>(list0,1,true,BigInteger.valueOf(1));
53
54                 //list-size:1,id:huangleibo1,name:huangleibo,modifyIndex:1;the first time:return true
55                 Assert.assertTrue(filter.filter(object0));
56                 
57                 //list-size:1,id:huangleibo1,name:huangleibo,modifyIndex:1;same index:return false
58                 Assert.assertFalse(filter.filter(object0));
59                 
60                 /////////////////////////////////////////////////////////////////////////////////
61                 
62                 List<ServiceHealth> list1 = new ArrayList<ServiceHealth>();
63                 
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()
68                                 .service(service1)
69                                 .node(ImmutableNode.builder().node("").address("").build())
70                                 .build();
71                 
72                 list1.add(serviceHealth0);
73                 list1.add(serviceHealth1);
74                 
75                 ConsulResponse<List<ServiceHealth>> object1 = new ConsulResponse<List<ServiceHealth>>(list1,1,true,BigInteger.valueOf(1));
76
77                 //list-size:2,
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));
82                 
83                 //////////////////////////////////////////////////////////////////////////
84                 List<ServiceHealth> list2 = new ArrayList<ServiceHealth>();
85                 
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()
90                                 .service(service2)
91                                 .node(ImmutableNode.builder().node("").address("").build())
92                                 .build();
93                 list2.add(serviceHealth0);
94                 list2.add(serviceHealth2);
95                 
96                 ConsulResponse<List<ServiceHealth>> object2 = new ConsulResponse<List<ServiceHealth>>(list2,1,true,BigInteger.valueOf(1));
97                 
98                 //list-size:2,
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));
103                 
104                 
105                 //////////////////////////////////////////////////////////////////////////
106                 List<ServiceHealth> list3 = new ArrayList<ServiceHealth>();
107                 
108                 //edit modifyindex 1 to 2
109                 Service service3 = service2.withModifyIndex(2);
110                 ServiceHealth serviceHealth3 = ImmutableServiceHealth.builder()
111                                 .service(service3)
112                                 .node(ImmutableNode.builder().node("").address("").build())
113                                 .build();
114                 list3.add(serviceHealth0);
115                 list3.add(serviceHealth3);
116                 
117                 ConsulResponse<List<ServiceHealth>> object3 = new ConsulResponse<List<ServiceHealth>>(list3,1,true,BigInteger.valueOf(1));
118                 
119                 //list-size:2,
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));
124                 
125                 //the same content,return false
126                 Assert.assertFalse(filter.filter(object3));
127                 
128         }
129 }