@Test public void rejectNewResourceWithPathExistentInTheSameApi() { thrown.expect(BadRequestException.class); thrown.expectMessage("Only one resource per api"); ResourceDTO res = new ResourceDTO(); res.setName("name"); Api api = new Api(); api.setId(10L); Resource resData = new Resource(); resData.setApi(api); Mockito.when(apiRepository.findOne(10L)).thenReturn(api); Mockito.when(resourceRepository.findByApiIdAndName(api.getId(), res.getName())).thenReturn(resData); resourceService.save(10L, res); }
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);  }