-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEventHubUtilsTest.java
More file actions
351 lines (300 loc) · 13 KB
/
EventHubUtilsTest.java
File metadata and controls
351 lines (300 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (c) 2016 GE. All Rights Reserved.
* GE Confidential: Restricted Internal Distribution
*/
package com.ge.predix.eventhub.client;
import static com.ge.predix.eventhub.EventHubConstants.EnvironmentVariables.INSTANCE_NAME;
import static com.ge.predix.eventhub.EventHubConstants.EnvironmentVariables.UAA_INSTANCE_NAME;
import static com.ge.predix.eventhub.EventHubConstants.EnvironmentVariables.VCAP_SERVICES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.ge.predix.eventhub.*;
import com.ge.predix.eventhub.configuration.LoggerConfiguration;
import com.ge.predix.eventhub.test.categories.DefectTest;
import com.ge.predix.eventhub.test.categories.SanityTest;
import com.ge.predix.eventhub.test.categories.SmokeTest;
import com.google.protobuf.ByteString;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import com.ge.predix.eventhub.configuration.EventHubConfiguration;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EventHubUtilsTest {
@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
System.out.println("################## Starting test ###############: " + description.getMethodName());
}
};
@Test
@Category(SanityTest.class)
public void parseVCAPSForEventHubDetails() {
String VCAPS = System.getenv(VCAP_SERVICES);
String eventHubInstanceName = System.getenv(INSTANCE_NAME);
try {
EventHubConfiguration varsFromVCAPS = EventHubUtils.getEventHubDetailsFromVCAPS(VCAPS, eventHubInstanceName);
assertEquals(System.getenv("EVENTHUB_URI"), varsFromVCAPS.getHost());
assertEquals(Integer.parseInt(System.getenv("EVENTHUB_PORT")), varsFromVCAPS.getPort());
assertEquals(System.getenv("ZONE_ID"), varsFromVCAPS.getZoneID());
} catch (EventHubClientException.InvalidConfigurationException e) {
fail();
}
}
@Test
@Category(SanityTest.class)
public void parseVCAPSForUAADetails() {
String VCAPS = System.getenv(VCAP_SERVICES);
String uaaInstanceName = System.getenv(UAA_INSTANCE_NAME);
try {
String UaaURI = EventHubUtils.getUAADetailsFromVCAPS(VCAPS, uaaInstanceName);
assertEquals(System.getenv("AUTH_URL"), UaaURI);
} catch (EventHubClientException.InvalidConfigurationException e) {
fail();
}
}
@Test
@Category(SanityTest.class)
public void EventHubConfigFromEnv() {
try {
// test manually building vs getting from VCAPS
EventHubConfiguration configFromEnv = new EventHubConfiguration.Builder().fromEnvironmentVariables().build();
EventHubConfiguration configManual = new EventHubConfiguration.Builder()
.host(System.getenv("EVENTHUB_URI"))
.port(Integer.parseInt(System.getenv("EVENTHUB_PORT")))
.zoneID(System.getenv("ZONE_ID"))
.clientID(System.getenv("CLIENT_ID"))
.clientSecret(System.getenv("CLIENT_SECRET"))
.authURL(System.getenv("AUTH_URL"))
.build();
assertEquals(configFromEnv, configManual);
// test giving the event hub and uaa instance names to find in VCAP_SERVICES
final String eventHubInstanceName = System.getenv(INSTANCE_NAME);
final String uaaInstanceName = System.getenv(UAA_INSTANCE_NAME);
EventHubConfiguration configGiven = new EventHubConfiguration.Builder().fromEnvironmentVariables(eventHubInstanceName, uaaInstanceName).build();
assertEquals(configFromEnv, configGiven);
// test giving only the event hub instance name to find in VCAP_SERVICES
EventHubConfiguration configEventHubOnly = new EventHubConfiguration.Builder().fromEnvironmentVariablesWithInstance(eventHubInstanceName).automaticTokenRenew(false).build();
EventHubConfiguration configEventHubOnlyManual = new EventHubConfiguration.Builder()
.host(System.getenv("EVENTHUB_URI"))
.port(Integer.parseInt(System.getenv("EVENTHUB_PORT")))
.zoneID(System.getenv("ZONE_ID"))
.automaticTokenRenew(false)
.build();
assertEquals(configEventHubOnly, configEventHubOnlyManual);
EventHubConfiguration configAuthURLOnly = new EventHubConfiguration.Builder().fromEnvironmentVariables(System.getenv("AUTH_URL")).build();
assertEquals(configFromEnv, configAuthURLOnly);
} catch (EventHubClientException.InvalidConfigurationException e) {
fail();
}
}
@Test
@Category(SanityTest.class)
public void badEventHubConfig() {
boolean caught = false;
try {
EventHubConfiguration badConfig = new EventHubConfiguration.Builder().host("some host").build();
} catch (EventHubClientException.InvalidConfigurationException e) {
caught = true;
}
assertTrue(caught); // should catch that values are not set
}
@Test
@Category(SanityTest.class)
public void badVCAPS() {
int caught = 0;
// no VCAPS
try {
EventHubUtils.getEventHubDetailsFromVCAPS(null, "some instance name");
} catch (EventHubClientException.InvalidConfigurationException e) {
caught++;
}
// instance not in VCAPS
try {
EventHubUtils.getEventHubDetailsFromVCAPS("{\"VCAP_SERVICES\":{\"service name\":[{\"name\":\"some instance name\"}]}}",
"some bad instance name");
} catch (EventHubClientException.InvalidConfigurationException e) {
caught++;
}
// VCAPS structure is missing grpc protocol
try {
EventHubUtils.getEventHubDetailsFromVCAPS(
"{\"VCAP_SERVICES\":{\"service name\":[{\"name\":\"some instance name\", \"credentials\":{\"publish\":{\"protocol_details\":[{\"protocol\":\"bad protocal\"}]}}}]}}",
"some instance name");
} catch (EventHubClientException.InvalidConfigurationException e) {
caught++;
}
assertEquals(3, caught);
}
@Test
@Category(SanityTest.class)
public void instanceFromVCAPSSingleton() {
String VCAPS = System.getenv(VCAP_SERVICES);
String eventHubInstanceName = System.getenv(INSTANCE_NAME);
try {
// pull instance details once and after that the details should come from cache
EventHubConfiguration varsFromVCAPS = EventHubUtils.getEventHubDetailsFromVCAPS(VCAPS, eventHubInstanceName);
EventHubConfiguration varsFromHashMap = EventHubUtils.getEventHubDetailsFromVCAPS("junk", eventHubInstanceName);
assertEquals(varsFromVCAPS, varsFromHashMap);
} catch (EventHubClientException.InvalidConfigurationException e) {
System.out.println(e.getMessage());
fail();
}
}
@Test
@Category(SanityTest.class)
public void instanceFromVCAPSThreading() throws Exception {
final String VCAPS = System.getenv(VCAP_SERVICES);
final String eventHubInstanceName = System.getenv(INSTANCE_NAME);
final String uaaInstanceName = System.getenv(UAA_INSTANCE_NAME);
final Map<String, JsonNode> map = new ConcurrentHashMap<String, JsonNode>();
final CountDownLatch startSync = new CountDownLatch(1);
Thread eventHubThread = new Thread() {
public void run() {
try {
startSync.await();
} catch (Exception e) {
e.printStackTrace();
}
try {
map.put(eventHubInstanceName, EventHubUtils.findByServiceName(VCAPS, eventHubInstanceName));
} catch (EventHubClientException.InvalidConfigurationException e) {
fail();
}
}
};
Thread uaaThread = new Thread() {
public void run() {
try {
startSync.await();
} catch (Exception e) {
e.printStackTrace();
}
try {
map.put(uaaInstanceName, EventHubUtils.findByServiceName(VCAPS, uaaInstanceName));
} catch (EventHubClientException.InvalidConfigurationException e) {
fail();
}
}
};
eventHubThread.start();
uaaThread.start();
startSync.countDown();
eventHubThread.join();
uaaThread.join();
assertNotEquals(map.get(eventHubInstanceName), map.get(uaaInstanceName));
assertEquals(eventHubInstanceName, map.get(eventHubInstanceName).get("name").asText());
assertEquals(uaaInstanceName, map.get(uaaInstanceName).get("name").asText());
}
/**
* test that tests all the different log json generator functions
*
*/
@Test
@Category(SanityTest.class)
public void testJSONMaker(){
Ack a = Ack.newBuilder()
.setId("ack-id")
.setOffset(2)
.setPartition(10)
.setStatusCode(AckStatus.ACCEPTED)
.setZoneId("z-o-n-e--i-d")
.build();
EventHubUtils.makeJson(a);
List<Message> messagesList = new ArrayList();
for(int i=0; i<10;i++){
messagesList.add(Message.newBuilder()
.setId("mesasge-id-" + i)
.setBody(ByteString.copyFromUtf8("message-body"))
.setZoneId("zone-id")
.build()
);
}
PublishResponse publishResponse = PublishResponse.newBuilder()
.addAck(a)
.addAck(a)
.addAck(a)
.build();
EventHubUtils.makeJson(messagesList);
Throwable t = new Exception("I AM A EXCEPTION!!!");
EventHubUtils.formatJson("test-this",
"ack", a,
"messages", messagesList,
"throwable", t,
"publishResponse", publishResponse,
"innerJson", EventHubUtils.formatJson(
"single message", messagesList.get(0)
));
}
/**
* test to cycle though all the log levels
* there is no real way to assure they functioned properly
* but it will at least run the code
*/
@Test
@Category(SanityTest.class)
public void testLogLevels(){
LoggerConfiguration utilLoggingConfig = new LoggerConfiguration.Builder()
.logLevel(LoggerConfiguration.LogLevel.ALL)
.prettyPrintJsonLogs(true)
.useJUL(true)
.build();
LoggerConfiguration slf4jLoggingConfig = new LoggerConfiguration.Builder()
.logLevel(LoggerConfiguration.LogLevel.ALL)
.prettyPrintJsonLogs(true)
.build();
EventHubLogger utilLogger = new EventHubLogger(this.getClass(), utilLoggingConfig);
EventHubLogger slf4jLogger = new EventHubLogger(this.getClass(), slf4jLoggingConfig);
for(Level level : new Level[] {Level.OFF, Level.WARNING, Level.SEVERE, Level.FINE, Level.FINER, Level.FINEST, Level.INFO, Level.ALL}){
utilLogger.log(level, "log level", level.toString());
slf4jLogger.log(level, "log level", level.toString());
}
}
/**
* test serializing messages for logging with nested object, e.g.
* {
* "publisher_msg":{
* "msg":"ignoring values from config, don't apply to this publisher type",
* "ignores":[
* "timeout",
* 10000
* ]
* }
* }
*/
@Test
@Category(DefectTest.class)
public void test() {
final List<Object> ignores = new ArrayList<>();
final String timeoutKey = "timeout";
final long timeoutValue = 10000L;
ignores.add(timeoutKey);
ignores.add(timeoutValue);
JSONObject jsonObject = EventHubUtils.formatJson(
"publisher_msg",
"msg",
"ignoring values from config, don't apply to this publisher type",
"ignores",
ignores
);
final JSONArray ignoresJsonValue = jsonObject.getJSONObject("publisher_msg").getJSONArray("ignores");
Assert.assertEquals(ignores, ignoresJsonValue.toList());
}
}