### Question:
TransactionConfigMatchingServiceValidator { public void validate(TransactionConfig transactionConfig) { if (transactionConfig.isUsingMatching()) { if (transactionConfig.getMatchingServiceEntityId() == null) { throw ConfigValidationException.createMissingMatchingEntityIdException(transactionConfig.getEntityId()); } matchingServiceConfigRepository.getData(transactionConfig.getMatchingServiceEntityId()) .orElseThrow(() -> ConfigValidationException.createAbsentMatchingServiceConfigException(transactionConfig.getMatchingServiceEntityId(), transactionConfig.getEntityId())); } } TransactionConfigMatchingServiceValidator(LocalConfigRepository<MatchingServiceConfig> matchingServiceConfigRepository); void validate(TransactionConfig transactionConfig);  }### Answer:
@Test public void matchingServiceEntityId_shouldHaveCorrespondingConfigurationWhenUsingMatching() { final String matchingServiceEntityId = "matching-service-entity-id"; TransactionConfig transactionConfig = aTransactionConfigData() .withMatchingServiceEntityId(matchingServiceEntityId) .build(); MatchingServiceConfig matchingServiceConfigData = aMatchingServiceConfig() .withEntityId(matchingServiceEntityId) .build(); when(matchingServiceConfigRepository.getData(matchingServiceEntityId)).thenReturn(Optional.ofNullable(matchingServiceConfigData)); validator.validate(transactionConfig); }
@Test public void matchingServiceEntityId_doesNotNeedCorrespondingConfigurationWhenNotUsingMatching() { final String matchingServiceEntityId = "matching-service-entity-id"; TransactionConfig transactionConfig = aTransactionConfigData() .withMatchingServiceEntityId(matchingServiceEntityId) .withUsingMatching(false) .build(); validator.validate(transactionConfig); }
@Test public void validator_shouldThrowExceptionWhenMatchingEntityIdIsAbsentIfUsingMatching() throws Exception { TransactionConfig transactionConfig = aTransactionConfigData() .withMatchingServiceEntityId(null) .build(); try { validator.validate(transactionConfig); fail("fail"); } catch(ConfigValidationException configValidationException) { final ConfigValidationException expectedException = createMissingMatchingEntityIdException(transactionConfig.getEntityId()); assertThat(configValidationException.getMessage()).isEqualTo(expectedException.getMessage()); } }
@Test public void validator_shouldThrowExceptionWhenCorrespondingMatchingServiceConfigurationIsAbsent() throws Exception { final String matchingServiceEntityId = "matching-service-entity-id"; TransactionConfig transactionConfig = aTransactionConfigData() .withMatchingServiceEntityId(matchingServiceEntityId) .build(); when(matchingServiceConfigRepository.getData(matchingServiceEntityId)).thenReturn(Optional.empty()); try { validator.validate(transactionConfig); fail("fail"); } catch(ConfigValidationException configValidationException) { final ConfigValidationException expectedException = createAbsentMatchingServiceConfigException(matchingServiceEntityId, transactionConfig.getEntityId()); assertThat(configValidationException.getMessage()).isEqualTo(expectedException.getMessage()); } }