MiddlewareService { @Transactional public Middleware update(Long apiId, Long middlewareId, MiddlewareDTO middlewareDTO) { Middleware middleware = middlewareRepository.findByApiIdAndId(apiId, middlewareId); HeimdallException.checkThrow(middleware == null, GLOBAL_RESOURCE_NOT_FOUND); Middleware resData = middlewareRepository.findByApiIdAndVersion(apiId, middlewareDTO.getVersion()); HeimdallException.checkThrow(resData != null && Objects.equals(resData.getApi().getId(), middleware.getApi().getId()) && !Objects.equals(resData.getId(), middleware.getId()), ONLY_ONE_MIDDLEWARE_PER_VERSION_AND_API); middleware = GenericConverter.mapper(middlewareDTO, middleware); Boolean deleteDeprecated = property.getMiddlewares().getDeleteDeprecated(); if (middleware.getStatus().equals(Status.DEPRECATED)) if (deleteDeprecated != null && deleteDeprecated) middleware.setFile(null); middleware = middlewareRepository.save(middleware); amqpMiddlewareService.dispatchCreateMiddlewares(middlewareId); return middleware; }  @Transactional(readOnly = true) Middleware find(Long apiId, Long middlewareId); @Transactional(readOnly = true) MiddlewarePage list(Long apiId, MiddlewareDTO middlewareDTO, PageableDTO pageableDTO); @Transactional(readOnly = true) List<Middleware> list(Long apiId, MiddlewareDTO middlewareDTO); @Transactional Middleware save(Long apiId, MiddlewareDTO middlewareDTO, MultipartFile file); @Transactional Middleware update(Long apiId, Long middlewareId, MiddlewareDTO middlewareDTO); @Transactional void delete(Long apiId, Long middlewareId); @Transactional void deleteAll(Long apiId);  }
@Test public void updateTest() { Mockito.when(middlewareRepository.findByApiIdAndId(Mockito.anyLong(), Mockito.anyLong())).thenReturn(middleware); Mockito.when(middlewareRepository.findByApiIdAndVersion(Mockito.anyLong(), Mockito.anyString())).thenReturn(null); middleware.setVersion("0.0.2"); Mockito.when(middlewareRepository.save(Mockito.any(Middleware.class))).thenReturn(middleware); Middleware updated = service.update(1L, 1L, middlewareDTO); assertEquals(middleware, updated); }