OperationService { @Transactional public Operation save(Long apiId, Long resourceId, Operation operation) { Resource resource = resourceRepository.findByApiIdAndId(apiId, resourceId); HeimdallException.checkThrow(resource == null, GLOBAL_RESOURCE_NOT_FOUND); Operation resData = operationRepository.findByResourceApiIdAndMethodAndPath(apiId, operation.getMethod(), operation.getPath()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getResource().getId(), resource.getId()), ONLY_ONE_OPERATION_PER_RESOURCE); boolean patternExists = operationJDBCRepository.patternExists(resource.getApi().getBasePath() + "/" + operation.getPath(), apiId); HeimdallException.checkThrow(patternExists, OPERATION_ROUTE_ALREADY_EXISTS); operation.setResource(resource); operation.setPath(StringUtils.removeMultipleSlashes(operation.getPath())); HeimdallException.checkThrow(validateSingleWildCardOperationPath(operation), OPERATION_CANT_HAVE_SINGLE_WILDCARD); HeimdallException.checkThrow(validateDoubleWildCardOperationPath(operation), OPERATION_CANT_HAVE_DOUBLE_WILDCARD_NOT_AT_THE_END); operation = operationRepository.save(operation); amqpRoute.dispatchRoutes(); return operation; }  @Transactional(readOnly = true) Operation find(Long apiId, Long resourceId, Long operationId); @Transactional(readOnly = true) OperationPage list(Long apiId, Long resourceId, OperationDTO operationDTO, PageableDTO pageableDTO); @Transactional(readOnly = true) List<Operation> list(Long apiId, Long resourceId, OperationDTO operationDTO); @Transactional(readOnly = true) List<Operation> list(final Long apiId); @Transactional Operation save(Long apiId, Long resourceId, Operation operation); @Transactional Operation update(Long apiId, Long resourceId, Long operationId, OperationDTO operationDTO); @Transactional void delete(Long apiId, Long resourceId, Long operationId); @Transactional void deleteAllfromResource(Long apiId, Long resourceId);  }
@Test public void saveOperationDTO() { Mockito.when(operationRepository.save(Mockito.any(Operation.class))).thenReturn(operation); Mockito.when(resourceRepository.findByApiIdAndId(Mockito.anyLong(), Mockito.anyLong())) .thenReturn(res); Mockito.when(operationRepository.findByResourceApiIdAndMethodAndPath(Mockito.anyLong(), Mockito.any(HttpMethod.class), Mockito.anyString() )).thenReturn(null); Mockito.when(operationJDBCRepository.patternExists(Mockito.anyString(), Mockito.anyLong())).thenReturn(false); Operation saved = operationService.save(api.getId(), res.getId(), operation); assertEquals(saved.getId(), operation.getId()); }
@Test public void saveOperation() { Mockito.when(operationRepository.save(Mockito.any(Operation.class))).thenReturn(operation); Mockito.when(resourceRepository.findByApiIdAndId(Mockito.anyLong(), Mockito.anyLong())) .thenReturn(res); Mockito.when(operationRepository.findByResourceApiIdAndMethodAndPath(Mockito.anyLong(), Mockito.any(HttpMethod.class), Mockito.anyString() )).thenReturn(null); Mockito.when(operationJDBCRepository.patternExists(Mockito.anyString(), Mockito.anyLong())).thenReturn(false); Operation saved = operationService.save(api.getId(), res.getId(), operation); assertEquals(saved.getId(), operation.getId()); }
@Test public void saveWithResourceNull() { thrown.expect(BadRequestException.class); thrown.expectMessage("Only one operation per resource"); Mockito.when(operationRepository.save(Mockito.any(Operation.class))).thenReturn(operation); Mockito.when(resourceRepository.findByApiIdAndId(Mockito.anyLong(), Mockito.anyLong())) .thenReturn(res); Mockito.when(operationRepository.findByResourceApiIdAndMethodAndPath(Mockito.anyLong(), Mockito.any(HttpMethod.class), Mockito.anyString() )).thenReturn(operation); Operation saved = operationService.save(api.getId(), res.getId(), operation); assertEquals(saved.getId(), operation.getId()); }