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);  }
@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); }
@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()); }
@Test public void saveResource() { Mockito.when(apiRepository.findOne(1L)).thenReturn(api); Mockito.when(resourceRepository.save(Mockito.any(Resource.class))).thenReturn(resource); Resource saved = resourceService.save(1L, resource); assertEquals(saved.getId(), resource.getId()); }
@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()); }