use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImplTest method createMockAPIProduct.
private APIProduct createMockAPIProduct(String provider) {
APIProductIdentifier productIdentifier = new APIProductIdentifier(provider, APIConstants.API_PRODUCT, APIConstants.API_PRODUCT_VERSION);
APIProduct apiProduct = new APIProduct(productIdentifier);
apiProduct.setContext("/test");
apiProduct.setState(APIConstants.CREATED);
apiProduct.setType(APIConstants.API_PRODUCT);
apiProduct.setOrganization(APIConstants.SUPER_TENANT_DOMAIN);
return apiProduct;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImplTest method prepareForLCStateChangeOfAPIProduct.
private void prepareForLCStateChangeOfAPIProduct(APIProviderImplWrapper apiProvider, APIProduct apiProduct) throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PrivilegedCarbonContext prContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prContext);
PowerMockito.doNothing().when(prContext).setUsername(Mockito.anyString());
PowerMockito.doNothing().when(prContext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
Mockito.when(artifactManager.getGenericArtifact(any(String.class))).thenReturn(artifact);
PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(artifact, apiProduct)).thenReturn(artifact);
PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, artifact.getId())).thenReturn(artifactPath);
Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
Mockito.when(apiProvider.registry.resourceExists(artifactPath)).thenReturn(false);
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateAPIProductForStateChange.
@Test
public void testUpdateAPIProductForStateChange() throws Exception {
String provider = "admin";
PowerMockito.mockStatic(MultitenantUtils.class);
Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(APIConstants.SUPER_TENANT_DOMAIN);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
APIProduct apiProduct = createMockAPIProduct(provider);
prepareForLCStateChangeOfAPIProduct(apiProvider, apiProduct);
PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
Organization organization = new Organization(APIConstants.SUPER_TENANT_DOMAIN);
Mockito.when(apiPersistenceInstance.updateAPIProduct(organization, publisherAPIProduct)).thenReturn(publisherAPIProduct);
apiProvider.updateAPIProductForStateChange(apiProduct, APIConstants.CREATED, APIConstants.PUBLISHED);
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateAPIProduct.
public void updateAPIProduct(APIProduct product, String username) throws APIManagementException {
Connection conn = null;
PreparedStatement ps = null;
if (log.isDebugEnabled()) {
log.debug("updateAPIProduct() : product- " + product.toString());
}
try {
conn = APIMgtDBUtil.getConnection();
conn.setAutoCommit(false);
String query = SQLConstants.UPDATE_PRODUCT_SQL;
ps = conn.prepareStatement(query);
ps.setString(1, product.getProductLevelPolicy());
ps.setString(2, username);
ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
ps.setString(4, product.getGatewayVendor());
APIProductIdentifier identifier = product.getId();
ps.setString(5, identifier.getName());
ps.setString(6, APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
ps.setString(7, identifier.getVersion());
ps.executeUpdate();
int productId = getAPIID(product.getUuid(), conn);
updateAPIProductResourceMappings(product, productId, conn);
conn.commit();
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
log.error("Error while rolling back the failed operation", e1);
}
}
handleException("Error in updating API Product: " + e.getMessage(), e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, null);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method addAPIProductWithoutPublishingToGateway.
@Override
public Map<API, List<APIProductResource>> addAPIProductWithoutPublishingToGateway(APIProduct product) throws APIManagementException {
Map<API, List<APIProductResource>> apiToProductResourceMapping = new HashMap<>();
validateApiProductInfo(product);
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
if (log.isDebugEnabled()) {
log.debug("API Product details successfully added to the registry. API Product Name: " + product.getId().getName() + ", API Product Version : " + product.getId().getVersion() + ", API Product context : " + // todo: log context
"change");
}
List<APIProductResource> resources = product.getProductResources();
// list to hold resources which are actually in an existing api. If user has created an API product with invalid
// API or invalid resource of a valid API, that content will be removed .validResources array will have only
// legitimate apis
List<APIProductResource> validResources = new ArrayList<APIProductResource>();
for (APIProductResource apiProductResource : resources) {
API api;
String apiUUID;
if (apiProductResource.getProductIdentifier() != null) {
APIIdentifier productAPIIdentifier = apiProductResource.getApiIdentifier();
String emailReplacedAPIProviderName = APIUtil.replaceEmailDomain(productAPIIdentifier.getProviderName());
APIIdentifier emailReplacedAPIIdentifier = new APIIdentifier(emailReplacedAPIProviderName, productAPIIdentifier.getApiName(), productAPIIdentifier.getVersion());
apiUUID = apiMgtDAO.getUUIDFromIdentifier(emailReplacedAPIIdentifier, product.getOrganization());
api = getAPIbyUUID(apiUUID, product.getOrganization());
} else {
apiUUID = apiProductResource.getApiId();
api = getAPIbyUUID(apiUUID, product.getOrganization());
// if API does not exist, getLightweightAPIByUUID() method throws exception.
}
if (api != null) {
validateApiLifeCycleForApiProducts(api);
if (api.getSwaggerDefinition() != null) {
api.setSwaggerDefinition(getOpenAPIDefinition(apiUUID, product.getOrganization()));
}
if (!apiToProductResourceMapping.containsKey(api)) {
apiToProductResourceMapping.put(api, new ArrayList<>());
}
List<APIProductResource> apiProductResources = apiToProductResourceMapping.get(api);
apiProductResources.add(apiProductResource);
apiProductResource.setApiIdentifier(api.getId());
apiProductResource.setProductIdentifier(product.getId());
if (api.isAdvertiseOnly()) {
apiProductResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
} else {
apiProductResource.setEndpointConfig(api.getEndpointConfig());
}
apiProductResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
URITemplate uriTemplate = apiProductResource.getUriTemplate();
Map<String, URITemplate> templateMap = apiMgtDAO.getURITemplatesForAPI(api);
if (uriTemplate == null) {
// if no resources are define for the API, we ingore that api for the product
} else {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getResourceURI();
if (templateMap.containsKey(key)) {
// Since the template ID is not set from the request, we manually set it.
uriTemplate.setId(templateMap.get(key).getId());
// request has a valid API id and a valid resource. we add it to valid resource map
validResources.add(apiProductResource);
} else {
// ignore
log.warn("API with id " + apiProductResource.getApiId() + " does not have a resource " + uriTemplate.getResourceURI() + " with http method " + uriTemplate.getHTTPVerb());
}
}
}
}
// set the valid resources only
product.setProductResources(validResources);
// now we have validated APIs and it's resources inside the API product. Add it to database
String provider = APIUtil.replaceEmailDomain(product.getId().getProviderName());
// Set version timestamp
product.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
// Create registry artifact
String apiProductUUID = createAPIProduct(product);
product.setUuid(apiProductUUID);
// Add to database
apiMgtDAO.addAPIProduct(product, product.getOrganization());
return apiToProductResourceMapping;
}
Aggregations