/src/test/java/org/atlasapi/feeds/radioplayer/upload/RadioPlayerUploadHealthProbeTest.java

https://github.com/atlasapi/atlas-feeds · Java · 132 lines · 94 code · 37 blank · 1 comment · 0 complexity · 4af0350771f3190c4d788e0c2307c3b2 MD5 · raw file

  1. package org.atlasapi.feeds.radioplayer.upload;
  2. import org.atlasapi.feeds.radioplayer.RadioPlayerService;
  3. import org.atlasapi.feeds.radioplayer.RadioPlayerServices;
  4. import org.atlasapi.feeds.upload.FileUploadResult;
  5. import org.atlasapi.feeds.upload.FileUploadResult.FileUploadResultType;
  6. import org.atlasapi.feeds.upload.persistence.MongoFileUploadResultStore;
  7. import org.atlasapi.media.entity.Publisher;
  8. import com.metabroadcast.common.health.ProbeResult;
  9. import com.metabroadcast.common.health.ProbeResult.ProbeResultType;
  10. import com.metabroadcast.common.persistence.MongoTestHelper;
  11. import com.metabroadcast.common.persistence.mongo.DatabasedMongo;
  12. import com.metabroadcast.common.time.DateTimeZones;
  13. import com.metabroadcast.common.time.DayRangeGenerator;
  14. import com.metabroadcast.common.time.TimeMachine;
  15. import com.google.common.collect.Iterables;
  16. import com.mongodb.BasicDBObject;
  17. import com.mongodb.BasicDBObjectBuilder;
  18. import org.joda.time.DateTime;
  19. import org.junit.After;
  20. import org.junit.BeforeClass;
  21. import org.junit.Test;
  22. import static org.atlasapi.feeds.radioplayer.upload.FileType.PI;
  23. import static org.atlasapi.feeds.upload.FileUploadResult.FileUploadResultType.FAILURE;
  24. import static org.atlasapi.feeds.upload.FileUploadResult.FileUploadResultType.SUCCESS;
  25. import static org.hamcrest.MatcherAssert.assertThat;
  26. import static org.hamcrest.Matchers.equalTo;
  27. import static org.hamcrest.Matchers.is;
  28. public class RadioPlayerUploadHealthProbeTest {
  29. private static final Publisher PUBLISHER = Publisher.BBC;
  30. private static final String REMOTE_SERVICE_ID = "remote";
  31. private static final String DATE_FORMAT = "yyyyMMdd";
  32. private static final RadioPlayerService SERVICE = RadioPlayerServices.all.get("340");
  33. //TODO: remove mongo and mock RadioPlayerUploadResultStore
  34. private final static DatabasedMongo mongo = MongoTestHelper.anEmptyTestDatabase();
  35. private final RadioPlayerUploadResultStore recorder = new UploadResultStoreBackedRadioPlayerResultStore(new MongoFileUploadResultStore(mongo));
  36. private final TimeMachine clock = new TimeMachine(new DateTime(DateTimeZones.UTC));
  37. private final RadioPlayerUploadHealthProbe probe = new RadioPlayerUploadHealthProbe(clock, REMOTE_SERVICE_ID, PUBLISHER, recorder, SERVICE, new DayRangeGenerator().withLookAhead(0).withLookBack(0));
  38. @BeforeClass
  39. public static void setup() {
  40. mongo.collection("uploads").createIndex(new BasicDBObjectBuilder().add("service",1).add("id", 1).add("time", -1).get());
  41. }
  42. @After
  43. public void tearDown() {
  44. mongo.collection("uploads").remove(new BasicDBObject());
  45. }
  46. @Test
  47. public void testProbe() {
  48. ProbeResult result = probe.probe();
  49. assertThat(Iterables.size(result.entries()), is(equalTo(3)));
  50. assertThat(Iterables.getFirst(result.entries(), null).getType(), is(equalTo(ProbeResultType.INFO)));
  51. assertThat(Iterables.getFirst(result.entries(), null).getValue(), is(equalTo("No Data")));
  52. recorder.record(successfulResult(new DateTime(DateTimeZones.UTC)));
  53. result = probe.probe();
  54. assertThat(Iterables.size(result.entries()), is(equalTo(3)));
  55. assertThat(Iterables.getFirst(result.entries(), null).getType(), is(equalTo(ProbeResultType.SUCCESS)));
  56. recorder.record(failedResult(new DateTime(DateTimeZones.UTC)));
  57. result = probe.probe();
  58. assertThat(Iterables.size(result.entries()), is(equalTo(3)));
  59. assertThat(Iterables.getFirst(result.entries(), null).getType(), is(equalTo(ProbeResultType.FAILURE)));
  60. recorder.record(successfulResult(new DateTime(DateTimeZones.UTC)));
  61. result = probe.probe();
  62. assertThat(Iterables.size(result.entries()), is(equalTo(3)));
  63. assertThat(Iterables.getFirst(result.entries(), null).getType(), is(equalTo(ProbeResultType.SUCCESS)));
  64. }
  65. @Test
  66. public void testSuccessTimesOut() {
  67. DateTime midday = new DateTime(2014,01,28,12,00,00,000, DateTimeZones.UTC);
  68. clock.jumpTo(midday.plusHours(4).plusMinutes(1));
  69. recorder.record(successfulResult(midday));
  70. ProbeResult result = probe.probe();
  71. assertThat(Iterables.size(result.entries()), is(equalTo(3)));
  72. assertThat(Iterables.getFirst(result.entries(), null).getType(), is(equalTo(ProbeResultType.FAILURE)));
  73. }
  74. @Test
  75. public void testFutureFailureIsInfo() {
  76. RadioPlayerUploadHealthProbe probe = new RadioPlayerUploadHealthProbe(clock, REMOTE_SERVICE_ID, PUBLISHER, recorder, SERVICE, new DayRangeGenerator().withLookAhead(4).withLookBack(0));
  77. DateTime futureDay = new DateTime(DateTimeZones.UTC).plusDays(4);
  78. FileUploadResult upResult = new FileUploadResult(REMOTE_SERVICE_ID, String.format("%s_%s_PI.xml", futureDay.toString(DATE_FORMAT), SERVICE.getRadioplayerId()), new DateTime(DateTimeZones.UTC), FAILURE);
  79. RadioPlayerUploadResult rpResult = new RadioPlayerUploadResult(PI, SERVICE, futureDay.toLocalDate(), upResult, "");
  80. recorder.record(rpResult);
  81. ProbeResult result = probe.probe();
  82. assertThat(Iterables.size(result.entries()), is(equalTo(11)));
  83. assertThat(Iterables.get(result.entries(), 10).getType(), is(equalTo(ProbeResultType.INFO)));
  84. }
  85. public RadioPlayerUploadResult successfulResult(DateTime successDate) {
  86. return result(successDate, SUCCESS);
  87. }
  88. public RadioPlayerUploadResult failedResult(DateTime successDate) {
  89. return result(successDate, FAILURE);
  90. }
  91. public RadioPlayerUploadResult result(DateTime successDate, FileUploadResultType type) {
  92. return new RadioPlayerUploadResult(PI, SERVICE, successDate.toLocalDate(),
  93. new FileUploadResult(REMOTE_SERVICE_ID, String.format("%s_%s_PI.xml", successDate.toString(DATE_FORMAT), SERVICE.getRadioplayerId()), successDate, type).withRemoteProcessingResult(SUCCESS).withTransactionId("123"),"");
  94. }
  95. }