use of org.wso2.siddhi.query.api.execution.query.input.store.Store in project carbon-apimgt by wso2.
the class APIManagerFactory method getAPIConsumer.
/**
* Get API Store object for a particular user
*
* @param username The username of user who's requesting the objecusernamet
* @return APIStore object
* @throws APIManagementException if error occurred while initializing API Store
*/
public APIStore getAPIConsumer(String username) throws APIManagementException {
APIStore consumer = consumers.get(username);
if (consumer == null) {
synchronized (username.intern()) {
consumer = consumers.get(username);
if (consumer != null) {
return consumer;
}
consumer = newConsumer(username);
consumers.put(username, consumer);
}
}
return consumer;
}
use of org.wso2.siddhi.query.api.execution.query.input.store.Store in project carbon-apimgt by wso2.
the class PostgresSQLStatements method prepareAttributeSearchStatementForStore.
/**
* @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
* Map, int, int)
*/
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
StringBuilder roleListBuilder = new StringBuilder();
roleListBuilder.append("?");
for (int i = 0; i < roles.size() - 1; i++) {
roleListBuilder.append(",?");
}
StringBuilder searchQuery = new StringBuilder();
Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
searchQuery.append("LOWER(");
if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
} else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
} else {
searchQuery.append(entry.getKey());
}
searchQuery.append(") LIKE ?");
if (entries.hasNext()) {
searchQuery.append(" AND ");
}
}
// retrieve the attribute applicable for the search
String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
// get the corresponding implementation based on the attribute to be searched
String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
query = "Select * from ( " + query + " ) A " + getStoreAPIsByLabelJoinQuery(labels);
try {
int queryIndex = 1;
PreparedStatement statement = connection.prepareStatement(query);
// include the attribute in the query (for APIs with public visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
// include user roles in the query
for (String role : roles) {
statement.setString(queryIndex, role);
queryIndex++;
}
// include the attribute in the query (for APIs with restricted visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
for (String label : labels) {
statement.setString(queryIndex, label);
queryIndex++;
}
// setting 0 as the default offset based on store-api.yaml and Postgress specifications
statement.setInt(queryIndex, (offset < 0) ? 0 : offset);
statement.setInt(++queryIndex, limit);
return statement;
} catch (SQLException e) {
String errorMsg = "Error occurred while searching APIs for attributes in the database.";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.siddhi.query.api.execution.query.input.store.Store in project carbon-apimgt by wso2.
the class LabelsApiServiceImplTest method testLabelsGetWithoutLabelId.
@Test
public void testLabelsGetWithoutLabelId() throws Exception {
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
List<Label> labels = new ArrayList<>();
Label label1 = new Label.Builder().id("1").name("label1").type("GATEWAY").build();
Label label2 = new Label.Builder().id("2").name("label2").type("STORE").build();
labels.add(label1);
labels.add(label2);
LabelsApiServiceImpl labelService = new LabelsApiServiceImpl();
Mockito.when(labelService.labelsGet(null, null, getRequest())).thenReturn(Response.status(Response.Status.OK).entity(LabelMappingUtil.fromLabelArrayToListDTO(labels)).build());
Response response = labelService.labelsGet(null, null, getRequest());
Assert.assertEquals(response.getEntity(), LabelMappingUtil.fromLabelArrayToListDTO(labels));
}
use of org.wso2.siddhi.query.api.execution.query.input.store.Store in project carbon-apimgt by wso2.
the class ApiDAOImplIT method compareResults.
/**
* Compare the results of attribute search in store
*
* @param userRoles List of the roles of the user.
* @param attributeMap Map containing the attributes to be searched
* @param expectedAPINames List of expected APIs.
* @return true if returned API list has all expected APIs, false otherwise
* @throws APIMgtDAOException if error occurs while accessing data layer
*/
private boolean compareResults(List<String> userRoles, List<String> labels, Map<String, String> attributeMap, String[] expectedAPINames) throws APIMgtDAOException {
ApiDAO apiDAO = DAOFactory.getApiDAO();
List<API> apiList = apiDAO.searchAPIsByAttributeInStore(userRoles, labels, attributeMap, 10, 0);
List<String> resultAPINameList = new ArrayList<>();
for (API api : apiList) {
resultAPINameList.add(api.getName());
}
List<String> expectedAPINameList = Arrays.asList(expectedAPINames);
// check if returned API list has all expected APIs
return resultAPINameList.containsAll(expectedAPINameList) && expectedAPINameList.containsAll(resultAPINameList);
}
use of org.wso2.siddhi.query.api.execution.query.input.store.Store in project carbon-apimgt by wso2.
the class ApiDAOImplIT method createAPIsAndGetIDsOfAddedAPIs.
/**
* This method creates few APIs and returns the ID list of those APIs
*
* @return the ID list of added APIs
* @throws APIMgtDAOException if it fails to creates APIs
*/
private List<String> createAPIsAndGetIDsOfAddedAPIs() throws APIMgtDAOException {
Set<String> visibleRoles = new HashSet<>();
Set<String> apiTags = new HashSet<>();
List<String> apiIDList = new ArrayList<>();
ApiDAO apiDAO = DAOFactory.getApiDAO();
Map<String, UriTemplate> uriTemplateMap;
// Construct an API which has public visibility
apiTags.add("Car");
apiTags.add("Van");
uriTemplateMap = getUriTemplateMap(new String[] { "/toyota", "/nissan" });
addAPIWithGivenData("PublicAPI", "1.2.3", "PublicContext", "Paul", API.Visibility.PUBLIC, null, APIStatus.CREATED.getStatus(), "This is a public API, visible to all.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
visibleRoles.clear();
apiTags.clear();
uriTemplateMap.clear();
apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Paul").get(0).getId());
// Construct an API which is visible to manager role only
apiTags.add("Pizza");
apiTags.add("Cake");
uriTemplateMap = getUriTemplateMap(new String[] { "/pizzahut", "/dominos" });
visibleRoles.add(MANAGER_ROLE);
addAPIWithGivenData("ManagerOnlyAPI", "2.3.4", "managerContext", "Mark", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "Users with manager role can view this API.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
visibleRoles.clear();
apiTags.clear();
uriTemplateMap.clear();
apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Mark").get(0).getId());
// Construct an API which is visible to admin and manager roles
apiTags.add("Java");
uriTemplateMap = getUriTemplateMap(new String[] { "/desktop", "/laptop", "nikoncam" });
visibleRoles.add(ADMIN);
visibleRoles.add(MANAGER_ROLE);
addAPIWithGivenData("AdminManagerAPI", "3.4.5", "adminManager", "Alex", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "Admin and manager can see this API.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
visibleRoles.clear();
apiTags.clear();
uriTemplateMap.clear();
apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Alex").get(0).getId());
// Construct an API in created state, this should not be shown in store
apiTags.add("Movie");
apiTags.add("TV");
uriTemplateMap = getUriTemplateMap(new String[] { "/cnn", "/bbc" });
addAPIWithGivenData("CreatedStateAPI", "4.5.6", "createdContext", "Colin", API.Visibility.PUBLIC, null, APIStatus.CREATED.getStatus(), "This API is in created state. Should not be shown in store.", apiTags, uriTemplateMap, APIStatus.CREATED.getStatus());
visibleRoles.clear();
apiTags.clear();
uriTemplateMap.clear();
apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Colin").get(0).getId());
// Construct an API which is visible to employee role only
apiTags.add("Salary");
apiTags.add("Bonus");
uriTemplateMap = getUriTemplateMap(new String[] { "/cash", "/cheque" });
visibleRoles.add(EMPLOYEE_ROLE);
addAPIWithGivenData("EmployeeAPI", "5.6.7", "employeeCtx", "Emma", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "API for Employees.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
visibleRoles.clear();
apiTags.clear();
uriTemplateMap.clear();
apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Emma").get(0).getId());
// Construct an API which is visible to all roles, except admin role
apiTags.add("Science");
apiTags.add("Technology");
uriTemplateMap = getUriTemplateMap(new String[] { "/velocity", "/distance" });
visibleRoles.add(EMPLOYEE_ROLE);
visibleRoles.add(MANAGER_ROLE);
visibleRoles.add(CUSTOMER_ROLE);
addAPIWithGivenData("NonAdminAPI", "6.7.8", "nonAdmin", "Nancy", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "This API should be visible to all roles, except admin role.", apiTags, uriTemplateMap, APIStatus.PROTOTYPED.getStatus());
visibleRoles.clear();
apiTags.clear();
uriTemplateMap.clear();
apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Nancy").get(0).getId());
return apiIDList;
}
Aggregations