@Test public void saveResourceOnlyOnePerAPI() { thrown.expect(BadRequestException.class); thrown.expectMessage("Only one resource per api"); Api resApi = new Api(); resApi.setId(10L); Resource resData = new Resource(); resData.setApi(resApi); Mockito.when(apiRepository.findOne(1L)).thenReturn(api); Mockito.when(resourceRepository.findByApiIdAndName(1L, "Local Resource")).thenReturn(resData); Mockito.when(resourceRepository.save(Mockito.any(Resource.class))).thenReturn(resource); Resource saved = resourceService.save(1L, resource); assertEquals(saved.getId(), resource.getId()); }
public Resource save(Long apiId, ResourceDTO resourceDTO) { Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Resource resData = resourceRepository.findByApiIdAndName(apiId, resourceDTO.getName()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getApi().getId(), api.getId()), ONLY_ONE_RESOURCE_PER_API); Resource resource = GenericConverter.mapper(resourceDTO, Resource.class); resource.setApi(api); resource = resourceRepository.save(resource); amqpRoute.dispatchRoutes(); return resource; }
ResourceService { public Resource save(Long apiId, ResourceDTO resourceDTO) { Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Resource resData = resourceRepository.findByApiIdAndName(apiId, resourceDTO.getName()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getApi().getId(), api.getId()), ONLY_ONE_RESOURCE_PER_API); Resource resource = GenericConverter.mapper(resourceDTO, Resource.class); resource.setApi(api); resource = resourceRepository.save(resource); amqpRoute.dispatchRoutes(); return resource; } }
ResourceService { public Resource save(Long apiId, ResourceDTO resourceDTO) { Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Resource resData = resourceRepository.findByApiIdAndName(apiId, resourceDTO.getName()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getApi().getId(), api.getId()), ONLY_ONE_RESOURCE_PER_API); Resource resource = GenericConverter.mapper(resourceDTO, Resource.class); resource.setApi(api); resource = resourceRepository.save(resource); amqpRoute.dispatchRoutes(); return resource; }  }
ResourceService { public Resource save(Long apiId, ResourceDTO resourceDTO) { Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Resource resData = resourceRepository.findByApiIdAndName(apiId, resourceDTO.getName()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getApi().getId(), api.getId()), ONLY_ONE_RESOURCE_PER_API); Resource resource = GenericConverter.mapper(resourceDTO, Resource.class); resource.setApi(api); resource = resourceRepository.save(resource); amqpRoute.dispatchRoutes(); return resource; }  @Transactional(readOnly = true) Resource find(Long apiId, Long resourceId); ResourcePage list(Long apiId, ResourceDTO resourceDTO, PageableDTO pageableDTO); List<Resource> list(Long apiId, ResourceDTO resourceDTO); Resource save(Long apiId, ResourceDTO resourceDTO); Resource save(Long apiId, Resource resource); Resource update(Long apiId, Long resourceId, ResourceDTO resourceDTO); void delete(Long apiId, Long resourceId); void deleteAllFromApi(Long apiId); }
ResourceService { public Resource save(Long apiId, ResourceDTO resourceDTO) { Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Resource resData = resourceRepository.findByApiIdAndName(apiId, resourceDTO.getName()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getApi().getId(), api.getId()), ONLY_ONE_RESOURCE_PER_API); Resource resource = GenericConverter.mapper(resourceDTO, Resource.class); resource.setApi(api); resource = resourceRepository.save(resource); amqpRoute.dispatchRoutes(); return resource; }  @Transactional(readOnly = true) Resource find(Long apiId, Long resourceId); ResourcePage list(Long apiId, ResourceDTO resourceDTO, PageableDTO pageableDTO); List<Resource> list(Long apiId, ResourceDTO resourceDTO); Resource save(Long apiId, ResourceDTO resourceDTO); Resource save(Long apiId, Resource resource); Resource update(Long apiId, Long resourceId, ResourceDTO resourceDTO); void delete(Long apiId, Long resourceId); void deleteAllFromApi(Long apiId);  }