use of org.wso2.carbon.apimgt.core.models.CompositeAPI in project carbon-apimgt by wso2.
the class APIStoreImpl method searchCompositeAPIs.
@Override
public List<CompositeAPI> searchCompositeAPIs(String query, int offset, int limit) throws APIManagementException {
List<CompositeAPI> apiResults;
// this should be current logged in user
String user = getUsername();
// role list of current user
Set<String> roles = APIUtils.getAllRolesOfUser(user);
try {
if (query != null && !query.isEmpty()) {
apiResults = getApiDAO().searchCompositeAPIs(roles, user, query, offset, limit);
} else {
apiResults = getApiDAO().getCompositeAPIs(roles, user, offset, limit);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating searching APIs - " + query;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
return apiResults;
}
use of org.wso2.carbon.apimgt.core.models.CompositeAPI in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteCompositeApi.
/**
* {@inheritDoc}
*/
@Override
public void deleteCompositeApi(String apiId) throws APIManagementException {
try {
CompositeAPI api = getApiDAO().getCompositeAPI(apiId);
// Delete API in gateway
gateway.deleteCompositeAPI(api);
getApiDAO().deleteCompositeApi(apiId);
} catch (GatewayException e) {
String message = "Error occurred while deleting Composite API with id - " + apiId + " from gateway";
throw new APIManagementException(message, e, ExceptionCodes.GATEWAY_EXCEPTION);
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting the Composite API with id " + apiId;
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.core.models.CompositeAPI in project carbon-apimgt by wso2.
the class APIStoreImpl method createNewCompositeApiVersion.
/**
* {@inheritDoc}
*/
@Override
public String createNewCompositeApiVersion(String apiId, String newVersion) throws APIManagementException {
// validate parameters
if (StringUtils.isEmpty(newVersion)) {
String errorMsg = "New API version cannot be empty";
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
if (StringUtils.isEmpty(apiId)) {
String errorMsg = "API ID cannot be empty";
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
String newVersionedId;
try {
CompositeAPI api = getApiDAO().getCompositeAPI(apiId);
if (api.getVersion().equals(newVersion)) {
String errMsg = "New API version " + newVersion + " cannot be same as the previous version for " + "API " + api.getName();
log.error(errMsg);
throw new APIManagementException(errMsg, ExceptionCodes.API_ALREADY_EXISTS);
}
CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder(api);
apiBuilder.id(UUID.randomUUID().toString());
apiBuilder.version(newVersion);
apiBuilder.context(api.getContext().replace(api.getVersion(), newVersion));
apiBuilder.copiedFromApiId(api.getId());
if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
}
getApiDAO().addApplicationAssociatedAPI(apiBuilder.build());
newVersionedId = apiBuilder.getId();
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't create new API version from " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
return newVersionedId;
}
use of org.wso2.carbon.apimgt.core.models.CompositeAPI in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testUpdateCompositeApi.
@Test(description = "Update Composite API")
public void testUpdateCompositeApi() throws APIManagementException {
// Add a new Composite API
CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
IdentityProvider idp = Mockito.mock(IdentityProvider.class);
APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
String ballerinaImpl = "Ballerina";
apiStore.addCompositeApi(apiBuilder);
CompositeAPI createdAPI = apiBuilder.build();
// Update existing Composite API
apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
apiBuilder.id(createdAPI.getId());
apiBuilder.name(createdAPI.getName());
apiBuilder.provider(createdAPI.getProvider());
apiBuilder.version(createdAPI.getVersion());
apiBuilder.context(createdAPI.getContext());
Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
Mockito.when(apiDAO.getCompositeAPIGatewayConfig(apiBuilder.getId())).thenReturn(new ByteArrayInputStream(ballerinaImpl.getBytes(StandardCharsets.UTF_8)));
Mockito.when(gatewaySourceGenerator.getGatewayConfigFromSwagger(Matchers.anyString(), Matchers.anyString())).thenReturn(ballerinaImpl);
apiStore.updateCompositeApi(apiBuilder);
CompositeAPI updatedAPI = apiBuilder.build();
Assert.assertEquals(updatedAPI.getId(), createdAPI.getId());
Assert.assertEquals(updatedAPI.getName(), createdAPI.getName());
Assert.assertEquals(updatedAPI.getProvider(), createdAPI.getProvider());
Assert.assertEquals(updatedAPI.getVersion(), createdAPI.getVersion());
Assert.assertEquals(updatedAPI.getContext(), createdAPI.getContext());
}
use of org.wso2.carbon.apimgt.core.models.CompositeAPI in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testCreateNewCompositeApiVersion.
@Test(description = "Create new Composite API version")
public void testCreateNewCompositeApiVersion() throws APIManagementException {
// Add a new Composite API
CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
IdentityProvider idp = Mockito.mock(IdentityProvider.class);
APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
apiStore.addCompositeApi(apiBuilder);
CompositeAPI createdAPI = apiBuilder.build();
// Create new API version
String newVersion = java.util.UUID.randomUUID().toString();
Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
apiStore.createNewCompositeApiVersion(createdAPI.getId(), newVersion);
final ArgumentCaptor<CompositeAPI> captor = ArgumentCaptor.forClass(CompositeAPI.class);
Mockito.verify(apiDAO, Mockito.times(2)).addApplicationAssociatedAPI(captor.capture());
CompositeAPI newAPIVersion = captor.getValue();
Assert.assertEquals(newAPIVersion.getVersion(), newVersion);
Assert.assertNotEquals(newAPIVersion.getId(), createdAPI.getId());
Assert.assertEquals(newAPIVersion.getCopiedFromApiId(), createdAPI.getId());
}
Aggregations