Skip to content

Commit d96cada

Browse files
author
manpreet.singh
committed
Added unit test for SSE entity
1 parent edf8c1c commit d96cada

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

sseclient/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,37 @@ dependencies {
1313
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'
1414
compile group: 'org.apache.httpcomponents', name: 'httpclient-cache', version: '4.5.12'
1515
compile group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.4'
16+
testCompile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.2'
17+
testCompile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.2'
18+
testCompile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.10.2'
19+
testCompile group: 'commons-io', name: 'commons-io', version: '2.8.0'
20+
testCompile group: 'org.testng', name: 'testng', version: '7.1.0'
1621
}
22+
23+
jar {
24+
manifest {
25+
attributes 'Implementation-Title': 'Apache SSE client',
26+
'Implementation-Version': '1.0'
27+
}
28+
}
29+
30+
task fatJar(type: Jar) {
31+
version("1.0-uber")
32+
manifest {
33+
attributes 'Implementation-Title': 'Apache SSE client',
34+
'Implementation-Version': '1.0'
35+
}
36+
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
37+
with jar
38+
}
39+
40+
test {
41+
useTestNG() {
42+
def builder = suiteXmlBuilder();
43+
builder.suite(name: 'sse-tests') {
44+
test(name: 'sse-tests') {
45+
builder.packages { 'package'(name: 'org.apache.client.sse.*') {} }
46+
}
47+
}
48+
}
49+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.apache.client.sse;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.testng.Assert;
6+
import org.testng.annotations.BeforeClass;
7+
import org.testng.annotations.Test;
8+
9+
import com.fasterxml.jackson.databind.JsonNode;
10+
11+
import org.apache.commons.io.FileUtils;
12+
13+
import java.io.*;
14+
import java.nio.CharBuffer;
15+
import java.nio.file.Paths;
16+
17+
/**
18+
* Unit tests to check that the SseEntity processes the char buffers correctly.
19+
*/
20+
public class SseEntityTest {
21+
22+
private final SseEntity sseEntity = new SseEntity(null);
23+
24+
@BeforeClass
25+
public void pushBuffer() throws IOException {
26+
String eventsFilePath = Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "raw_events.txt").toString();
27+
String content = FileUtils.readFileToString(new File(eventsFilePath), "UTF-8");
28+
CharBuffer buffer = CharBuffer.allocate(1024);
29+
buffer.put(content);
30+
buffer.rewind();
31+
sseEntity.pushBuffer(buffer, null);
32+
System.out.println("Size: " + sseEntity.getEvents().size());
33+
}
34+
35+
@Test(groups = { "unit" })
36+
public void testLastEventIDMatch() {
37+
Assert.assertEquals(sseEntity.getLastEventId(), "dsjfnwuis");
38+
}
39+
40+
@Test(groups = { "unit" })
41+
public void testEventsCreated() {
42+
Assert.assertTrue(sseEntity.hasMoreEvents());
43+
Assert.assertEquals(sseEntity.getEvents().size(), 5);
44+
}
45+
46+
@Test(groups = { "unit" }, dependsOnMethods = { "testEventsCreated" })
47+
public void testEventParsing() throws JsonProcessingException {
48+
Event firstEvent = sseEntity.getEvents().poll();
49+
Assert.assertNotNull(firstEvent);
50+
Assert.assertEquals(firstEvent.getId(), "sdjkf36ff");
51+
Assert.assertEquals(firstEvent.getEvent(), "added_account");
52+
Assert.assertEquals(firstEvent.getRetry(), 5000);
53+
54+
ObjectMapper jsonMapper = new ObjectMapper();
55+
JsonNode jsonNode = jsonMapper.readTree(firstEvent.getData());
56+
Assert.assertEquals(jsonNode.get("account_name").asText(), "event_entity_1");
57+
Assert.assertEquals(jsonNode.get("owner").asText(), "[email protected]");
58+
Assert.assertEquals(jsonNode.get("type").asText(), "basic");
59+
}
60+
61+
@Test(groups = { "unit" }, dependsOnMethods = { "testEventParsing" })
62+
public void testEventQueue() {
63+
Assert.assertEquals(sseEntity.getEvents().size(), 4);
64+
Assert.assertEquals(sseEntity.getEvents().poll().getId(), "kjewf438sd");
65+
Assert.assertEquals(sseEntity.getEvents().poll().getId(), "sljf48fsmc");
66+
Assert.assertEquals(sseEntity.getEvents().poll().getId(), "jfu438fkjv");
67+
Assert.assertEquals(sseEntity.getEvents().poll().getId(), "dsjfnwuis");
68+
Assert.assertFalse(sseEntity.hasMoreEvents());
69+
}
70+
71+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
id:sdjkf36ff
2+
event:added_account
3+
retry:5000
4+
data:{
5+
data:"account_name":"event_entity_1",
6+
data:"owner":"[email protected]",
7+
data:"type":"basic"
8+
data:}
9+
10+
id:kjewf438sd
11+
event:updated_account
12+
data:{
13+
data:"account_name":"event_entity_1",
14+
data:"owner":"[email protected]",
15+
data:"type":"premium"
16+
data:}
17+
18+
19+
id:sljf48fsmc
20+
event:added_account
21+
data:{
22+
data:"account_name":"event_entity_2",
23+
data:"owner":"[email protected]",
24+
data:"type":"enterprise"
25+
data:}
26+
27+
28+
id:jfu438fkjv
29+
event:added_account
30+
data:{
31+
data:"account_name":"event_entity_3",
32+
data:"owner":"[email protected]",
33+
data:"type":"basic"
34+
data:}
35+
36+
37+
id:dsjfnwuis
38+
event:deleted_account
39+
data:{
40+
data:"account_name":"event_entity_1"
41+
data:}
42+

0 commit comments

Comments
 (0)