Search in sources :

Example 1 with TENANT_DOMAIN

use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testRegistrySelectionForTenantUserCrossTenatAccess.

@Test
public void testRegistrySelectionForTenantUserCrossTenatAccess() throws Exception {
    RegistryService registryService = Mockito.mock(RegistryService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    Mockito.when(tenantManager.getTenantId(TENANT_DOMAIN)).thenReturn(TENANT_ID);
    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(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);
    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. access super tenant api
    UserContext ctx = new UserContext("user", new Organization(TENANT_DOMAIN), null, null);
    apiPersistenceInstance.searchAPIsForDevPortal(new Organization(SUPER_TENANT_DOMAIN), "", 0, 10, ctx);
    // check whether super tenant's system registy is accessed
    Mockito.verify(registryService, times(1)).getGovernanceSystemRegistry((SUPER_TENANT_ID));
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.persistence.internal.ServiceReferenceHolder) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) ArrayList(java.util.ArrayList) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Matchers.anyString(org.mockito.Matchers.anyString) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) CarbonContext(org.wso2.carbon.context.CarbonContext) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with TENANT_DOMAIN

use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.

the class RegistryPersistenceUtilTestCase method testTenantDomain.

@Test
public void testTenantDomain() {
    PowerMockito.mockStatic(ServerConfiguration.class);
    ServerConfiguration config = Mockito.mock(ServerConfiguration.class);
    PowerMockito.when(ServerConfiguration.getInstance()).thenReturn(config);
    PowerMockito.when(config.getFirstProperty(anyString())).thenReturn(null);
    String domain = RegistryPersistenceUtil.getTenantDomain(new APIIdentifier("test@" + TENANT_DOMAIN, "test", "1.0"));
    domain = RegistryPersistenceUtil.getTenantDomain(new APIIdentifier("test", "test", "1.0"));
}
Also used : ServerConfiguration(org.wso2.carbon.base.ServerConfiguration) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with TENANT_DOMAIN

use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.

the class ApiMgtDAO method retrieveWorkflowFromInternalReference.

/**
 * Returns a workflow object for a given internal workflow reference and the workflow type.
 *
 * @param workflowReference
 * @param workflowType
 * @return
 * @throws APIManagementException
 */
public WorkflowDTO retrieveWorkflowFromInternalReference(String workflowReference, String workflowType) throws APIManagementException {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    WorkflowDTO workflowDTO = null;
    String query = SQLConstants.GET_ALL_WORKFLOW_ENTRY_FROM_INTERNAL_REF_SQL;
    try {
        connection = APIMgtDBUtil.getConnection();
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, workflowReference);
        prepStmt.setString(2, workflowType);
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            workflowDTO = WorkflowExecutorFactory.getInstance().createWorkflowDTO(rs.getString("WF_TYPE"));
            workflowDTO.setStatus(WorkflowStatus.valueOf(rs.getString("WF_STATUS")));
            workflowDTO.setExternalWorkflowReference(rs.getString("WF_EXTERNAL_REFERENCE"));
            workflowDTO.setCreatedTime(rs.getTimestamp("WF_CREATED_TIME").getTime());
            workflowDTO.setWorkflowReference(rs.getString("WF_REFERENCE"));
            workflowDTO.setTenantDomain(rs.getString("TENANT_DOMAIN"));
            workflowDTO.setTenantId(rs.getInt("TENANT_ID"));
            workflowDTO.setWorkflowDescription(rs.getString("WF_STATUS_DESC"));
        }
    } catch (SQLException e) {
        handleException("Error while retrieving workflow details for " + workflowReference, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return workflowDTO;
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with TENANT_DOMAIN

use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.

the class ApiMgtDAO method getworkflowReferenceByExternalWorkflowReferenceID.

/**
 * Get the Pending workflow Request using ExternalWorkflowReference for a particular tenant
 *
 * @param externelWorkflowRef of pending workflow request
 * @param status              workflow status of workflow pending process
 * @param tenantDomain        tenant domain of user
 * @return workflow pending request
 */
public Workflow getworkflowReferenceByExternalWorkflowReferenceID(String externelWorkflowRef, String status, String tenantDomain) throws APIManagementException {
    ResultSet rs = null;
    Workflow workflow = new Workflow();
    String sqlQuery = SQLConstants.GET_ALL_WORKFLOW_DETAILS_BY_EXTERNAL_WORKFLOW_REFERENCE;
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement prepStmt = connection.prepareStatement(sqlQuery)) {
        try {
            prepStmt.setString(1, externelWorkflowRef);
            prepStmt.setString(2, status);
            prepStmt.setString(3, tenantDomain);
            rs = prepStmt.executeQuery();
            while (rs.next()) {
                workflow.setWorkflowId(rs.getInt("WF_ID"));
                workflow.setWorkflowReference(rs.getString("WF_REFERENCE"));
                workflow.setWorkflowType(rs.getString("WF_TYPE"));
                String workflowstatus = rs.getString("WF_STATUS");
                workflow.setStatus(org.wso2.carbon.apimgt.api.WorkflowStatus.valueOf(workflowstatus));
                workflow.setCreatedTime(rs.getTimestamp("WF_CREATED_TIME").toString());
                workflow.setUpdatedTime(rs.getTimestamp("WF_UPDATED_TIME").toString());
                workflow.setWorkflowDescription(rs.getString("WF_STATUS_DESC"));
                workflow.setTenantId(rs.getInt("TENANT_ID"));
                workflow.setTenantDomain(rs.getString("TENANT_DOMAIN"));
                workflow.setExternalWorkflowReference(rs.getString("WF_EXTERNAL_REFERENCE"));
                InputStream targetStream = rs.getBinaryStream("WF_METADATA");
                InputStream propertiesTargetStream = rs.getBinaryStream("WF_PROPERTIES");
                if (targetStream != null) {
                    String metadata = APIMgtDBUtil.getStringFromInputStream(targetStream);
                    Gson metadataGson = new Gson();
                    JSONObject metadataJson = metadataGson.fromJson(metadata, JSONObject.class);
                    workflow.setMetadata(metadataJson);
                } else {
                    JSONObject metadataJson = new JSONObject();
                    workflow.setMetadata(metadataJson);
                }
                if (propertiesTargetStream != null) {
                    String properties = APIMgtDBUtil.getStringFromInputStream(propertiesTargetStream);
                    Gson propertiesGson = new Gson();
                    JSONObject propertiesJson = propertiesGson.fromJson(properties, JSONObject.class);
                    workflow.setProperties(propertiesJson);
                } else {
                    JSONObject propertiesJson = new JSONObject();
                    workflow.setProperties(propertiesJson);
                }
            }
        } catch (SQLException e) {
            handleException("Error when retriving the workflow details. ", e);
        } finally {
            APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
        }
    } catch (SQLException e) {
        handleException("Error when retriving the workflow details. ", e);
    }
    return workflow;
}
Also used : JSONObject(org.json.simple.JSONObject) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) Workflow(org.wso2.carbon.apimgt.api.model.Workflow) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement)

Example 5 with TENANT_DOMAIN

use of org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifacts.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifacts(String tenantDomain) throws APIManagementException {
    String query = SQLConstants.RETRIEVE_ARTIFACTS;
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, tenantDomain);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String apiId = resultSet.getString("API_ID");
                String label = resultSet.getString("LABEL");
                try {
                    APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
                    apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
                    apiRuntimeArtifactDto.setApiId(apiId);
                    String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
                    apiRuntimeArtifactDto.setLabel(label);
                    apiRuntimeArtifactDto.setVhost(resolvedVhost);
                    apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
                    apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
                    apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
                    apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
                    apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
                    apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
                    InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
                    if (artifact != null) {
                        byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
                        try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
                            apiRuntimeArtifactDto.setArtifact(newArtifact);
                        }
                    }
                    apiRuntimeArtifactDto.setFile(true);
                    apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
                } catch (APIManagementException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error resolving vhost while retrieving runtime artifact for API %s, " + "gateway environment \"%s\", tenant: \"%s\"." + "Skipping runtime artifact for the API.", apiId, label, tenantDomain), e);
                } catch (IOException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error occurred retrieving input stream from byte array of " + "API: %s, gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                } catch (SQLException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Failed to retrieve Gateway Artifact of API: %s, " + "gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve Gateway Artifacts.", e);
    }
    return apiRuntimeArtifactDtoList;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 Connection (java.sql.Connection)19 Test (org.testng.annotations.Test)19 Test (org.junit.Test)13 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)10 ArrayList (java.util.ArrayList)10 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)10 PreparedStatement (java.sql.PreparedStatement)9 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)7 Properties (java.util.Properties)6 Matchers.anyString (org.mockito.Matchers.anyString)6 JSONObject (org.json.simple.JSONObject)5 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)5 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)5 IOException (java.io.IOException)4 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)4