Java Code Examples for org.wso2.carbon.apimgt.api.model.API#setUriTemplates()

The following examples show how to use org.wso2.carbon.apimgt.api.model.API#setUriTemplates() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APIMgtDAOTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
 public void testAddAndConvertNullThrottlingTiers() throws APIManagementException {

     //Adding an API with a null THROTTLING_TIER should automatically convert it to Unlimited
     APIIdentifier apiIdentifier = new APIIdentifier("testAddAndGetApi", "testAddAndGetApi", "1.0.0");
     API api = new API(apiIdentifier);
     api.setContext("/testAddAndGetApi");
     api.setContextTemplate("/testAddAndGetApi/{version}");
     Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
     uriTemplates.add(getUriTemplate("/abc", "GET", "Any", "read", null));
     api.setUriTemplates(uriTemplates);
     api.setScopes(getScopes());
     api.setStatus(APIConstants.PUBLISHED);
     api.setAsDefaultVersion(true);
     int apiId = apiMgtDAO.addAPI(api, -1234);
     apiMgtDAO.addURITemplates(apiId, api, -1234);
     HashMap<String, String> result1 = apiMgtDAO.getURITemplatesPerAPIAsString(apiIdentifier);
     Assert.assertTrue(result1.containsKey("/abc::GET::Any::Unlimited::abcd defgh fff"));

     //Change the inserted throttling tier back to Null and test the convertNullThrottlingTier method
     updateThrottlingTierToNull();
     apiMgtDAO.convertNullThrottlingTiers();
     HashMap<String, String> result2 = apiMgtDAO.getURITemplatesPerAPIAsString(apiIdentifier);
     Assert.assertTrue(result2.containsKey("/abc::GET::Any::Unlimited::abcd defgh fff"));
}
 
Example 2
Source File: OASTestBase.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public void testGenerateAPIDefinition(APIDefinition parser) throws Exception {
    APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
    API api = new API(identifier);
    api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope)));
    api.setUriTemplates(new HashSet<>(Arrays.asList(petGet)));

    String definition = parser.generateAPIDefinition(new SwaggerData(api));
    APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));

    Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
    Assert.assertEquals(1, uriTemplates.size());
    Assert.assertTrue(uriTemplates.contains(petGet));

    Set<Scope> scopes = parser.getScopes(definition);
    Assert.assertEquals(2, scopes.size());
    Assert.assertTrue(scopes.contains(sampleScope));
    Assert.assertTrue(scopes.contains(extensionScope));
}
 
Example 3
Source File: APIMgtDAOTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddAndGetApi() throws Exception{
    APIIdentifier apiIdentifier = new APIIdentifier("testAddAndGetApi",
            "testAddAndGetApi", "1.0.0");
    API api = new API(apiIdentifier);
    api.setContext("/testAddAndGetApi");
    api.setContextTemplate("/testAddAndGetApi/{version}");
    api.setUriTemplates(getUriTemplateSet());
    api.setScopes(getScopes());
    api.setStatus(APIConstants.PUBLISHED);
    api.setAsDefaultVersion(true);
    int apiID = apiMgtDAO.addAPI(api, -1234);
    apiMgtDAO.addURITemplates(apiID, api, -1234);
    apiMgtDAO.updateAPI(api);
    apiMgtDAO.updateURITemplates(api, -1234);
    Set<APIStore> apiStoreSet = new HashSet<APIStore>();
    APIStore apiStore = new APIStore();
    apiStore.setDisplayName("wso2");
    apiStore.setEndpoint("http://localhost:9433/store");
    apiStore.setName("wso2");
    apiStore.setType("wso2");
    apiStoreSet.add(apiStore);
    apiMgtDAO.addExternalAPIStoresDetails(apiIdentifier,apiStoreSet);
    assertTrue(apiMgtDAO.getExternalAPIStoresDetails(apiIdentifier).size()>0);
    apiMgtDAO.deleteExternalAPIStoresDetails(apiIdentifier, apiStoreSet);
    apiMgtDAO.updateExternalAPIStoresDetails(apiIdentifier, Collections.<APIStore>emptySet());
    assertTrue(apiMgtDAO.getExternalAPIStoresDetails(apiIdentifier).size()==0);
    apiMgtDAO.deleteAPI(apiIdentifier);
}
 
Example 4
Source File: OASTestBase.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public String testGenerateAPIDefinitionWithExtension(APIDefinition parser, String content) throws Exception {
    JSONObject jsonObject = new JSONObject(content);
    String equalNoOfResourcesWithExtension = jsonObject.getJSONObject("equalNoOfResourcesWithExtension").toString();

    Scope newScope = new Scope();
    newScope.setName("newScope");
    newScope.setKey("newScope");
    newScope.setRoles("admin");
    newScope.setDescription("newScopeDescription");

    URITemplate updatedItemGet = new URITemplate();
    updatedItemGet.setUriTemplate("/items");
    updatedItemGet.setAuthType("Application & Application User");
    updatedItemGet.setAuthTypes("Application & Application User");
    updatedItemGet.setHTTPVerb("GET");
    updatedItemGet.setHttpVerbs("GET");
    updatedItemGet.setThrottlingTier("Unlimited");
    updatedItemGet.setThrottlingTiers("Unlimited");
    updatedItemGet.setScope(newScope);
    updatedItemGet.setScopes(newScope);

    APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
    API api = new API(identifier);
    api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope, newScope)));
    api.setUriTemplates(new HashSet<>(Arrays.asList(petPost, updatedItemGet)));

    String definition = parser.generateAPIDefinition(new SwaggerData(api), equalNoOfResourcesWithExtension);
    APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    return definition;
}
 
Example 5
Source File: OASTestBase.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public void testGenerateAPIDefinition2(APIDefinition parser, String content, OASParserEvaluator evaluator) throws Exception {
    JSONObject jsonObject = new JSONObject(content);
    String equalNoOfResources = jsonObject.getJSONObject("equalNoOfResources").toString();

    APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
    API api = new API(identifier);
    api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope)));
    api.setUriTemplates(new HashSet<>(Arrays.asList(petGet, petPost, itemGet, itemPost)));

    String definition = parser.generateAPIDefinition(new SwaggerData(api), equalNoOfResources);
    APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));

    Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
    Assert.assertEquals(4, uriTemplates.size());
    Assert.assertTrue(uriTemplates.contains(petGet));
    Assert.assertTrue(uriTemplates.contains(petPost));
    Assert.assertTrue(uriTemplates.contains(itemGet));
    Assert.assertTrue(uriTemplates.contains(itemPost));

    Set<Scope> scopes = parser.getScopes(definition);
    Assert.assertEquals(2, scopes.size());
    Assert.assertTrue(scopes.contains(sampleScope));
    Assert.assertTrue(scopes.contains(extensionScope));

    // Remove operation and path from API object
    String extraResourcesInDefinition = jsonObject.getJSONObject("extraResourcesInDefinition").toString();
    api.setUriTemplates(new HashSet<>(Arrays.asList(itemGet, itemPost)));
    definition = parser.generateAPIDefinition(new SwaggerData(api), extraResourcesInDefinition);
    response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
    uriTemplates = parser.getURITemplates(definition);
    Assert.assertEquals(2, uriTemplates.size());

    //assert generated paths
    if(evaluator != null) {
        evaluator.eval(definition);
    }

    Iterator iterator = uriTemplates.iterator();
    while (iterator.hasNext()) {
        URITemplate element = (URITemplate) iterator.next();
        if ("/pets".equalsIgnoreCase(element.getUriTemplate())) {
            Assert.fail("Removed paths from API operation should not present.");
        }
        if ("/items".equalsIgnoreCase(element.getUriTemplate()) && "PUT".equalsIgnoreCase(element.getHTTPVerb())) {
            Assert.fail("Removed item from API operation should not present.");
        }
    }
    Assert.assertTrue(uriTemplates.contains(itemGet));
    Assert.assertTrue(uriTemplates.contains(itemPost));

    // Add operation and path to API object
    String lessResourcesInDefinition = jsonObject.getJSONObject("lessResourcesInDefinition").toString();
    api.setUriTemplates(new HashSet<>(Arrays.asList(petGet, petPost, itemGet, itemPost)));
    definition = parser.generateAPIDefinition(new SwaggerData(api), lessResourcesInDefinition);
    response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
    uriTemplates = parser.getURITemplates(definition);
    Assert.assertEquals(4, uriTemplates.size());
    Assert.assertTrue(uriTemplates.contains(petGet));
    Assert.assertTrue(uriTemplates.contains(petPost));
    Assert.assertTrue(uriTemplates.contains(itemGet));
    Assert.assertTrue(uriTemplates.contains(itemPost));
}