Search in sources :

Example 91 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testGetRoleName.

@Test
public void testGetRoleName() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    String validRoleId = "ac093278-9343-466c-8a71-af47921a575b";
    String expectedRoleName = "engineer";
    // happy path
    String successfulResponseBody = "{\"displayName\":\"" + expectedRoleName + "\",\"meta\":{\"created\":" + "\"2017-06-26T16:30:42\",\"location\":\"https://localhost:9443/wso2/scim/Groups/" + validRoleId + "\"" + ",\"lastModified\":\"2017-06-26T16:30:42\"},\"schemas\":[\"urn:scim:schemas:core:1.0\"],\"id\":\"" + validRoleId + "\"}";
    Response successfulResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body(successfulResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.getGroup(validRoleId)).thenReturn(successfulResponse);
    try {
        String roleName = idpImpl.getRoleName(validRoleId);
        Assert.assertEquals(roleName, expectedRoleName);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
    // error path
    // When response is null
    String invalidRoleIdResponseNull = "invalidRoleId_Response_Null";
    Mockito.when(scimServiceStub.getGroup(invalidRoleIdResponseNull)).thenReturn(null);
    try {
        idpImpl.getRoleName(invalidRoleIdResponseNull);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving name of role with Id " + invalidRoleIdResponseNull + ". Error : Response is null.");
    }
    // error path
    // When the request did not return a 200 OK response
    String invalidRoleIdNot200OK = "invalidRoleId_Not_200_OK";
    String errorResponseBody = "{\"Errors\":[{\"code\":\"404\",\"description\":\"Group not found in the user " + "store.\"}]}";
    Response errorResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_404_NOT_FOUND).headers(new HashMap<>()).body(errorResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.getGroup(invalidRoleIdNot200OK)).thenReturn(errorResponse);
    try {
        idpImpl.getRoleName(invalidRoleIdNot200OK);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving name of role with Id " + invalidRoleIdNot200OK + ". Error : Group not found in the user store.");
    }
    // Error case - When response body is empty
    String invalidRoleIdResponseEmpty = "invalidRoleId_Response_Empty";
    Response emptyResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body("".getBytes()).build();
    Mockito.when(scimServiceStub.getGroup(invalidRoleIdResponseEmpty)).thenReturn(emptyResponse);
    try {
        idpImpl.getRoleName(invalidRoleIdResponseEmpty);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving role name with role Id " + invalidRoleIdResponseEmpty + " from SCIM endpoint. " + "Response body is null or empty.");
    }
}
Also used : Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Test(org.testng.annotations.Test)

Example 92 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultCustomPolicy.

public static CustomPolicy createDefaultCustomPolicy() {
    CustomPolicy customPolicy = new CustomPolicy(SAMPLE_CUSTOM_RULE);
    customPolicy.setKeyTemplate("$userId");
    String siddhiQuery = "FROM RequestStream SELECT userId, ( userId == 'admin@carbon.super' ) AS isEligible , " + "str:concat('admin@carbon.super','') as throttleKey INSERT INTO EligibilityStream;" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(1 min) SELECT throttleKey, " + "(count(userId) >= 5 as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS into " + "ResultStream;";
    customPolicy.setSiddhiQuery(siddhiQuery);
    customPolicy.setDescription("Sample custom policy");
    return customPolicy;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)

Example 93 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultSiddhiAppforAppPolicy.

public static String createDefaultSiddhiAppforAppPolicy() {
    ApplicationPolicy policy = createDefaultApplicationPolicy();
    RequestCountLimit limit = (RequestCountLimit) createDefaultApplicationPolicy().getDefaultQuotaPolicy().getLimit();
    String siddhiApp = "@App:name('application_" + policy.getPolicyName() + "')\n" + "@App:description('ExecutionPlan for app_" + policy.getPolicyName() + "')\n" + "@source(type='inMemory', topic='apim', @map(type='passThrough'))\n" + "define stream RequestStream (messageID string, appKey string, appTier string, " + "subscriptionKey string," + " apiKey string, apiTier string, subscriptionTier string, resourceKey string," + " resourceTier string," + " userId string,  apiContext string, apiVersion string, appTenant string, apiTenant string," + " appId " + "string, apiName string, propertiesMap string);\n" + "@sink(type='jms', @map(type='text'),\n" + "factory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672', destination='TEST.FOO', connection.factory." + "type='topic',\n" + "connection.factory.jndi.name='TopicConnectionFactory')\n" + "define stream GlobalThrottleStream (throttleKey string, isThrottled bool" + ", expiryTimeStamp long);\n" + "FROM RequestStream\n" + "SELECT messageID, (appTier == '" + policy.getPolicyName() + "') AS isEligible, appKey AS throttleKey, " + "propertiesMap\n" + "INSERT INTO EligibilityStream;\n" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(" + policy.getDefaultQuotaPolicy().getLimit().getUnitTime() + " " + policy.getDefaultQuotaPolicy().getLimit().getTimeUnit() + ", 0)\n" + "select throttleKey, (count(messageID) >= " + limit.getRequestCount() + ")" + " as isThrottled, expiryTimeStamp group by throttleKey\n" + "INSERT ALL EVENTS into ResultStream;\n" + "from ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)\n" + "select *\n" + "insert into GlobalThrottleStream;\n";
    return siddhiApp;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)

Example 94 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultSiddhiAppForAPILevelDefaultThrottlePolicy.

public static String createDefaultSiddhiAppForAPILevelDefaultThrottlePolicy() {
    APIPolicy apiPolicy = createDefaultAPIPolicy();
    String siddhiApp = "\n@App:name('resource_" + apiPolicy.getPolicyName() + "_default')" + "\n@App:description('ExecutionPlan for resource_" + apiPolicy.getPolicyName() + "_default')\n" + "\n@source(type='inMemory', topic='apim', @map(type='passThrough'))" + "\ndefine stream RequestStream (messageID string, appKey string," + " appTier string, subscriptionKey string," + " apiKey string, apiTier string, subscriptionTier string, resourceKey string," + " resourceTier string, userId string,  apiContext string, apiVersion string, appTenant string," + " apiTenant string," + " appId string, apiName string, propertiesMap string);\n" + "\n@sink(type='jms', @map(type='text')," + "\nfactory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672'," + " destination='TEST.FOO', connection.factory.type='topic'," + "\nconnection.factory.jndi.name='TopicConnectionFactory')" + "\ndefine stream GlobalThrottleStream (throttleKey string, isThrottled bool," + " expiryTimeStamp long);\n" + "\nFROM RequestStream" + "\nSELECT messageID, (resourceTier == 'SampleAPIPolicy' AND " + "NOT(((3232238595l<=cast(map:get(propertiesMap,'ip'),'Long')" + " AND 3232258067l>=cast(map:get(propertiesMap,'ip'),'Long')) AND " + "(cast(map:get(propertiesMap,'ip'),'Long')==2066353720l)) " + "OR ((regex:find('Chrome',cast(map:get(propertiesMap,'Browser'),'string')))" + " AND (regex:find('attributed'," + "cast(map:get(propertiesMap,'/path/path2'),'string')))" + " AND (cast(map:get(propertiesMap,'Location'),'string')=='Colombo'))))" + " AS isEligible, resourceKey AS throttleKey, propertiesMap" + "\nINSERT INTO EligibilityStream;\n" + "\nFROM EligibilityStream[isEligible==true]#throttler:timeBatch(1000 s, 0)" + "\nselect throttleKey, (count(messageID) >= 10000) as isThrottled," + " expiryTimeStamp group by throttleKey" + "\nINSERT ALL EVENTS into ResultStream;\n" + "\nfrom ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)" + "\nselect *" + "\ninsert into GlobalThrottleStream;\n";
    return siddhiApp;
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 95 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromConditionalGroupDTOToPipeline.

/**
 * Converts a single Conditional Group DTO into a Pipeline object
 *
 * @param dto Conditional Group DTO
 * @return Derived Pipeline object from Conditional Group DTO
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static Pipeline fromConditionalGroupDTOToPipeline(ConditionalGroupDTO dto) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    Pipeline pipeline = new Pipeline();
    pipeline.setDescription(dto.getDescription());
    pipeline.setEnabled(true);
    pipeline.setQuotaPolicy(fromDTOToQuotaPolicy(dto.getLimit()));
    List<Condition> conditions = fromDTOListToConditionList(dto.getConditions());
    pipeline.setConditions(conditions);
    return pipeline;
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Aggregations

Test (org.testng.annotations.Test)128 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)97 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)94 Event (org.wso2.siddhi.core.event.Event)87 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)78 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)51 Query (org.wso2.siddhi.query.api.execution.query.Query)17 ArrayList (java.util.ArrayList)16 CharonException (org.wso2.charon3.core.exceptions.CharonException)15 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)13 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)12 HashMap (java.util.HashMap)8 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)8 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)7 UserManager (org.wso2.charon3.core.extensions.UserManager)7 Group (org.wso2.charon3.core.objects.Group)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Produces (javax.ws.rs.Produces)6