ImageDataRetriever { public Pair<String, byte[]> getAnnotatedImage(final ImageDataRetrievalContext context) { if (context == null) { throw new IllegalArgumentException("The image data retrieval context cannot be null; The" + " parameters are is required to fetch the image "); } ImageProperties imageProperties = new ImageProperties(); String chartName = context.getTriggeredMetric().getDisplayName(); if (!Strings.isNullOrEmpty(chartName)) { imageProperties.setChartName(chartName); } if (context.getNotificationStatus() == NotificationStatus.TRIGGERED) { Long triggerFiredTime = context.getTriggerFiredTime() .orElseGet(() -> { logger.error("Trigger fired time is required to generate the image for alert ID "+ context.getAlert().getId() +". Defaulting to use the current timestamp"); return System.currentTimeMillis(); }); imageProperties.setShadeYAxisArea(getYAxisCoordinates(triggerFiredTime, context.getTrigger().getInertia())); imageProperties.setShadeXAxisArea(getXAxisCoordinates(context.getTrigger())); } return imageService.generateAndStoreImage(ImmutableList.of(context.getTriggeredMetric()), imageProperties, true); } @Inject  ImageDataRetriever(ImageService imageService, SystemConfiguration systemConfiguration); String getImageURL(final Pair<String, byte[]> imageDetails); Pair<String, byte[]> getAnnotatedImage(final ImageDataRetrievalContext context);  }
@Test(expected = IllegalArgumentException.class) public void testRetrievingAnnotatedImageWhenNotificationContextIsNull() { Pair<String, byte[]> imageDetails = imageDataRetriever.getAnnotatedImage(null); }
@Test public void testRetrievingAnnotatedImageWhenTriggerTypeIsGreaterThan() { Double triggerThreshold = 1D; Long triggerFiredTime = 1563692460000L; Long inertia = 1800*1000L; ImageDataRetrievalContext context = getImageDataRetrievalContext(Trigger.TriggerType.GREATER_THAN, triggerFiredTime, inertia, triggerThreshold, NotificationStatus.TRIGGERED); ArgumentMatcher<ImageProperties> imageProperties = new ArgumentMatcher<ImageProperties>() { @Override public boolean matches(ImageProperties imageProperties) { return imageProperties.getShadeXAxisArea().size() == 1 && imageProperties.getShadeYAxisArea().size() == 1; } }; ArgumentMatcher<List<Metric>> metricList = new ArgumentMatcher<List<Metric>>() { @Override public boolean matches(List<Metric> metricList) { return metricList.size() == 1; } }; when(imageServiceMock.generateAndStoreImage(argThat(metricList), argThat(imageProperties), eq(true))).thenReturn(Pair.of("1", "TestString".getBytes())); Pair<String, byte[]> imageDetails = imageDataRetriever.getAnnotatedImage(context); verify(imageServiceMock, times(1)).generateAndStoreImage(argThat(metricList), argThat(imageProperties), eq(true)); }
@Test public void testRetrievingAnnotatedImageWhenTriggerStatusIsCleared() { Double triggerThreshold = 1D; Long triggerFiredTime = 1563692460000L; Long inertia = 1800*1000L; ImageDataRetrievalContext context = getImageDataRetrievalContext(Trigger.TriggerType.GREATER_THAN, triggerFiredTime, inertia, triggerThreshold, NotificationStatus.CLEARED); ArgumentMatcher<ImageProperties> imageProperties = new ArgumentMatcher<ImageProperties>() { @Override public boolean matches(ImageProperties imageProperties) { return imageProperties.getShadeYAxisArea() == null && imageProperties.getShadeXAxisArea() == null; } }; when(imageServiceMock.generateAndStoreImage(eq(ImmutableList.of(context.getTriggeredMetric())), argThat(imageProperties), eq(true))).thenReturn(Pair.of("1", "TestString".getBytes())); Pair<String, byte[]> imageDetails = imageDataRetriever.getAnnotatedImage(context); verify(imageServiceMock, times(1)).generateAndStoreImage( eq(ImmutableList.of(context.getTriggeredMetric())), argThat(imageProperties), eq(true)); }