Skip to content

Commit

Permalink
Fix issue #511
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Oct 28, 2024
1 parent f6dd645 commit 9b017f3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,19 +24,22 @@ 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
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();
Expand Down Expand Up @@ -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));
}

}

0 comments on commit 9b017f3

Please sign in to comment.