@Test public void acceptNewResourceWithPathExistentInDifferentApi() { ResourceDTO res = new ResourceDTO(); res.setName("name"); res.setDescription("description"); Api api = new Api(); api.setId(10L); Api diffApi = new Api(); diffApi.setId(20L); Resource resData = new Resource(); resData.setApi(diffApi); Resource newRes = new Resource(); newRes.setId(40L); newRes.setName("name"); newRes.setApi(diffApi); newRes.setDescription("foo description"); Mockito.when(apiRepository.findOne(10L)).thenReturn(api); Mockito.when(resourceRepository.findByApiIdAndName(Mockito.anyLong(), Mockito.anyString())) .thenReturn(resData); Mockito.when(resourceRepository.save(Mockito.any(Resource.class))).thenReturn(newRes); Resource resource = resourceService.save(10L, res); assertEquals(res.getName(), resource.getName()); assertNotEquals(api.getId(), resource.getApi().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);  }