Search in sources :

Example 21 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testSearchAPIs.

@Test
public void testSearchAPIs() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> userRoles = new HashSet<>(Arrays.asList(CUSTOMER_ROLE, MANAGER_ROLE, EMPLOYEE_ROLE));
    // Sample API names
    final String mixedCaseString = "Mixed Case";
    final String lowerCaseString = "lower case";
    final String upperCaseString = "UPPER CASE";
    final String charSymbolNumString = "mi ##symbol 12num";
    final String symbolSpaceString = "_under & Score_";
    // Search string cases
    final String commonMixedCaseSearchString = "CaSe";
    final String commonLowerCaseSearchString = "case";
    final String commonUpperCaseSearchString = "CASE";
    final String symbolSearchString = "##symbol";
    // In some databases numbers are not used in indexing
    final String numberSearchString = "12n";
    // API Provider, the person who owns the API
    final String provider = "John";
    // Create test data
    Map<String, API> apis = new HashMap<>();
    apis.put(mixedCaseString, SampleTestObjectCreator.createUniqueAPI().name(mixedCaseString).provider(provider).build());
    apis.put(lowerCaseString, SampleTestObjectCreator.createUniqueAPI().name(lowerCaseString).provider(provider).build());
    apis.put(upperCaseString, SampleTestObjectCreator.createUniqueAPI().name(upperCaseString).provider(provider).build());
    apis.put(charSymbolNumString, SampleTestObjectCreator.createUniqueAPI().name(charSymbolNumString).provider(provider).build());
    apis.put(symbolSpaceString, SampleTestObjectCreator.createUniqueAPI().name(symbolSpaceString).provider(provider).build());
    // Add APIs
    testAddGetEndpoint();
    for (Map.Entry<String, API> entry : apis.entrySet()) {
        API api = entry.getValue();
        apiDAO.addAPI(api);
        // Replace with summary object for validation
        apis.put(entry.getKey(), SampleTestObjectCreator.getSummaryFromAPI(api));
    }
    // Sleep for indexing
    Thread.sleep(5000);
    // Expected common string formatApiSearch result
    List<API> commonStringResult = new ArrayList<>();
    commonStringResult.add(apis.get(mixedCaseString));
    commonStringResult.add(apis.get(lowerCaseString));
    commonStringResult.add(apis.get(upperCaseString));
    // Search by common mixed case
    List<API> apiList = apiDAO.searchAPIs(new HashSet<>(), provider, commonMixedCaseSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 3);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, commonStringResult, new APIComparator()), TestUtil.printListDiff(apiList, commonStringResult));
    // Search by common lower case
    apiList = apiDAO.searchAPIs(userRoles, provider, commonLowerCaseSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 3);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, commonStringResult, new APIComparator()), TestUtil.printListDiff(apiList, commonStringResult));
    // Search by common upper case
    apiList = apiDAO.searchAPIs(userRoles, provider, commonUpperCaseSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 3);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, commonStringResult, new APIComparator()), TestUtil.printListDiff(apiList, commonStringResult));
    // Search by symbol
    apiList = apiDAO.searchAPIs(userRoles, provider, symbolSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 1);
    API actualAPI = apiList.get(0);
    API expectedAPI = apis.get(charSymbolNumString);
    Assert.assertEquals(actualAPI, expectedAPI, TestUtil.printDiff(actualAPI, expectedAPI));
    // Search by number
    apiList = apiDAO.searchAPIs(userRoles, provider, numberSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 1);
    actualAPI = apiList.get(0);
    expectedAPI = apis.get(charSymbolNumString);
    Assert.assertEquals(actualAPI, expectedAPI, TestUtil.printDiff(actualAPI, expectedAPI));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 22 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsWithUserRoles.

@Test(description = "Tests getting the APIs when the user has roles assigned")
public void testGetAPIsWithUserRoles() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    rolesOfUser.add(SampleTestObjectCreator.ADMIN_ROLE_ID);
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    Assert.assertTrue(apiList.isEmpty());
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    builder = SampleTestObjectCreator.createAlternativeAPI();
    API api2 = builder.build();
    apiDAO.addAPI(api2);
    apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    List<API> expectedAPIs = new ArrayList<>();
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api2));
    Assert.assertTrue(apiList.size() == 2);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
Also used : ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 23 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetDocumentInlineContent.

@Test(description = "Getting document inline content for an API")
public void testGetDocumentInlineContent() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    testAddGetEndpoint();
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    apiDAO.addAPI(api);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo();
    apiDAO.addDocumentInfo(api.getId(), documentInfo);
    String inlineDocContent = SampleTestObjectCreator.createDefaultInlineDocumentationContent();
    apiDAO.addDocumentInlineContent(documentInfo.getId(), inlineDocContent, ADMIN);
    String inlineDocContentFromDB = apiDAO.getDocumentInlineContent(documentInfo.getId());
    Assert.assertEquals(inlineDocContent, inlineDocContentFromDB);
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 24 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetDocumentContent.

@Test(description = "Getting document content for an API")
public void testGetDocumentContent() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    testAddGetEndpoint();
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    apiDAO.addAPI(api);
    DocumentInfo documentInfo1 = SampleTestObjectCreator.createDefaultDocumentationInfo();
    apiDAO.addDocumentInfo(api.getId(), documentInfo1);
    List<DocumentInfo> documentInfoList = new ArrayList<>();
    documentInfoList.add(documentInfo1);
    List<DocumentInfo> documentInfoListFromDB = apiDAO.getDocumentsInfoList(api.getId());
    Assert.assertTrue(documentInfoListFromDB.containsAll(documentInfoList));
    DocumentInfo documentInfo = SampleTestObjectCreator.createFileDocumentationInfo();
    apiDAO.addDocumentInfo(api.getId(), documentInfo);
    byte[] contentBytes = SampleTestObjectCreator.createDefaultFileDocumentationContent();
    apiDAO.addDocumentFileContent(documentInfo.getId(), new ByteArrayInputStream(contentBytes), "application/pdf", ADMIN);
    byte[] retrievedContentFromDB = IOUtils.toByteArray(apiDAO.getDocumentFileContent(documentInfo.getId()));
    Assert.assertEquals(contentBytes.length, retrievedContentFromDB.length);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 25 with ApiDAO

use of org.wso2.carbon.apimgt.core.dao.ApiDAO in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testAddAPIWithoutAddingLabels.

@Test
public void testAddAPIWithoutAddingLabels() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Label label1 = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    Label label2 = SampleTestObjectCreator.createLabel("private", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    List<String> labelIds = new ArrayList<>();
    labelIds.add(label1.getId());
    labelIds.add(label2.getId());
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    API apiFromDB = apiDAO.getAPI(api.getId());
    Assert.assertNotNull(apiFromDB);
    Assert.assertEquals(apiFromDB.getLabels().size(), 2);
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)315 Test (org.testng.annotations.Test)305 API (org.wso2.carbon.apimgt.core.models.API)210 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)108 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)102 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)65 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)62 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)59 BeforeTest (org.testng.annotations.BeforeTest)50 ArrayList (java.util.ArrayList)48 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)47 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)46 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)45 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)43 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)36 HashMap (java.util.HashMap)31 SQLException (java.sql.SQLException)30 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)30 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)27