@@ -59,12 +59,12 @@ void setUp() throws Exception {
5959 private static Stream <Arguments > testCalculateExistingAppsToBackup () {
6060 return Stream .of (
6161 // (1) Already deployed application match version of current deployment descriptor
62- Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "1" )), Collections .emptyList (), "1" ,
62+ Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "1" )), Collections .emptyList (), "1" , true ,
6363 List .of ("app-1-live" ), List .of ()),
6464 // (2) Current deployment descriptor version has different value than deployed mta
6565 Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "1" ),
6666 new TestApplication ("app-1" , "app-1-idle" , "2" , ProductizationState .IDLE )),
67- Collections .emptyList (), "2" , List .of ("app-1-live" , "app-1-idle" ),
67+ Collections .emptyList (), "2" , true , List .of ("app-1-live" , "app-1-idle" ),
6868 List .of (ImmutableCloudApplication .builder ()
6969 .name ("app-1-live" )
7070 .v3Metadata (Metadata .builder ()
@@ -74,41 +74,48 @@ private static Stream<Arguments> testCalculateExistingAppsToBackup() {
7474 .build ())),
7575 // (3) Current deployment descriptor match version of deployed and backup mta
7676 Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "1" )),
77- List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "1" , List .of ("app-1-live" ),
77+ List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "1" , true , List .of ("app-1-live" ),
7878 Collections .emptyList ()),
7979 // (4) Current deployment descriptor version has different value of deployed and backup mta
8080 Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "2" )),
81- List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "3" , List .of ("app-1-live" ,
82- "mta-backup-app-1" ),
83- List .of (ImmutableCloudApplication .builder ()
84- .name ("app-1-live" )
85- .v3Metadata (Metadata .builder ()
86- .annotation (MtaMetadataAnnotations .MTA_VERSION ,
87- "2" )
88- .build ())
89- .build ())),
81+ List .of (new TestApplication ("app-1" ,
82+ "mta-backup-app-1" ,
83+ "1" )),
84+ "3" , true , List .of ("app-1-live" , "mta-backup-app-1" ), List .of (ImmutableCloudApplication .builder ()
85+ .name ("app-1-live" )
86+ .v3Metadata (Metadata .builder ()
87+ .annotation (MtaMetadataAnnotations .MTA_VERSION ,
88+ "2" )
89+ .build ())
90+ .build ())),
9091 // (5) Current deployment descriptor match version of deployed mta only
9192 Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "2" )),
92- List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "2" , List .of ("app-1-live" ),
93+ List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "2" , true , List .of ("app-1-live" ),
9394 Collections .emptyList ()),
9495 // (6) Current deployment descriptor version match value of backup mta
9596 Arguments .of (List .of (new TestApplication ("app-1" , "app-1-live" , "2" )),
96- List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "1" ,
97- List .of ("app-1-live" , "app-1-idle" ), Collections .emptyList ()));
97+ List .of (new TestApplication ("app-1" , "mta-backup-app-1" , "1" )), "1" , true ,
98+ List .of ("app-1-live" , "app-1-idle" ), Collections .emptyList ()),
99+ // (7) Deployed mta does not have backup descriptor in db and won't be preserved
100+ Arguments .of (List .of (new TestApplication ("app-1" , "app-1" , "1" ), new TestApplication ("app-2" , "app-2" , "1" )),
101+ Collections .emptyList (), "2" , false , Collections .emptyList (), Collections .emptyList ()));
98102 }
99103
100104 @ ParameterizedTest
101105 @ MethodSource
102106 void testCalculateExistingAppsToBackup (List <TestApplication > deployedApplications , List <TestApplication > backupApplications ,
103- String mtaVersionOfCurrentDescriptor , List < String > appNamesToUndeploy ,
104- List <CloudApplication > expectedAppsToBackup ) {
107+ String mtaVersionOfCurrentDescriptor , boolean isDescriptorAvailableInDb ,
108+ List <String > appNamesToUndeploy , List < CloudApplication > expectedAppsToBackup ) {
105109 DeployedMta deployedMta = getDeployedMta (deployedApplications );
106110 DeployedMta backupMta = getDeployedMta (backupApplications );
107111
112+ prepareContext (isDescriptorAvailableInDb );
113+
108114 ExistingAppsToBackupCalculator calculator = new ExistingAppsToBackupCalculator (deployedMta , backupMta , descriptorBackupService );
109115
110116 List <CloudApplication > appsToUndeploy = getAppsToUndeploy (deployedMta .getApplications (), appNamesToUndeploy );
111- List <CloudApplication > appsToBackup = calculator .calculateExistingAppsToBackup (appsToUndeploy , mtaVersionOfCurrentDescriptor );
117+ List <CloudApplication > appsToBackup = calculator .calculateExistingAppsToBackup (context , appsToUndeploy ,
118+ mtaVersionOfCurrentDescriptor );
112119
113120 assertEquals (expectedAppsToBackup , appsToBackup );
114121 }
@@ -159,15 +166,7 @@ void testCalculateAppsToUndeploy(List<TestApplication> deployedBackupApps, List<
159166 boolean isDescriptorAvailableInDb , List <CloudApplication > expectedAppsToUndeploy ) {
160167 DeployedMta backupMta = getDeployedMta (deployedBackupApps );
161168
162- when (context .getVariable (Variables .MTA_ID )).thenReturn (MTA_ID );
163- when (context .getVariable (Variables .SPACE_GUID )).thenReturn (SPACE_GUID );
164- when (descriptorBackupService .createQuery ()).thenReturn (descriptorBackupQuery );
165- when (descriptorBackupQuery .mtaId (anyString ())).thenReturn (descriptorBackupQuery );
166- when (descriptorBackupQuery .spaceId (anyString ())).thenReturn (descriptorBackupQuery );
167- when (descriptorBackupQuery .namespace (any ())).thenReturn (descriptorBackupQuery );
168- when (descriptorBackupQuery .mtaVersion (anyString ())).thenReturn (descriptorBackupQuery );
169- when (descriptorBackupQuery .list ()).thenReturn (isDescriptorAvailableInDb ? List .of (Mockito .mock (BackupDescriptor .class ))
170- : Collections .emptyList ());
169+ prepareContext (isDescriptorAvailableInDb );
171170
172171 ExistingAppsToBackupCalculator calculator = new ExistingAppsToBackupCalculator (null , backupMta , descriptorBackupService );
173172
@@ -210,6 +209,18 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
210209 .build ();
211210 }
212211
212+ private void prepareContext (boolean isDescriptorAvailableInDb ) {
213+ when (context .getVariable (Variables .MTA_ID )).thenReturn (MTA_ID );
214+ when (context .getVariable (Variables .SPACE_GUID )).thenReturn (SPACE_GUID );
215+ when (descriptorBackupService .createQuery ()).thenReturn (descriptorBackupQuery );
216+ when (descriptorBackupQuery .mtaId (anyString ())).thenReturn (descriptorBackupQuery );
217+ when (descriptorBackupQuery .spaceId (anyString ())).thenReturn (descriptorBackupQuery );
218+ when (descriptorBackupQuery .namespace (any ())).thenReturn (descriptorBackupQuery );
219+ when (descriptorBackupQuery .mtaVersion (anyString ())).thenReturn (descriptorBackupQuery );
220+ when (descriptorBackupQuery .list ()).thenReturn (isDescriptorAvailableInDb ? List .of (Mockito .mock (BackupDescriptor .class ))
221+ : Collections .emptyList ());
222+ }
223+
213224 private List <CloudApplication > getAppsToUndeploy (List <DeployedMtaApplication > deployedApplications , List <String > appNamesToUndeploy ) {
214225 return deployedApplications .stream ()
215226 .filter (deployedApplication -> appNamesToUndeploy .contains (deployedApplication .getName ()))
0 commit comments