ReleaseService { @Transactional public Release rollback(long releaseId, String operator) { Release release = findOne(releaseId); if (release == null) { throw new NotFoundException("release not found"); } if (release.isAbandoned()) { throw new BadRequestException("release is not active"); } String appId = release.getAppId(); String clusterName = release.getClusterName(); String namespaceName = release.getNamespaceName(); PageRequest page = PageRequest.of(0, 2); List<Release> twoLatestActiveReleases = findActiveReleases(appId, clusterName, namespaceName, page); if (twoLatestActiveReleases == null || twoLatestActiveReleases.size() < 2) { throw new BadRequestException(String.format( "Can't rollback namespace(appId=%s, clusterName=%s, namespaceName=%s) because there is only one active release", appId, clusterName, namespaceName)); } release.setAbandoned(true); release.setDataChangeLastModifiedBy(operator); releaseRepository.save(release); releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, clusterName, twoLatestActiveReleases.get(1).getId(), release.getId(), ReleaseOperation.ROLLBACK, null, operator); rollbackChildNamespace(appId, clusterName, namespaceName, twoLatestActiveReleases, operator); return release; } ReleaseService(
      final ReleaseRepository releaseRepository,
      final ItemService itemService,
      final AuditService auditService,
      final NamespaceLockService namespaceLockService,
      final NamespaceService namespaceService,
      final NamespaceBranchService namespaceBranchService,
      final ReleaseHistoryService releaseHistoryService,
      final ItemSetService itemSetService); Release findOne(long releaseId); Release findActiveOne(long releaseId); List<Release> findByReleaseIds(Set<Long> releaseIds); List<Release> findByReleaseKeys(Set<String> releaseKeys); Release findLatestActiveRelease(Namespace namespace); Release findLatestActiveRelease(String appId, String clusterName, String namespaceName); List<Release> findAllReleases(String appId, String clusterName, String namespaceName, Pageable page); List<Release> findActiveReleases(String appId, String clusterName, String namespaceName, Pageable page); @Transactional Release mergeBranchChangeSetsAndRelease(Namespace namespace, String branchName, String releaseName,
                                                 String releaseComment, boolean isEmergencyPublish,
                                                 ItemChangeSets changeSets); @Transactional Release publish(Namespace namespace, String releaseName, String releaseComment,
                         String operator, boolean isEmergencyPublish); @Transactional Release grayDeletionPublish(Namespace namespace, String releaseName, String releaseComment,
                                     String operator, boolean isEmergencyPublish, Set<String> grayDelKeys); @Transactional Release rollback(long releaseId, String operator); @Transactional Release rollbackTo(long releaseId, long toReleaseId, String operator); @Transactional int batchDelete(String appId, String clusterName, String namespaceName, String operator);  }
@Test(expected = BadRequestException.class) public void testNamespaceNotExist() { when(releaseRepository.findById(releaseId)).thenReturn(Optional.of(firstRelease)); releaseService.rollback(releaseId, user); }
@Test(expected = BadRequestException.class) public void testHasNoRelease() { when(releaseRepository.findById(releaseId)).thenReturn(Optional.of(firstRelease)); when(releaseRepository.findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderByIdDesc(appId, clusterName, namespaceName, pageRequest)) .thenReturn(null); releaseService.rollback(releaseId, user); }
@Test public void testRollback() { when(releaseRepository.findById(releaseId)).thenReturn(Optional.of(firstRelease)); when(releaseRepository.findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderByIdDesc(appId, clusterName, namespaceName, pageRequest)) .thenReturn( Arrays.asList(firstRelease, secondRelease)); releaseService.rollback(releaseId, user); verify(releaseRepository).save(firstRelease); Assert.assertEquals(true, firstRelease.isAbandoned()); Assert.assertEquals(user, firstRelease.getDataChangeLastModifiedBy()); }