/** * Generates the HTTP POST to send to the endpoint. * * @param transmission * The transmission to send. * @return The completed {@link HttpPost} object */ private HttpPost createTransmissionPostRequest(Transmission transmission) { HttpPost request = new HttpPost(serverUri); request.addHeader(CONTENT_TYPE_HEADER, transmission.getWebContentType()); request.addHeader(CONTENT_ENCODING_HEADER, transmission.getWebContentEncodingType()); ByteArrayEntity bae = new ByteArrayEntity(transmission.getContent()); request.setEntity(bae); return request; }
private static Transmission createMockTransmission() { byte[] mockContent = new byte[2]; Transmission tested = new Transmission(mockContent, MOCK_WEB_CONTENT_TYPE, MOCK_WEB_ENCODING_TYPE); return tested; } }
@Test public void testIncrementNumberOfSends() throws Exception { Transmission tested = createMockTransmission(); tested.incrementNumberOfSends(); assertEquals(1, tested.getNumberOfSends()); }
@Test public void testGetContent() throws Exception { byte[] mockContent = new byte[2]; Transmission tested = new Transmission(mockContent, MOCK_WEB_CONTENT_TYPE, MOCK_WEB_ENCODING_TYPE); assertSame(mockContent, tested.getContent()); }
List<String> originalItems = new ArrayList<String>(); if (args.getTransmission().getWebContentEncodingType() == "gzip") { new ByteArrayInputStream(args.getTransmission().getContent())); bufferedReader = new BufferedReader(new InputStreamReader(gis)); String line; for (String s : new String(args.getTransmission().getContent()).split("\r\n")) { originalItems.add(s);
@Test public void testFetchOldestFiles() throws Exception { File folder = tmpFolder.newFolder(TEMP_TEST_FOLDER+"2"); try { TransmissionFileSystemOutput tested = new TransmissionFileSystemOutput(folder.getAbsolutePath()); for (int i = 1; i <= 10; ++i) { String iAsString = String.valueOf(i); String content = MOCK_CONTENT + iAsString; tested.send(new Transmission(content.getBytes(), MOCK_CONTENT_TYPE_BASE + iAsString, MOCK_ENCODING_TYPE_BASE + iAsString)); TimeUnit.MILLISECONDS.sleep(150); // sleep a bit so 2 files can never have the same timestamp. } for (int i = 1; i <= 10; ++i) { Transmission transmission = tested.fetchOldestFile(); assertNotNull(transmission); String iAsString = String.valueOf(i); assertEquals(String.format("Wrong WebContentType %s", transmission.getWebContentType()), MOCK_CONTENT_TYPE_BASE + iAsString, transmission.getWebContentType()); assertEquals(String.format("Wrong WebContentEncodingType %s", transmission.getWebContentEncodingType()), MOCK_ENCODING_TYPE_BASE + iAsString, transmission.getWebContentEncodingType()); String fetchedContent = new String(transmission.getContent()); assertEquals(String.format("Wrong content %s", fetchedContent), MOCK_CONTENT + iAsString, fetchedContent); } Transmission transmission = tested.fetchOldestFile(); assertNull(transmission); } finally { if (folder.exists()) { FileUtils.deleteDirectory(folder); } } }
@Override public boolean send(Transmission transmission) { ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(transmission.getContent())), out); String[] strings = new String(out.toByteArray()).split(System.getProperty("line.separator")); testResultsVerifier.notifyEventsArrival(strings.length); } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; }
@Test public void testNumberOfSends() throws Exception { Transmission tested = createMockTransmission(); assertEquals(0, tested.getNumberOfSends()); }
boolean validateTransmissionAndSend(TransmissionHandlerArgs args) { if (args.getTransmission() != null && args.getTransmissionDispatcher() != null) { args.getTransmission().incrementNumberOfSends(); switch (args.getResponseCode()) { case TransmissionSendResult.REQUEST_TIMEOUT: case TransmissionSendResult.INTERNAL_SERVER_ERROR: case TransmissionSendResult.SERVICE_UNAVAILABLE: backoffAndSendTransmission(args); return true; default: InternalLogger.INSTANCE.trace("Http response code %s not handled by %s", args.getResponseCode(), this.getClass().getName()); return false; } } else if (args.getException() != null) { backoffAndSendTransmission(args); return true; } return false; }
@Test public void testIncrementNumberOfPersistence() throws Exception { Transmission tested = createMockTransmission(); tested.incrementNumberOfPersistence(); assertEquals(1, tested.getNumberOfPersistence()); }
@Test public void testGetWebContentEncodingType() throws Exception { Transmission tested = createMockTransmission(); assertEquals(MOCK_WEB_ENCODING_TYPE, tested.getWebContentEncodingType()); }
@Test public void testGetWebContentType() throws Exception { Transmission tested = createMockTransmission(); assertEquals(MOCK_WEB_CONTENT_TYPE, tested.getWebContentType()); }
@Test public void testVersion() throws Exception { Transmission tested = createMockTransmission(); assertEquals(1, tested.getVersion()); }
@Test public void testNumberOfPersistence() throws Exception { Transmission tested = createMockTransmission(); assertEquals(0, tested.getNumberOfPersistence()); }
private void backoffAndSendTransmission(TransmissionHandlerArgs args) { // It is possible for us to have a temporary blip in transmission // this setting will allow us to control how many instant retries we perform // before backing off the send if (args.getTransmission() != null && (args.getTransmission().getNumberOfSends() > transmissionPolicyManager.getMaxInstantRetries())) { this.transmissionPolicyManager.backoff(); } args.getTransmissionDispatcher().dispatch(args.getTransmission()); } }
/** * Provides the core logic for the retransmission * * @param args * The {@link TransmissionHandlerArgs} for this transmission. * @return Returns a pass/fail for handling this transmission. */ boolean validateTransmissionAndSend(TransmissionHandlerArgs args) { if (args.getRetryHeader() != null && args.getTransmission() != null && args.getTransmissionDispatcher() != null) { args.getTransmission().incrementNumberOfSends(); switch (args.getResponseCode()) { case TransmissionSendResult.THROTTLED: case TransmissionSendResult.THROTTLED_OVER_EXTENDED_TIME: suspendTransmissions(TransmissionPolicy.BLOCKED_BUT_CAN_BE_PERSISTED, args.getRetryHeader()); args.getTransmissionDispatcher().dispatch(args.getTransmission()); return true; default: InternalLogger.INSTANCE.trace("Http response code %s not handled by %s", args.getResponseCode(), this.getClass().getName()); return false; } } InternalLogger.INSTANCE.trace("Http response code %s not handled by %s.", args.getResponseCode(), this.getClass().getName()); return false; }
assertNotNull(serializationData.getContent()); assertEquals("application/x-json-stream", serializationData.getWebContentType()); assertEquals("gzip", serializationData.getWebContentEncodingType()); GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(serializationData.getContent())); try { ByteArrayOutputStream contents = new ByteArrayOutputStream();
@Test(expected = IllegalArgumentException.class) public void testNullContentType() throws Exception { byte[] mockContent = new byte[2]; new Transmission(mockContent, null, MOCK_WEB_ENCODING_TYPE); }
if (code > HttpStatus.SC_PARTIAL_CONTENT && transmission.getNumberOfSends() > this.transmissionPolicyManager.getMaxInstantRetries()) { return false; } else if (code == HttpStatus.SC_OK) {
@Test(expected = IllegalArgumentException.class) public void testNullContentEncodingType() throws Exception { byte[] mockContent = new byte[2]; new Transmission(mockContent, MOCK_WEB_CONTENT_TYPE, null); }