### Question:
StackDownscaleValidatorService { public void checkInstanceIsTheClusterManagerServerOrNot(String instancePublicIp, InstanceMetadataType metadataType) { if (GATEWAY.equals(metadataType) || GATEWAY_PRIMARY.equals(metadataType)) { String additionalIpPartForTemplate = ""; String messageTemplate = "Downscale for the given node%s is prohibited because it " + "serves as a host the Cluster Manager server"; if (StringUtils.isNotEmpty(instancePublicIp)) { additionalIpPartForTemplate = " [public IP: " + instancePublicIp + "]"; } throw new BadRequestException(String.format(messageTemplate, additionalIpPartForTemplate)); } }  void checkInstanceIsTheClusterManagerServerOrNot(String instancePublicIp, InstanceMetadataType metadataType); void checkUserHasRightToTerminateInstance(String owner, String userId, Long stackId); void checkClusterInValidStatus(Cluster cluster);  }### Answer:
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenInstanceIsCoreTypeThenNoExceptionWouldInvoke() { underTest.checkInstanceIsTheClusterManagerServerOrNot(INSTANCE_PUBLIC_IP, CORE); }
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenTypeIsGatewayThenException() { expectedException.expect(BadRequestException.class); expectedException.expectMessage(CLUSTER_MANAGER_SERVER_HOST_EXCEPTION_MESSAGE); underTest.checkInstanceIsTheClusterManagerServerOrNot(INSTANCE_PUBLIC_IP, GATEWAY); }
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenTypeIsGatewayPrimaryThenException() { expectedException.expect(BadRequestException.class); expectedException.expectMessage(CLUSTER_MANAGER_SERVER_HOST_EXCEPTION_MESSAGE); underTest.checkInstanceIsTheClusterManagerServerOrNot(INSTANCE_PUBLIC_IP, GATEWAY_PRIMARY); }
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenTypeIsGatewayPrimaryAndNullPublicIpProvidedThenExceptionWithoutIp() { expectedException.expect(BadRequestException.class); expectedException.expectMessage(CLUSTER_MANAGER_HOST_EXCEPTION_MESSAGE_WITHOUT_IP); underTest.checkInstanceIsTheClusterManagerServerOrNot(null, GATEWAY_PRIMARY); }
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenTypeIsGatewayPrimaryAndEmptyPublicIpProvidedThenExceptionWithoutIp() { expectedException.expect(BadRequestException.class); expectedException.expectMessage(CLUSTER_MANAGER_HOST_EXCEPTION_MESSAGE_WITHOUT_IP); underTest.checkInstanceIsTheClusterManagerServerOrNot("", GATEWAY_PRIMARY); }
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenTypeIsGatewayAndNullPublicIpProvidedThenExceptionWithoutIp() { expectedException.expect(BadRequestException.class); expectedException.expectMessage(CLUSTER_MANAGER_HOST_EXCEPTION_MESSAGE_WITHOUT_IP); underTest.checkInstanceIsTheClusterManagerServerOrNot(null, GATEWAY); }
@Test public void testCheckInstanceIsTheAmbariServerOrNotMethodWhenTypeIsGatewayAndEmptyPublicIpProvidedThenExceptionWithoutIp() { expectedException.expect(BadRequestException.class); expectedException.expectMessage(CLUSTER_MANAGER_HOST_EXCEPTION_MESSAGE_WITHOUT_IP); underTest.checkInstanceIsTheClusterManagerServerOrNot("", GATEWAY); }