EnvironmentService { @Transactional public Environment save(EnvironmentDTO environmentDTO) { List<Environment> environments = environmentRepository.findByInboundURL(environmentDTO.getInboundURL()); Environment environmentEqual = environments.stream().filter(e -> e.getOutboundURL().equals(environmentDTO.getOutboundURL())).findFirst().orElse(null); HeimdallException.checkThrow(environmentEqual != null, ExceptionMessage.ENVIRONMENT_ALREADY_EXISTS); Environment environment = GenericConverter.mapper(environmentDTO, Environment.class); HeimdallException.checkThrow(!validateInboundURL(environment.getInboundURL()), ENVIRONMENT_INBOUND_DNS_PATTERN); environment = environmentRepository.save(environment); return environment; }  Environment find(Long id); EnvironmentPage list(EnvironmentDTO environmentDTO, PageableDTO pageableDTO); List<Environment> list(EnvironmentDTO environmentDTO); @Transactional Environment save(EnvironmentDTO environmentDTO); @Transactional Environment update(Long id, EnvironmentDTO environmentDTO); @Transactional void delete(Long id);  }
@Test public void saveEnvironmentSuccess() { Mockito.when(environmentRepository.save(Mockito.any(Environment.class))).thenReturn(environment); Environment saved = environmentService.save(environmentDTO); assertEquals(saved.getId(), environment.getId()); }
@Test public void saveEnviromentMalFormedInboundURL() { thrown.expect(BadRequestException.class); thrown.expectMessage( "Environment inbound URL has to follow the pattern http[s]: Mockito.when(environmentRepository.save(Mockito.any(Environment.class))).thenReturn(environment); environmentDTO.setInboundURL("http: Environment saved = environmentService.save(environmentDTO); }