CloudFormation { public void signalReady(String stackName, String resourceName) { Preconditions.checkArgument(stackName != null && !stackName.isEmpty()); Preconditions.checkArgument(resourceName != null && !resourceName.isEmpty()); SignalResourceRequest req = new SignalResourceRequest(); req.setLogicalResourceId(resourceName); req.setStackName(stackName); req.setStatus(ResourceSignalStatus.SUCCESS); req.setUniqueId(this.ec2Context.getInstanceId()); this.cloudFormationClient.signalResource(req); }  void signalReady(String stackName, String resourceName); void signalReady(String resourceName); void signalReady(); static final String TAG_CLOUDFORMATION_LOGICAL_ID; static final String TAG_CLOUDFORMATION_STACK_NAME; }
@Test public void signalInstanceReady() throws Exception { try { this.cloudFormation.signalReady("stack", null); Assert.fail("Null check failed for resource"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } try { this.cloudFormation.signalReady("stack", ""); Assert.fail("Empty check failed for resource"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } try { this.cloudFormation.signalReady(null, "resource"); Assert.fail("Null check failed for stack"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } try { this.cloudFormation.signalReady("", "resource"); Assert.fail("Empty check failed for stack"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { SignalResourceRequest req = (SignalResourceRequest) invocationOnMock.getArguments()[0]; Assert.assertEquals("stack", req.getStackName()); Assert.assertEquals("resource", req.getLogicalResourceId()); Assert.assertEquals(ResourceSignalStatus.SUCCESS.toString(), req.getStatus()); Assert.assertEquals("localId", req.getUniqueId()); return null; } }).when(this.cfnMock).signalResource(Mockito.any(SignalResourceRequest.class)); this.cloudFormation.signalReady("stack", "resource"); }
@Test public void signalInstanceReadyLocalStack() throws Exception { HashMap<String, String> tags = new HashMap<>(); tags.put(CloudFormation.TAG_CLOUDFORMATION_LOGICAL_ID, "resourceTag"); tags.put(CloudFormation.TAG_CLOUDFORMATION_STACK_NAME, "stackTag"); Mockito.when(this.ec2Mock.getInstanceTags()).thenReturn(tags); try { this.cloudFormation.signalReady(null); Assert.fail("Null check failed for resource"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } try { this.cloudFormation.signalReady(""); Assert.fail("Empty check failed for resource"); } catch (Exception e) { Assert.assertEquals(IllegalArgumentException.class, e.getClass()); } Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { SignalResourceRequest req = (SignalResourceRequest) invocationOnMock.getArguments()[0]; Assert.assertEquals("stackTag", req.getStackName()); Assert.assertEquals("resource", req.getLogicalResourceId()); Assert.assertEquals(ResourceSignalStatus.SUCCESS.toString(), req.getStatus()); Assert.assertEquals("localId", req.getUniqueId()); return null; } }).when(this.cfnMock).signalResource(Mockito.any(SignalResourceRequest.class)); this.cloudFormation.signalReady("resource"); }
@Test public void signalInstanceReadyLocalStackAndResource() throws Exception { HashMap<String, String> tags = new HashMap<>(); tags.put(CloudFormation.TAG_CLOUDFORMATION_LOGICAL_ID, "resourceTag"); tags.put(CloudFormation.TAG_CLOUDFORMATION_STACK_NAME, "stackTag"); Mockito.when(this.ec2Mock.getInstanceTags()).thenReturn(tags); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { SignalResourceRequest req = (SignalResourceRequest) invocationOnMock.getArguments()[0]; Assert.assertEquals("stackTag", req.getStackName()); Assert.assertEquals("resourceTag", req.getLogicalResourceId()); Assert.assertEquals(ResourceSignalStatus.SUCCESS.toString(), req.getStatus()); Assert.assertEquals("localId", req.getUniqueId()); return null; } }).when(this.cfnMock).signalResource(Mockito.any(SignalResourceRequest.class)); this.cloudFormation.signalReady(); }