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));
}
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));
}
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);
}
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);
}
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);
}
Aggregations