KeyPairPlugin extends AbstractEC2InstancePlugin { @Override protected void process(final EC2InstanceContext context) { getKeyName(context).ifPresent( k -> violationSink.put( context.violation() .withType(EC2_WITH_KEYPAIR) .withPluginFullyQualifiedClassName(KeyPairPlugin.class) .withMetaInfo(ImmutableMap.of( "key_name", k, "ami_owner_id", context.getAmi().map(Image::getOwnerId).orElse(""), "ami_id", context.getAmiId().orElse(""), "ami_name", context.getAmi().map(Image::getName).orElse(""))) .build())); } KeyPairPlugin(final EC2InstanceContextProvider contextProvider, final ViolationSink violationSink);   }
@Test public void testProcessWithKeypair() throws Exception { when(mockContext.getAmiId()).thenReturn(Optional.of("ami-123456")); when(mockContext.getAmi()).thenReturn(Optional.of(new Image().withName("An AMI").withOwnerId("me"))); when(mockContext.getInstanceJson()).thenReturn("{\"keyName\": \"the-key\"}"); keyPairPlugin.process(mockContext); verify(mockContext).getInstanceJson(); verify(mockContext).violation(); verify(mockContext, times(2)).getAmi(); verify(mockContext).getAmiId(); verify(mockViolationSink).put(any(Violation.class)); }
@Test public void testProcessNullKeypair() throws Exception { when(mockContext.getInstanceJson()).thenReturn("{\"keyName\": null}"); keyPairPlugin.process(mockContext); verify(mockContext).getInstanceJson(); verify(mockViolationSink, never()).put(any(Violation.class)); }
@Test public void testProcessBlankKeypair() throws Exception { when(mockContext.getInstanceJson()).thenReturn("{\"keyName\": \" \"}"); keyPairPlugin.process(mockContext); verify(mockContext).getInstanceJson(); verify(mockViolationSink, never()).put(any(Violation.class)); }
@Test public void testProcessMissingKeypair() throws Exception { when(mockContext.getInstanceJson()).thenReturn("{}"); keyPairPlugin.process(mockContext); verify(mockContext).getInstanceJson(); verify(mockViolationSink, never()).put(any(Violation.class)); }