@Test public void saveNewMiddlewareHugeRollbackTest() { middlewareProperty.setAllowInactive(99999999); Middleware saved = service.save(api.getId(), middlewareDTO, multipartFile); Map<Status, List<Middleware>> middlewareMap = middlewareList.stream() .collect(Collectors.groupingBy(m -> m.getStatus())); assertTrue(saved.equals(middleware)); assertEquals(Status.ACTIVE, saved.getStatus()); assertEquals(3, middlewareMap.get(Status.INACTIVE).size()); assertEquals(2, middlewareMap.get(Status.DEPRECATED).size()); }
@Transactional public Middleware save(Long apiId, MiddlewareDTO middlewareDTO, MultipartFile file) { List<Middleware> middlewares = updateMiddlewaresStatus(middlewareRepository.findByApiId(apiId)); if (middlewares != null && !middlewares.isEmpty()) middlewareRepository.save(middlewares); Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Middleware resData = middlewareRepository.findByApiIdAndVersion(apiId, middlewareDTO.getVersion()); HeimdallException.checkThrow(resData != null && (Objects.equals(resData.getApi().getId(), api.getId())), ONLY_ONE_MIDDLEWARE_PER_VERSION_AND_API); String type = FilenameUtils.getExtension(file.getOriginalFilename()); HeimdallException.checkThrow(!("jar".equalsIgnoreCase(type)), MIDDLEWARE_UNSUPPORTED_TYPE); Middleware middleware = GenericConverter.mapper(middlewareDTO, Middleware.class); middleware.setApi(api); middleware.setPath(root + "/api/" + apiId + "/middleware"); middleware.setType(type); try { middleware.setFile(file.getBytes()); } catch (Exception e) { log.error(e.getMessage(), e); } List<Interceptor> interceptors = interceptorRepository.findByTypeAndOperationResourceApiId(TypeInterceptor.MIDDLEWARE, middleware.getApi().getId()); middleware.setInterceptors(interceptors); middleware = middlewareRepository.save(middleware); amqpMiddlewareService.dispatchCreateMiddlewares(middleware.getId()); return middleware; }
MiddlewareService { @Transactional public Middleware save(Long apiId, MiddlewareDTO middlewareDTO, MultipartFile file) { List<Middleware> middlewares = updateMiddlewaresStatus(middlewareRepository.findByApiId(apiId)); if (middlewares != null && !middlewares.isEmpty()) middlewareRepository.save(middlewares); Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Middleware resData = middlewareRepository.findByApiIdAndVersion(apiId, middlewareDTO.getVersion()); HeimdallException.checkThrow(resData != null && (Objects.equals(resData.getApi().getId(), api.getId())), ONLY_ONE_MIDDLEWARE_PER_VERSION_AND_API); String type = FilenameUtils.getExtension(file.getOriginalFilename()); HeimdallException.checkThrow(!("jar".equalsIgnoreCase(type)), MIDDLEWARE_UNSUPPORTED_TYPE); Middleware middleware = GenericConverter.mapper(middlewareDTO, Middleware.class); middleware.setApi(api); middleware.setPath(root + "/api/" + apiId + "/middleware"); middleware.setType(type); try { middleware.setFile(file.getBytes()); } catch (Exception e) { log.error(e.getMessage(), e); } List<Interceptor> interceptors = interceptorRepository.findByTypeAndOperationResourceApiId(TypeInterceptor.MIDDLEWARE, middleware.getApi().getId()); middleware.setInterceptors(interceptors); middleware = middlewareRepository.save(middleware); amqpMiddlewareService.dispatchCreateMiddlewares(middleware.getId()); return middleware; } }
MiddlewareService { @Transactional public Middleware save(Long apiId, MiddlewareDTO middlewareDTO, MultipartFile file) { List<Middleware> middlewares = updateMiddlewaresStatus(middlewareRepository.findByApiId(apiId)); if (middlewares != null && !middlewares.isEmpty()) middlewareRepository.save(middlewares); Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Middleware resData = middlewareRepository.findByApiIdAndVersion(apiId, middlewareDTO.getVersion()); HeimdallException.checkThrow(resData != null && (Objects.equals(resData.getApi().getId(), api.getId())), ONLY_ONE_MIDDLEWARE_PER_VERSION_AND_API); String type = FilenameUtils.getExtension(file.getOriginalFilename()); HeimdallException.checkThrow(!("jar".equalsIgnoreCase(type)), MIDDLEWARE_UNSUPPORTED_TYPE); Middleware middleware = GenericConverter.mapper(middlewareDTO, Middleware.class); middleware.setApi(api); middleware.setPath(root + "/api/" + apiId + "/middleware"); middleware.setType(type); try { middleware.setFile(file.getBytes()); } catch (Exception e) { log.error(e.getMessage(), e); } List<Interceptor> interceptors = interceptorRepository.findByTypeAndOperationResourceApiId(TypeInterceptor.MIDDLEWARE, middleware.getApi().getId()); middleware.setInterceptors(interceptors); middleware = middlewareRepository.save(middleware); amqpMiddlewareService.dispatchCreateMiddlewares(middleware.getId()); return middleware; }  }
MiddlewareService { @Transactional public Middleware save(Long apiId, MiddlewareDTO middlewareDTO, MultipartFile file) { List<Middleware> middlewares = updateMiddlewaresStatus(middlewareRepository.findByApiId(apiId)); if (middlewares != null && !middlewares.isEmpty()) middlewareRepository.save(middlewares); Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Middleware resData = middlewareRepository.findByApiIdAndVersion(apiId, middlewareDTO.getVersion()); HeimdallException.checkThrow(resData != null && (Objects.equals(resData.getApi().getId(), api.getId())), ONLY_ONE_MIDDLEWARE_PER_VERSION_AND_API); String type = FilenameUtils.getExtension(file.getOriginalFilename()); HeimdallException.checkThrow(!("jar".equalsIgnoreCase(type)), MIDDLEWARE_UNSUPPORTED_TYPE); Middleware middleware = GenericConverter.mapper(middlewareDTO, Middleware.class); middleware.setApi(api); middleware.setPath(root + "/api/" + apiId + "/middleware"); middleware.setType(type); try { middleware.setFile(file.getBytes()); } catch (Exception e) { log.error(e.getMessage(), e); } List<Interceptor> interceptors = interceptorRepository.findByTypeAndOperationResourceApiId(TypeInterceptor.MIDDLEWARE, middleware.getApi().getId()); middleware.setInterceptors(interceptors); middleware = middlewareRepository.save(middleware); amqpMiddlewareService.dispatchCreateMiddlewares(middleware.getId()); 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); }
MiddlewareService { @Transactional public Middleware save(Long apiId, MiddlewareDTO middlewareDTO, MultipartFile file) { List<Middleware> middlewares = updateMiddlewaresStatus(middlewareRepository.findByApiId(apiId)); if (middlewares != null && !middlewares.isEmpty()) middlewareRepository.save(middlewares); Api api = apiRepository.findOne(apiId); HeimdallException.checkThrow(api == null, GLOBAL_RESOURCE_NOT_FOUND); Middleware resData = middlewareRepository.findByApiIdAndVersion(apiId, middlewareDTO.getVersion()); HeimdallException.checkThrow(resData != null && (Objects.equals(resData.getApi().getId(), api.getId())), ONLY_ONE_MIDDLEWARE_PER_VERSION_AND_API); String type = FilenameUtils.getExtension(file.getOriginalFilename()); HeimdallException.checkThrow(!("jar".equalsIgnoreCase(type)), MIDDLEWARE_UNSUPPORTED_TYPE); Middleware middleware = GenericConverter.mapper(middlewareDTO, Middleware.class); middleware.setApi(api); middleware.setPath(root + "/api/" + apiId + "/middleware"); middleware.setType(type); try { middleware.setFile(file.getBytes()); } catch (Exception e) { log.error(e.getMessage(), e); } List<Interceptor> interceptors = interceptorRepository.findByTypeAndOperationResourceApiId(TypeInterceptor.MIDDLEWARE, middleware.getApi().getId()); middleware.setInterceptors(interceptors); middleware = middlewareRepository.save(middleware); amqpMiddlewareService.dispatchCreateMiddlewares(middleware.getId()); 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);  }