1
1
package app .registry .com .aol .micro .server ;
2
2
3
+ import static java .util .stream .Collectors .joining ;
3
4
import static org .hamcrest .CoreMatchers .containsString ;
4
5
import static org .hamcrest .CoreMatchers .is ;
5
6
import static org .hamcrest .CoreMatchers .not ;
6
7
import static org .junit .Assert .assertThat ;
7
8
8
9
import java .text .SimpleDateFormat ;
9
10
import java .util .Date ;
11
+ import java .util .List ;
10
12
import java .util .concurrent .ExecutionException ;
13
+ import java .util .stream .Stream ;
11
14
15
+ import com .aol .micro .server .rest .client .RestClient ;
16
+ import com .fasterxml .jackson .core .type .TypeReference ;
12
17
import org .junit .After ;
13
18
import org .junit .Before ;
14
19
import org .junit .Test ;
25
30
public class RegistryAppRunner {
26
31
27
32
RestAgent rest = new RestAgent ();
28
- private final AsyncRestClient restAsync = new AsyncRestClient (
29
- 100 , 2000 );
33
+ private final AsyncRestClient restAsync = new AsyncRestClient (100 , 2000 );
30
34
MicroserverApp server ;
31
35
36
+ String baseUrl = "http://localhost:8080/registry-app/service-registry" ;
37
+
32
38
@ Before
33
39
public void startServer () {
34
-
35
- server = new MicroserverApp (
36
- () -> "registry-app" );
40
+ server = new MicroserverApp (() -> "registry-app" );
37
41
server .start ();
38
-
39
42
}
40
43
41
44
@ After
@@ -46,58 +49,115 @@ public void stopServer() {
46
49
@ Test
47
50
public void runAppAndBasicTest () throws InterruptedException , ExecutionException {
48
51
49
- SimpleDateFormat f = new SimpleDateFormat (
50
- "EEE" );
52
+ SimpleDateFormat f = new SimpleDateFormat ("EEE" );
51
53
String date = f .format (new Date ());
52
54
Thread .sleep (1000 );
53
55
54
- assertThat (rest .post ("http://localhost:8080/registry-app/service-registry/schedule" ),
55
- is ("{\" status\" :\" success\" }" ));
56
+ assertThat (rest .post (baseUrl + "/schedule" ), is ("{\" status\" :\" success\" }" ));
56
57
Thread .sleep (1000 );
57
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
58
- containsString ("[{\" port\" :8080," ));
59
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
60
- containsString ("externalPort\" :8080" ));
61
58
62
- sendPing (new RegisterEntry (
63
- 8081 , "use-ip" , "hello" , "world" , new Date (), "my-target" , 8082 ));
59
+ String listResponse = rest .getJson (baseUrl + "/list" );
60
+
61
+ assertThat (listResponse , containsString ("[{\" port\" :8080," ));
62
+ assertThat (listResponse , containsString ("externalPort\" :8080" ));
63
+
64
+ sendPing ("1" , 8081 , "use-ip" , "hello" , "world" , "my-target" , 8082 );
64
65
Thread .sleep (1000 );
65
- System .out .println (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ));
66
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
67
- containsString ("{\" port\" :8081," ));
68
-
69
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
70
- containsString ("\" target\" :\" my-target\" " ));
71
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
72
- containsString ("\" target\" :\" configured-target\" " ));
73
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
74
- not (containsString ("\" hostname\" :\" test-host\" " )));
75
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
76
- containsString ("\" formattedDate\" " ));
77
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
78
- containsString ("\" manifest\" " ));
79
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ),
80
- containsString ("Manifest-Version" ));
81
-
82
- assertThat (rest .getJson ("http://localhost:8080/registry-app/service-registry/list" ), containsString (date ));
66
+
67
+ listResponse = rest .getJson (baseUrl + "/list" );;
68
+
69
+ assertThat (listResponse , containsString ("{\" port\" :8081," ));
70
+ assertThat (listResponse , containsString ("\" target\" :\" my-target\" " ));
71
+ assertThat (listResponse , containsString ("\" target\" :\" configured-target\" " ));
72
+ assertThat (listResponse , not (containsString ("\" hostname\" :\" test-host\" " )));
73
+ assertThat (listResponse , containsString ("\" formattedDate\" " ));
74
+ assertThat (listResponse , containsString ("\" manifest\" " ));
75
+ assertThat (listResponse , containsString ("Manifest-Version" ));
76
+ assertThat (listResponse , containsString (date ));
83
77
84
78
}
85
79
86
- private void sendPing (RegisterEntry entry ) {
80
+ @ Test
81
+ public void filterTest () throws Exception {
82
+ Thread .sleep (1000 );
87
83
88
- try {
84
+ List <RegisterEntry > entries = list ();
85
+ assertThat (entries .size (), is (1 ));
89
86
90
- restAsync .post ("http://localhost:8080/registry-app/service-registry/register" ,
91
- JacksonUtil .serializeToJson (entry ))
92
- .join ();
93
- } catch (Exception e ) {
87
+ sendPing ("121" , 8080 , "host1" , "module1" , "context1" , "target1" , 9080 );
88
+ sendPing ("122" , 8080 , "host2" , "module1" , "context1" , "target1" , 9080 );
89
+ sendPing ("131" , 6080 , "host3" , "module2" , "context2" , "target2" , 7080 );
90
+ sendPing ("132" , 6080 , "host4" , "module2" , "context2" , "target2" , 7080 );
91
+
92
+ entries = list ();
93
+ assertThat (entries .size (), is (5 ));
94
+
95
+ entries = list ("port=8080" );
96
+ assertThat (entries .size (), is (3 ));
97
+
98
+ entries = list ("port=8080" , "externalPort=9080" );
99
+ System .out .println (entries );
100
+ assertThat (entries .size (), is (2 ));
94
101
95
- }
102
+ entries = list ("port=8080" , "externalPort=9080" , "module=module" , "context=context1" );
103
+ assertThat (entries .size (), is (2 ));
104
+
105
+ entries = list ("port=8080" , "externalPort=9080" , "module=module" , "context=context1" , "hostname=host1" );
106
+ assertThat (entries .size (), is (1 ));
107
+
108
+ entries = list ("port=8080" , "externalPort=9080" , "module=module1" , "context=context2" );
109
+ assertThat (entries .size (), is (0 ));
110
+
111
+ entries = list ("manifest.Implementation-Version=version" );
112
+ assertThat (entries .size (), is (4 ));
113
+
114
+ entries = list ("manifest.Implementation-Version=version1" );
115
+ assertThat (entries .size (), is (4 ));
116
+
117
+ entries = list ("manifest.Implementation-Version=version121" );
118
+ assertThat (entries .size (), is (1 ));
119
+
120
+ entries = list ("manifest.Implementation-revision=rev12" );
121
+ assertThat (entries .size (), is (2 ));
122
+
123
+ entries = list ("manifest.Implementation-Timestamp=2017_13" );
124
+ assertThat (entries .size (), is (2 ));
125
+
126
+ entries = list ("health=OK" );
127
+ assertThat (entries .size (), is (5 ));
128
+
129
+ List <String > list = JacksonUtil .convertFromJson (rest .getJson (baseUrl + "/list?port=OK" ), new TypeReference <List <String >>() {});
130
+ assertThat (list .size (), is (1 ));
131
+ assertThat (list .get (0 ), is ("Bad Request: 'OK' is not a valid number." ));
132
+
133
+ list = JacksonUtil .convertFromJson (rest .getJson (baseUrl + "/list?health=Suspended" ), new TypeReference <List <String >>() {});
134
+ assertThat (list .size (), is (1 ));
135
+ assertThat (list .get (0 ), is ("Bad Request: 'Suspended' is not valid, valid values are [OK, ERROR]" ));
96
136
}
97
137
98
- public static void main (String [] args ) {
99
- RegistryAppRunner appRunner = new RegistryAppRunner ( );
100
- appRunner . startServer ( );
138
+ private List < RegisterEntry > list (String ... parameters ) {
139
+ String url = baseUrl + "/list?" + Stream . of ( parameters ). collect ( joining ( "&" ) );
140
+ return JacksonUtil . convertFromJson ( rest . getJson ( url ), new TypeReference < List < RegisterEntry >>() {} );
101
141
}
102
142
143
+ private void sendPing (String uuid , int port , String hostName , String module , String context , String target , int externalPort ) {
144
+ try {
145
+ RegisterEntry re = RegisterEntry .builder ()
146
+ .port (port )
147
+ .hostname (hostName )
148
+ .module (module )
149
+ .context (context )
150
+ .time (new Date ())
151
+ .uuid (uuid )
152
+ .target (target )
153
+ .externalPort (externalPort )
154
+ .build ();
155
+ re .getManifest ().put ("Implementation-revision" , "rev" + uuid );
156
+ re .getManifest ().put ("Implementation-Version" , "version" + uuid );
157
+ re .getManifest ().put ("Implementation-Timestamp" , "2017_" + uuid );
158
+ restAsync .post ("http://localhost:8080/registry-app/service-registry/register" ,
159
+ JacksonUtil .serializeToJson (re ))
160
+ .get ();
161
+ } catch (Exception e ) {}
162
+ }
103
163
}
0 commit comments