LifecyclePlugin extends AbstractEC2InstancePlugin { @Override protected void process(final EC2InstanceContext context) { final LifecycleEntity lifecycleEntity = new LifecycleEntity(); lifecycleEntity.setEventType(context.getEventName()); lifecycleEntity.setEventDate(getLifecycleDate(context)); lifecycleEntity.setAccountId(context.getAccountId()); lifecycleEntity.setRegion(context.getRegionAsString()); lifecycleEntity.setInstanceId(context.getInstanceId()); context.getAmiId().ifPresent(lifecycleEntity::setImageId); context.getAmi().map(Image::getName).ifPresent(lifecycleEntity::setImageName); final Optional<ApplicationEntity> application = context.getApplicationId().map(ApplicationEntity::new); if (!application.isPresent()) { log.warn("Could not determine applicationId. Skip processing of LifecyclePlugin."); return; } final Optional<VersionEntity> version = context.getVersionId().map(VersionEntity::new); if (!version.isPresent()) { log.warn("Could not determine versionId. Skip processing of LifecyclePlugin."); return; } applicationLifecycleService.saveLifecycle(application.get(), version.get(), lifecycleEntity); } @Autowired  LifecyclePlugin(final EC2InstanceContextProvider contextProvider,
                           final ApplicationLifecycleService applicationLifecycleService);   }
@Test public void testProcessRunInstanceEvent() throws Exception { when(contextMock.getEventName()).thenReturn(RUN_INSTANCES); when(contextMock.getInstanceJson()).thenReturn( "{\"launchTime\": 1434616306000}"); when(contextMock.getAmiId()).thenReturn(Optional.of("ami-01234567")); when(contextMock.getAmi()).thenReturn(Optional.empty()); when(contextMock.getApplicationId()).thenReturn(Optional.of("hello-world")); when(contextMock.getVersionId()).thenReturn(Optional.of("1.0")); plugin.process(contextMock); verify(applicationLifecycleServiceMock).saveLifecycle(any(), any(), any()); }
@Test public void testMissingApplicationId() throws Exception { when(contextMock.getEventName()).thenReturn(RUN_INSTANCES); when(contextMock.getInstanceJson()).thenReturn( "{\"launchTime\": 1434616306000}"); when(contextMock.getAmiId()).thenReturn(Optional.of("ami-01234567")); when(contextMock.getAmi()).thenReturn(Optional.empty()); when(contextMock.getApplicationId()).thenReturn(Optional.empty()); when(contextMock.getVersionId()).thenReturn(Optional.of("1.0")); plugin.process(contextMock); }
@Test public void testMissingVersionId() throws Exception { when(contextMock.getEventName()).thenReturn(RUN_INSTANCES); when(contextMock.getInstanceJson()).thenReturn( "{\"launchTime\": 1434616306000}"); when(contextMock.getAmiId()).thenReturn(Optional.of("ami-01234567")); when(contextMock.getAmi()).thenReturn(Optional.empty()); when(contextMock.getApplicationId()).thenReturn(Optional.of("hello-world")); when(contextMock.getVersionId()).thenReturn(Optional.empty()); plugin.process(contextMock); }
@Test public void testProcessStartInstanceEvent() throws Exception { final CloudTrailEventData eventData = mock(CloudTrailEventData.class); when(eventData.getEventTime()).thenReturn(new Date()); when(contextMock.getEventName()).thenReturn(START_INSTANCES); when(contextMock.getEvent()).thenReturn(new CloudTrailEvent(eventData, null)); when(contextMock.getAmiId()).thenReturn(Optional.of("ami-01234567")); when(contextMock.getAmi()).thenReturn(Optional.empty()); when(contextMock.getApplicationId()).thenReturn(Optional.of("hello-world")); when(contextMock.getVersionId()).thenReturn(Optional.of("1.0")); plugin.process(contextMock); verify(applicationLifecycleServiceMock).saveLifecycle(any(), any(), any()); }