use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class APIUtilRolesTest method testCreateDefaultRoles.
@Test
public void testCreateDefaultRoles() throws Exception {
System.setProperty("carbon.home", APIUtilRolesTest.class.getResource("/").getFile());
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
final int tenantId = MultitenantConstants.SUPER_TENANT_ID;
final String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
File siteConfFile = new File(Thread.currentThread().getContextClassLoader().getResource("tenant-conf.json").getFile());
String tenantConfValue = FileUtils.readFileToString(siteConfFile);
InputStream signUpConfStream = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("default-sign-up-config.xml").getFile());
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RealmService realmService = Mockito.mock(RealmService.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
TenantIndexingLoader indexingLoader = Mockito.mock(TenantIndexingLoader.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
RealmConfiguration realmConfiguration = Mockito.mock(RealmConfiguration.class);
APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerComponent.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(serviceReferenceHolder.getIndexLoaderService()).thenReturn(indexingLoader);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(realmService.getBootstrapRealm()).thenReturn(userRealm);
Mockito.when(realmService.getTenantUserRealm(tenantId)).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userRealm.getRealmConfiguration()).thenReturn(realmConfiguration);
Mockito.when(realmConfiguration.getAdminUserName()).thenReturn("admin");
Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
Mockito.when(tenantManager.getDomain(tenantId)).thenReturn(tenantDomain);
Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
Mockito.when(apimConfigService.getTenantConfig(tenantDomain)).thenReturn(tenantConfValue);
Mockito.when(apimConfigService.getSelfSighupConfig(tenantDomain)).thenReturn(IOUtils.toString(signUpConfStream));
APIUtil.createDefaultRoles(tenantId);
String[] adminName = { "admin" };
Mockito.verify(userStoreManager, Mockito.atLeastOnce()).addRole(eq("Internal/publisher"), eq(adminName), new Permission[] { Mockito.any(Permission.class) });
Mockito.verify(userStoreManager, Mockito.atLeastOnce()).addRole(eq("Internal/subscriber"), eq(adminName), new Permission[] { Mockito.any(Permission.class) });
Mockito.verify(userStoreManager, Mockito.atLeastOnce()).addRole(eq("Internal/creator"), eq(adminName), new Permission[] { Mockito.any(Permission.class) });
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class APIUtilTest method testIsPerAPISequence.
@Test
public void testIsPerAPISequence() throws Exception {
APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry registry = Mockito.mock(UserRegistry.class);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "in" + RegistryConstants.PATH_SEPARATOR;
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Mockito.when(registry.resourceExists(eq(path))).thenReturn(true);
Collection collection = Mockito.mock(Collection.class);
Mockito.when(registry.get(eq(path))).thenReturn(collection);
String[] childPaths = { "test" };
Mockito.when(collection.getChildren()).thenReturn(childPaths);
InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
Resource resource = Mockito.mock(Resource.class);
Mockito.when(registry.get(eq("test"))).thenReturn(resource);
Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
boolean isPerAPiSequence = APIUtil.isPerAPISequence("sample", 1, apiIdentifier, "in");
Assert.assertTrue(isPerAPiSequence);
}
use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class APIUtilTest method testGetRoleNamesNonSuperTenant.
@Test
public void testGetRoleNamesNonSuperTenant() throws Exception {
String userName = "John";
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
String[] roleNames = { "role1", "role2" };
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(MultitenantUtils.class);
Mockito.when(MultitenantUtils.getTenantDomain(userName)).thenReturn("test.com");
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(realmService.getTenantUserRealm(Mockito.anyInt())).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userStoreManager.getRoleNames()).thenReturn(roleNames);
Assert.assertEquals(roleNames, APIUtil.getRoleNames(userName));
}
use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class APIUtilTest method testGetCustomSequenceNotFound.
@Test
public void testGetCustomSequenceNotFound() throws Exception {
APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Collection collection = Mockito.mock(Collection.class);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "custom" + RegistryConstants.PATH_SEPARATOR;
Mockito.when(registry.get(eq(path))).thenReturn(null, collection);
String[] childPaths = { "test" };
Mockito.when(collection.getChildren()).thenReturn(childPaths);
String expectedUUID = UUID.randomUUID().toString();
InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
Resource resource = Mockito.mock(Resource.class);
Mockito.when(registry.get(eq("test"))).thenReturn(resource);
Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
OMElement customSequence = APIUtil.getCustomSequence("sample", 1, "custom", apiIdentifier);
Assert.assertNotNull(customSequence);
sampleSequence.close();
}
use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.
the class APIUtilTest method testCreateSwaggerJSONContent.
@Test
public void testCreateSwaggerJSONContent() throws Exception {
System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Environment environment = Mockito.mock(Environment.class);
Map<String, Environment> environmentMap = new HashMap<String, Environment>();
environmentMap.put("Production", environment);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getApiGatewayEnvironments()).thenReturn(environmentMap);
Mockito.when(environment.getApiGatewayEndpoint()).thenReturn("");
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
PowerMockito.mockStatic(ApiMgtDAO.class);
Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
Mockito.when(apiMgtDAO.getAllEnvironments(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(new ArrayList<org.wso2.carbon.apimgt.api.model.Environment>());
String swaggerJSONContent = APIUtil.createSwaggerJSONContent(getUniqueAPI());
Assert.assertNotNull(swaggerJSONContent);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
Aggregations