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);  }
@Test public void saveNewMiddlewareTest() { middlewareProperty.setAllowInactive(1); middlewareProperty.setDeleteDeprecated(true); Middleware saved = service.save(api.getId(), middlewareDTO, multipartFile); Map<Status, List<Middleware>> middlewareMap = middlewareList.stream() .collect(Collectors.groupingBy(Middleware::getStatus)); assertEquals(saved, middleware); assertEquals(Status.ACTIVE, saved.getStatus()); assertEquals(1, middlewareMap.get(Status.INACTIVE).size()); assertNull(middlewareMap.get(Status.DEPRECATED)); }
@Test public void propertyNotSetTest() { middlewareProperty.setAllowInactive(null); middlewareProperty.setDeleteDeprecated(null); Middleware saved = service.save(api.getId(), middlewareDTO, multipartFile); Map<Status, List<Middleware>> middlewareMap = middlewareList.stream() .collect(Collectors.groupingBy(Middleware::getStatus)); assertEquals(saved, middleware); assertEquals(Status.ACTIVE, saved.getStatus()); assertEquals(3, middlewareMap.get(Status.INACTIVE).size()); assertEquals(2, middlewareMap.get(Status.DEPRECATED).size()); }
@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()); }
@Test public void doNotDeleteDeprecated() { middlewareProperty.setAllowInactive(1); middlewareProperty.setDeleteDeprecated(false); Middleware saved = service.save(api.getId(), middlewareDTO, multipartFile); Map<Status, List<Middleware>> middlewareMap = middlewareList.stream() .collect(Collectors.groupingBy(Middleware::getStatus)); assertEquals(saved, middleware); assertEquals(Status.ACTIVE, saved.getStatus()); assertEquals(1, middlewareMap.get(Status.INACTIVE).size()); assertEquals(4, middlewareMap.get(Status.DEPRECATED).size()); assertNotNull(m2.getFile()); assertNotNull(m3.getFile()); assertNotNull(m4.getFile()); assertNotNull(m5.getFile()); }
@Test public void deleteDeprecated() { middlewareProperty.setAllowInactive(1); middlewareProperty.setDeleteDeprecated(true); Middleware saved = service.save(api.getId(), middlewareDTO, multipartFile); Map<Status, List<Middleware>> middlewareMap = middlewareList.stream() .collect(Collectors.groupingBy(Middleware::getStatus)); assertEquals(saved, middleware); assertEquals(Status.ACTIVE, saved.getStatus()); assertEquals(1, middlewareMap.get(Status.INACTIVE).size()); assertNull(middlewareMap.get(Status.DEPRECATED)); }
@Test public void noMiddlewareTest() { Mockito.when(middlewareRepository.findByApiId(api.getId())).thenReturn(null); middlewareProperty.setAllowInactive(1); middlewareProperty.setDeleteDeprecated(true); Middleware saved = service.save(api.getId(), middlewareDTO, multipartFile); assertTrue(saved.equals(middleware)); }