use of org.wso2.carbon.apimgt.persistence.dto.UserContext in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testRegistrySelectionForTenantUser.
@Test
public void testRegistrySelectionForTenantUser() throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(tenantManager.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID);
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext context = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
PowerMockito.mockStatic(GovernanceUtils.class);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
artifacts.add(artifact);
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
RealmService realmService = Mockito.mock(RealmService.class);
PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
UserRealm realm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(TENANT_ID)).thenReturn(realm);
PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
Mockito.when(context.getTenantDomain()).thenReturn(TENANT_DOMAIN);
Mockito.when(context.getTenantId()).thenReturn(TENANT_ID);
// return null artifact because we are not testing artifact related params. this is only to get the registry obj
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// trigger registry object creation
UserContext ctx = new UserContext("user", new Organization(TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("user", TENANT_ID);
ctx = new UserContext("wso2.anonymous.user", new Organization(TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("wso2.anonymous.user", TENANT_ID);
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext in project carbon-apimgt by wso2.
the class RegistryPersistenceImplTestCase method testRegistrySelectionForSuperTenantUser.
@Test
public void testRegistrySelectionForSuperTenantUser() throws Exception {
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(tenantManager.getTenantId(SUPER_TENANT_DOMAIN)).thenReturn(SUPER_TENANT_ID);
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext context = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(context);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedContext);
PowerMockito.mockStatic(GovernanceUtils.class);
GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
artifacts.add(artifact);
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.any(String.class), Mockito.any(Registry.class), Mockito.any(String.class), Mockito.any(Boolean.class))).thenReturn(artifacts);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
ServiceReferenceHolder serviceRefHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceRefHolder);
RealmService realmService = Mockito.mock(RealmService.class);
PowerMockito.when(serviceRefHolder.getRealmService()).thenReturn(realmService);
UserRealm realm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(SUPER_TENANT_ID)).thenReturn(realm);
PowerMockito.doNothing().when(RegistryPersistenceUtil.class, "loadloadTenantAPIRXT", Mockito.any(String.class), Mockito.any(Integer.class));
Mockito.when(context.getTenantDomain()).thenReturn(SUPER_TENANT_DOMAIN);
Mockito.when(context.getTenantId()).thenReturn(SUPER_TENANT_ID);
APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// return null artifact because we are not testing artifact related params. this is only to get the registry obj
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.any(String.class))).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.any(String.class))).thenReturn(null);
// trigger registry object creation
UserContext ctx = new UserContext("user", new Organization(SUPER_TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("user", SUPER_TENANT_ID);
apiPersistenceInstance = new RegistryPersistenceImplWrapper(tenantManager, registryService);
// trigger registry object creation
ctx = new UserContext("wso2.anonymous.user", new Organization(SUPER_TENANT_DOMAIN), null, null);
apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
Mockito.verify(registryService, times(1)).getGovernanceUserRegistry("wso2.anonymous.user", SUPER_TENANT_ID);
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchAPIsForPublisher.
@Override
public PublisherAPISearchResult searchAPIsForPublisher(Organization org, String searchQuery, int start, int offset, UserContext ctx, String sortBy, String sortOrder) throws APIPersistenceException {
String requestedTenantDomain = org.getName();
boolean isTenantFlowStarted = false;
PublisherAPISearchResult result = null;
try {
RegistryHolder holder = getRegistry(requestedTenantDomain);
Registry sysRegistry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
int tenantIDLocal = holder.getTenantId();
log.debug("Requested query for publisher search: " + searchQuery);
String modifiedQuery = RegistrySearchUtil.getPublisherSearchQuery(searchQuery, ctx);
log.debug("Modified query for publisher search: " + modifiedQuery);
String tenantAdminUsername = getTenantAwareUsername(RegistryPersistenceUtil.getTenantAdminUserName(requestedTenantDomain));
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(tenantAdminUsername);
if (searchQuery != null && searchQuery.startsWith(APIConstants.DOCUMENTATION_SEARCH_TYPE_PREFIX)) {
result = searchPaginatedPublisherAPIsByDoc(sysRegistry, tenantIDLocal, searchQuery.split(":")[1], tenantAdminUsername, start, offset);
} else {
result = searchPaginatedPublisherAPIs(sysRegistry, tenantIDLocal, modifiedQuery, start, offset);
}
} catch (APIManagementException e) {
throw new APIPersistenceException("Error while searching APIs ", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return result;
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext in project carbon-apimgt by wso2.
the class RegistrySearchUtilTestCase method testAdminUserQueryInPublisher.
@Test
public void testAdminUserQueryInPublisher() throws APIPersistenceException {
// Normal publisher api listing
String inputQuery = "";
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("isAdmin", true);
UserContext ctx = new UserContext("admin", organization, properties, new String[] { "admin" });
String searchQuery = RegistrySearchUtil.getPublisherSearchQuery(inputQuery, ctx);
String expected = "name=*&type=(HTTP OR WS OR SOAPTOREST OR GRAPHQL OR SOAP OR SSE OR WEBSUB OR WEBHOOK OR " + "ASYNC)";
Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
// search for 'test' in description
inputQuery = "description:test";
expected = "description=*test*&type=(HTTP OR WS OR SOAPTOREST OR GRAPHQL OR SOAP OR SSE OR WEBSUB OR WEBHOOK " + "OR ASYNC)";
searchQuery = RegistrySearchUtil.getPublisherSearchQuery(inputQuery, ctx);
Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
// search for provider 'pubuser'
inputQuery = "provider:pubuser";
expected = "provider=*pubuser*&type=(HTTP OR WS OR SOAPTOREST OR GRAPHQL OR SOAP OR SSE OR WEBSUB OR WEBHOOK " + "OR ASYNC)";
searchQuery = RegistrySearchUtil.getPublisherSearchQuery(inputQuery, ctx);
Assert.assertEquals("Generated query mismatched for provider search. ", expected, searchQuery);
// search for propertyname 'test'
inputQuery = "property_name:test";
expected = "api_meta.property_name=*test*&type=(HTTP OR WS OR SOAPTOREST OR GRAPHQL OR SOAP OR SSE OR " + "WEBSUB OR WEBHOOK OR ASYNC)";
searchQuery = RegistrySearchUtil.getPublisherSearchQuery(inputQuery, ctx);
Assert.assertEquals("Generated query mismatched for property search. ", expected, searchQuery);
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext in project carbon-apimgt by wso2.
the class APIProviderImpl method searchPaginatedAPIProducts.
/**
* Returns APIProduct Search result based on the provided query.
*
* @param registry
* @param searchQuery Ex: provider=*admin*
* @return APIProduct result
* @throws APIManagementException
*/
public Map<String, Object> searchPaginatedAPIProducts(Registry registry, String searchQuery, int start, int end) throws APIManagementException {
SortedSet<APIProduct> productSet = new TreeSet<APIProduct>(new APIProductNameComparator());
List<APIProduct> productList = new ArrayList<APIProduct>();
Map<String, Object> result = new HashMap<String, Object>();
if (log.isDebugEnabled()) {
log.debug("Original search query received : " + searchQuery);
}
Organization org = new Organization(tenantDomain);
String[] roles = APIUtil.getFilteredUserRoles(userNameWithoutChange);
Map<String, Object> properties = APIUtil.getUserProperties(userNameWithoutChange);
UserContext userCtx = new UserContext(userNameWithoutChange, org, properties, roles);
try {
PublisherAPIProductSearchResult searchAPIs = apiPersistenceInstance.searchAPIProductsForPublisher(org, searchQuery, start, end, userCtx);
if (log.isDebugEnabled()) {
log.debug("searched API products for query : " + searchQuery + " :-->: " + searchAPIs.toString());
}
if (searchAPIs != null) {
List<PublisherAPIProductInfo> list = searchAPIs.getPublisherAPIProductInfoList();
List<Object> apiList = new ArrayList<>();
for (PublisherAPIProductInfo publisherAPIInfo : list) {
APIProduct mappedAPI = new APIProduct(new APIProductIdentifier(publisherAPIInfo.getProviderName(), publisherAPIInfo.getApiProductName(), publisherAPIInfo.getVersion()));
mappedAPI.setUuid(publisherAPIInfo.getId());
mappedAPI.setState(publisherAPIInfo.getState());
mappedAPI.setContext(publisherAPIInfo.getContext());
mappedAPI.setApiSecurity(publisherAPIInfo.getApiSecurity());
populateAPIStatus(mappedAPI);
productList.add(mappedAPI);
}
productSet.addAll(productList);
result.put("products", productSet);
result.put("length", searchAPIs.getTotalAPIsCount());
result.put("isMore", true);
} else {
result.put("products", productSet);
result.put("length", 0);
result.put("isMore", false);
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while searching the api ", e);
}
return result;
}
Aggregations