diff --git a/modules/apis/src/main/java/com/axway/apim/APIImportApp.java b/modules/apis/src/main/java/com/axway/apim/APIImportApp.java index 7450d63ca..4e91a9731 100644 --- a/modules/apis/src/main/java/com/axway/apim/APIImportApp.java +++ b/modules/apis/src/main/java/com/axway/apim/APIImportApp.java @@ -92,7 +92,7 @@ public int importAPI(APIImportParams params) { .useFEAPIDefinition(params.isUseFEAPIDefinition()) // Should API-Definition load from the FE-API? .build(); API actualAPI = apimAdapter.getApiAdapter().getAPI(filter, true); - APIChangeState changes = new APIChangeState(actualAPI, desiredAPI); + APIChangeState changes = new APIChangeState(actualAPI, desiredAPI, params); new APIImportManager().applyChanges(changes, params.isForceUpdate(), params.isUpdateOnly()); APIPropertiesExport.getInstance().store(); return 0; diff --git a/modules/apis/src/main/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPI.java b/modules/apis/src/main/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPI.java index 54dea0171..896c84fbc 100644 --- a/modules/apis/src/main/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPI.java +++ b/modules/apis/src/main/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPI.java @@ -34,16 +34,23 @@ public void execute(APIChangeState changes) throws AppException { APIImportParams apiImportParams = changes.getApiImportParams(); CreateNewAPI createNewAPI = new CreateNewAPI(); String createdApiId = createNewAPI.execute(changes, true); - if(apiImportParams != null && (apiImportParams.isReferenceAPIDeprecate() || apiImportParams.isReferenceAPIRetire())){ + if(handleOldApi(apiImportParams, actualAPI)){ LOG.info("New API successfully created"); - }else { - LOG.info("New API successfully created. Going to delete old API: {} {} (ID: {})", actualAPI.getName(), actualAPI.getVersion(), actualAPI.getId()); - // Delete the existing old API! - new APIStatusManager().update(actualAPI, API.STATE_DELETED, true); } // Maintain the Ehcache // All cached entities referencing this API must be updated with the correct API-ID APIManagerAdapter.getInstance().getCacheManager().flipApiId(changes.getActualAPI().getId(), createdApiId); } + public boolean handleOldApi( APIImportParams apiImportParams, API actualAPI) throws AppException { + if(apiImportParams != null && (apiImportParams.isReferenceAPIDeprecate() || apiImportParams.isReferenceAPIRetire())){ + return true; + }else { + LOG.info("New API successfully created. Going to delete old API: {} {} (ID: {})", actualAPI.getName(), actualAPI.getVersion(), actualAPI.getId()); + // Delete the existing old API! + new APIStatusManager().update(actualAPI, API.STATE_DELETED, true); + } + return false; + } + } diff --git a/modules/apis/src/test/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPITest.java b/modules/apis/src/test/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPITest.java index 517c976e4..5ba7c2dbb 100644 --- a/modules/apis/src/test/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPITest.java +++ b/modules/apis/src/test/java/com/axway/apim/apiimport/actions/RecreateToUpdateAPITest.java @@ -8,9 +8,11 @@ import com.axway.apim.api.model.Organization; import com.axway.apim.apiimport.APIChangeState; import com.axway.apim.apiimport.DesiredAPI; +import com.axway.apim.apiimport.lib.params.APIImportParams; import com.axway.apim.lib.CoreParameters; import com.axway.apim.lib.error.AppException; import com.axway.apim.lib.utils.Utils; +import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -22,6 +24,10 @@ public class RecreateToUpdateAPITest extends WiremockWrapper { @BeforeClass public void initWiremock() { super.initWiremock(); + CoreParameters coreParameters = new CoreParameters(); + coreParameters.setHostname("localhost"); + coreParameters.setUsername("test"); + coreParameters.setPassword(Utils.getEncryptedPassword()); } @AfterClass @@ -29,12 +35,11 @@ public void close() { super.close(); } + + @Test public void testRepublishToUpdateApi() throws AppException { - CoreParameters coreParameters = new CoreParameters(); - coreParameters.setHostname("localhost"); - coreParameters.setUsername("test"); - coreParameters.setPassword(Utils.getEncryptedPassword()); + APIManagerAdapter apiManagerAdapter = APIManagerAdapter.getInstance(); Organization organization = apiManagerAdapter.getOrgAdapter().getOrgForName("orga"); RecreateToUpdateAPI recreateToUpdateAPI = new RecreateToUpdateAPI(); @@ -67,4 +72,47 @@ public void testRepublishToUpdateApi() throws AppException { recreateToUpdateAPI.execute(apiChangeState); apiManagerAdapter.deleteInstance(); } + + @Test + public void handleApiDontDelete() throws AppException { + RecreateToUpdateAPI recreateToUpdateAPI = new RecreateToUpdateAPI(); + APIImportParams apiImportParams = new APIImportParams(); + apiImportParams.setReferenceAPIRetire(true); + Assert.assertTrue(recreateToUpdateAPI.handleOldApi(apiImportParams, null)); + } + + @Test + public void handleApiDontDeleteDeprecate() throws AppException { + RecreateToUpdateAPI recreateToUpdateAPI = new RecreateToUpdateAPI(); + APIImportParams apiImportParams = new APIImportParams(); + apiImportParams.setReferenceAPIDeprecate(true); + Assert.assertTrue(recreateToUpdateAPI.handleOldApi(apiImportParams, null)); + } + + @Test + public void handleApiDontDeleteDeprecateAndRetire() throws AppException { + RecreateToUpdateAPI recreateToUpdateAPI = new RecreateToUpdateAPI(); + APIImportParams apiImportParams = new APIImportParams(); + apiImportParams.setReferenceAPIDeprecate(true); + apiImportParams.setReferenceAPIRetire(true); + Assert.assertTrue(recreateToUpdateAPI.handleOldApi(apiImportParams, null)); + } + + @Test + public void handleApiDelete() throws AppException { + API actualAPI = new API(); + actualAPI.setName("petstore"); + actualAPI.setPath("/api/v3"); + actualAPI.setVersion("1.1"); + actualAPI.setDescriptionType("original"); + actualAPI.setSummary("Petstore api"); + actualAPI.setState("published"); + actualAPI.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150"); + actualAPI.setApplications(new ArrayList<>()); + actualAPI.setApiId("1f4263ca-7f03-41d9-9d34-9eff79d29bd8"); + + RecreateToUpdateAPI recreateToUpdateAPI = new RecreateToUpdateAPI(); + Assert.assertFalse(recreateToUpdateAPI.handleOldApi(null, actualAPI)); + } + }