@Test void testChangePrimaryGatewayMetadataWhenFormerPgwIsEmptyThenItSavesMetadata() { Stack stack = mock(Stack.class); InstanceMetaData im1 = mock(InstanceMetaData.class); InstanceMetaData im2 = mock(InstanceMetaData.class); when(stack.getId()).thenReturn(STACK_ID); when(instanceMetaDataRepository.findNotTerminatedForStack(anyLong())).thenReturn(Set.of(im1, im2)); when(im1.getInstanceId()).thenReturn(INSTANCE_ID_1); lenient().when(im2.getInstanceId()).thenReturn(INSTANCE_ID_2); underTest.changePrimaryGatewayMetadata(stack, Optional.empty(), INSTANCE_ID_1); verify(im1).setInstanceMetadataType(eq(InstanceMetadataType.GATEWAY_PRIMARY)); verify(instanceMetaDataRepository).save(eq(im1)); }
public void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId) { if (formerPrimaryGatwayInstanceId.isEmpty() || !newPrimaryGatewayInstanceId.equals(formerPrimaryGatwayInstanceId.get())) { LOGGER.debug("Changing the primary gateway metadata from {} to {}", formerPrimaryGatwayInstanceId, newPrimaryGatewayInstanceId); Set<InstanceMetaData> instanceMetaDataSet = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId()); InstanceMetaData newPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, newPrimaryGatewayInstanceId); formerPrimaryGatwayInstanceId.ifPresent(formerId -> { InstanceMetaData formerPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, formerId); formerPrimaryGateway.setInstanceMetadataType(GATEWAY); instanceMetaDataRepository.save(formerPrimaryGateway); LOGGER.debug("The primary gateway {} metadata has been changed to a regular gateway.", formerId); }); newPrimaryGateway.setInstanceMetadataType(GATEWAY_PRIMARY); instanceMetaDataRepository.save(newPrimaryGateway); LOGGER.debug("The regular gateway {} metadata has been changed to a primary gateway.", newPrimaryGatewayInstanceId); } else { LOGGER.debug("The primary gateway {} metadata did not change", newPrimaryGatewayInstanceId); } }
ChangePrimaryGatewayService { public void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId) { if (formerPrimaryGatwayInstanceId.isEmpty() || !newPrimaryGatewayInstanceId.equals(formerPrimaryGatwayInstanceId.get())) { LOGGER.debug("Changing the primary gateway metadata from {} to {}", formerPrimaryGatwayInstanceId, newPrimaryGatewayInstanceId); Set<InstanceMetaData> instanceMetaDataSet = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId()); InstanceMetaData newPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, newPrimaryGatewayInstanceId); formerPrimaryGatwayInstanceId.ifPresent(formerId -> { InstanceMetaData formerPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, formerId); formerPrimaryGateway.setInstanceMetadataType(GATEWAY); instanceMetaDataRepository.save(formerPrimaryGateway); LOGGER.debug("The primary gateway {} metadata has been changed to a regular gateway.", formerId); }); newPrimaryGateway.setInstanceMetadataType(GATEWAY_PRIMARY); instanceMetaDataRepository.save(newPrimaryGateway); LOGGER.debug("The regular gateway {} metadata has been changed to a primary gateway.", newPrimaryGatewayInstanceId); } else { LOGGER.debug("The primary gateway {} metadata did not change", newPrimaryGatewayInstanceId); } } }
ChangePrimaryGatewayService { public void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId) { if (formerPrimaryGatwayInstanceId.isEmpty() || !newPrimaryGatewayInstanceId.equals(formerPrimaryGatwayInstanceId.get())) { LOGGER.debug("Changing the primary gateway metadata from {} to {}", formerPrimaryGatwayInstanceId, newPrimaryGatewayInstanceId); Set<InstanceMetaData> instanceMetaDataSet = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId()); InstanceMetaData newPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, newPrimaryGatewayInstanceId); formerPrimaryGatwayInstanceId.ifPresent(formerId -> { InstanceMetaData formerPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, formerId); formerPrimaryGateway.setInstanceMetadataType(GATEWAY); instanceMetaDataRepository.save(formerPrimaryGateway); LOGGER.debug("The primary gateway {} metadata has been changed to a regular gateway.", formerId); }); newPrimaryGateway.setInstanceMetadataType(GATEWAY_PRIMARY); instanceMetaDataRepository.save(newPrimaryGateway); LOGGER.debug("The regular gateway {} metadata has been changed to a primary gateway.", newPrimaryGatewayInstanceId); } else { LOGGER.debug("The primary gateway {} metadata did not change", newPrimaryGatewayInstanceId); } }  }
ChangePrimaryGatewayService { public void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId) { if (formerPrimaryGatwayInstanceId.isEmpty() || !newPrimaryGatewayInstanceId.equals(formerPrimaryGatwayInstanceId.get())) { LOGGER.debug("Changing the primary gateway metadata from {} to {}", formerPrimaryGatwayInstanceId, newPrimaryGatewayInstanceId); Set<InstanceMetaData> instanceMetaDataSet = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId()); InstanceMetaData newPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, newPrimaryGatewayInstanceId); formerPrimaryGatwayInstanceId.ifPresent(formerId -> { InstanceMetaData formerPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, formerId); formerPrimaryGateway.setInstanceMetadataType(GATEWAY); instanceMetaDataRepository.save(formerPrimaryGateway); LOGGER.debug("The primary gateway {} metadata has been changed to a regular gateway.", formerId); }); newPrimaryGateway.setInstanceMetadataType(GATEWAY_PRIMARY); instanceMetaDataRepository.save(newPrimaryGateway); LOGGER.debug("The regular gateway {} metadata has been changed to a primary gateway.", newPrimaryGatewayInstanceId); } else { LOGGER.debug("The primary gateway {} metadata did not change", newPrimaryGatewayInstanceId); } }  Optional<String> getPrimaryGatewayInstanceId(Stack stack); String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds); void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId); }
ChangePrimaryGatewayService { public void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId) { if (formerPrimaryGatwayInstanceId.isEmpty() || !newPrimaryGatewayInstanceId.equals(formerPrimaryGatwayInstanceId.get())) { LOGGER.debug("Changing the primary gateway metadata from {} to {}", formerPrimaryGatwayInstanceId, newPrimaryGatewayInstanceId); Set<InstanceMetaData> instanceMetaDataSet = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId()); InstanceMetaData newPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, newPrimaryGatewayInstanceId); formerPrimaryGatwayInstanceId.ifPresent(formerId -> { InstanceMetaData formerPrimaryGateway = findInstanceMetaDataByInstanceId(instanceMetaDataSet, formerId); formerPrimaryGateway.setInstanceMetadataType(GATEWAY); instanceMetaDataRepository.save(formerPrimaryGateway); LOGGER.debug("The primary gateway {} metadata has been changed to a regular gateway.", formerId); }); newPrimaryGateway.setInstanceMetadataType(GATEWAY_PRIMARY); instanceMetaDataRepository.save(newPrimaryGateway); LOGGER.debug("The regular gateway {} metadata has been changed to a primary gateway.", newPrimaryGatewayInstanceId); } else { LOGGER.debug("The primary gateway {} metadata did not change", newPrimaryGatewayInstanceId); } }  Optional<String> getPrimaryGatewayInstanceId(Stack stack); String selectNewPrimaryGatewayInstanceId(Stack stack, List<String> repairInstanceIds); void changePrimaryGatewayMetadata(Stack stack, Optional<String> formerPrimaryGatwayInstanceId, String newPrimaryGatewayInstanceId);  }