|
| 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 | +} |
0 commit comments