### Question:
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);  }### Answer:
@Test void testChangePrimaryGatewayMetadataWhenFormerPgwIsNewPgwThenItDoesNotSaveMetadata() { Stack stack = mock(Stack.class); underTest.changePrimaryGatewayMetadata(stack, Optional.of(INSTANCE_ID_1), INSTANCE_ID_1); verify(instanceMetaDataRepository, never()).save(any()); }
@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)); }
@Test void testChangePrimaryGatewayMetadataWhenFormerPgwExiststThenItSavesMetadata() { Stack stack = mock(Stack.class); InstanceMetaData im1 = mock(InstanceMetaData.class); InstanceMetaData im2 = mock(InstanceMetaData.class); InstanceMetaData im3 = mock(InstanceMetaData.class); when(stack.getId()).thenReturn(STACK_ID); when(instanceMetaDataRepository.findNotTerminatedForStack(anyLong())).thenReturn(Set.of(im1, im2, im3)); when(im1.getInstanceId()).thenReturn(INSTANCE_ID_1); when(im2.getInstanceId()).thenReturn(INSTANCE_ID_2); lenient().when(im3.getInstanceId()).thenReturn(INSTANCE_ID_3); underTest.changePrimaryGatewayMetadata(stack, Optional.of(INSTANCE_ID_1), INSTANCE_ID_2); verify(im1).setInstanceMetadataType(eq(InstanceMetadataType.GATEWAY)); verify(im2).setInstanceMetadataType(eq(InstanceMetadataType.GATEWAY_PRIMARY)); verify(instanceMetaDataRepository).save(eq(im1)); verify(instanceMetaDataRepository).save(eq(im2)); }