String password = args[3];
InfiniteDriver infDriver = new InfiniteDriver(rootUrl);
System.out.println("LOGIN=" + infDriver.login(username, password, new ResponseObject()));
//POST
AdvancedQueryPojo query = new AdvancedQueryPojo();
QueryTermPojo qt = new QueryTermPojo();
qt.etext = "*";
query.qt = Arrays.asList(qt);
ResponseObject response = new ResponseObject();
ObjectId communityId = new ObjectId("4c927585d591d31d7b37097a");
ResponsePojo retVal = infDriver.sendQuery(query, communityId, response);
System.out.println("RETURNED " + retVal.getStats().found + " docs");
return;
}
// Aliases:
boolean bTestAliases = false;
if (bTestAliases) { // will need to set some variables...
String rootUrl = "http://infinite.ROOT.com/api/";
String apiKey = "API_KEY";
InfiniteDriver infDriver = new InfiniteDriver(rootUrl, apiKey);
String aliasCommIdStr = "4d38b72c054548f038a0414a";
String emptyAliasCommIdStr = "506afc85e4b01d98fcf9bf5f";
ResponseObject response = new ResponseObject();
Map<String, List<SharePojo>> aliasMapping = new HashMap<String, List<SharePojo>>();
Map<String, EntityFeaturePojo> aliases = infDriver.getAliases(aliasCommIdStr, aliasMapping, response);
if (response.isSuccess()) {
System.out.println("ALIASES: " + new com.google.gson.Gson().toJson(aliases));
System.out.println("ALIAS MAPPINGS: " + new com.google.gson.Gson().toJson(aliasMapping));
}
else {
System.out.println("FAIL: " + response.getMessage());
}
// Change 1 of the aliases
Iterator<EntityFeaturePojo> it = aliases.values().iterator();
EntityFeaturePojo alias = it.next();
alias.addToSemanticLinks(Arrays.asList("AlexTest"));
EntityFeaturePojo alias2 = it.next();
alias2.addAlias("XXXXX/person");
Set<String> failures = infDriver.updateAliases(Arrays.asList(alias, alias2), aliasCommIdStr, false, response);
if (!response.isSuccess()) {
System.out.println("UPDATE TOTAL FAILURE: " + response.getMessage());
}
else if (!failures.isEmpty()) {
System.out.println("UPDATE FAILURES: " + Arrays.toString(failures.toArray()));
}
else {
System.out.println("UPDATED " + alias.getIndex() + " AND " + alias2.getIndex());
}
// Check upserting (failure and success) into an existing share
// upsert fail:
String savedAlias = alias.getIndex();
alias.setIndex("testXXX/person");
if (aliases.containsKey("testXXX/person")) {
System.out.println("(skipping upsert fail, target index exists)");
}
else {
failures = infDriver.updateAliases(Arrays.asList(alias, alias2), aliasCommIdStr, false, response);
if (!response.isSuccess()) {
System.out.println("UPDATE TOTAL FAILURE (CORRECT IF ONLY 1 ALIAS SPECIFIED): " + response.getMessage() + " " + Arrays.toString(failures.toArray()));
}
else if (!failures.isEmpty()) {
System.out.println("UPSERT CORRECTLY FAILS: " + Arrays.toString(failures.toArray()));
}
else {
System.out.println("INCORRECTLY UPDATED " + alias.getIndex());
}
}
// upsert success:
failures = infDriver.updateAliases(Arrays.asList(alias), aliasCommIdStr, true, response);
if (!response.isSuccess()) {
System.out.println("UPDATE TOTAL FAILURE: " + response.getMessage());
}
else if (!failures.isEmpty()) {
System.out.println("UPDATE FAILURES: " + Arrays.toString(failures.toArray()));
}
else {
System.out.println("UPDATED " + alias.getIndex());
}
// Check upserting into a new community
failures = infDriver.updateAliases(Arrays.asList(alias), emptyAliasCommIdStr, true, response);
if (!response.isSuccess()) {
System.out.println("UPDATE TOTAL FAILURE: " + response.getMessage());
}
else if (!failures.isEmpty()) {
System.out.println("UPDATE FAILURES: " + Arrays.toString(failures.toArray()));
}
else {
System.out.println("UPDATED " + alias.getIndex());
}
// Check removal
boolean bRemoval = true;
if (bRemoval) {
failures = infDriver.removeAliases(Arrays.asList(alias.getIndex()), emptyAliasCommIdStr, response);
if (!response.isSuccess()) {
System.out.println("REMOVE TOTAL FAILURE: " + response.getMessage());
}
else if (!failures.isEmpty()) {
System.out.println("REMOVE FAILURES: " + Arrays.toString(failures.toArray()));
}
else {
System.out.println("REMOVE " + alias.getIndex());
}
failures = infDriver.removeAliases(Arrays.asList(alias.getIndex()), aliasCommIdStr, response);
if (!response.isSuccess()) {
System.out.println("REMOVE TOTAL FAILURE: " + response.getMessage());
}
else if (!failures.isEmpty()) {
System.out.println("REMOVE FAILURES: " + Arrays.toString(failures.toArray()));
}
else {
System.out.println("REMOVE " + alias.getIndex());
}
// Reset old shares back again
alias.setIndex(savedAlias);
if (null != alias.getSemanticLinks()) {
alias.getSemanticLinks().remove("AlexTest");
}
alias.getAlias().remove("XXXXX/person");
if (null != alias2.getSemanticLinks()) {
alias2.getSemanticLinks().remove("AlexTest");
}
alias2.getAlias().remove("XXXXX/person");
failures = infDriver.updateAliases(Arrays.asList(alias, alias2), aliasCommIdStr, false, response);
System.out.println("RESET: " + response.isSuccess() + " " + (2 - failures.size()));
}
}//(end alias testing)
/////////////////////////////////////////////////////////////////////////////
// API testing:
// API: Variable community source pojo...
ResponsePojo rp1 = new ResponsePojo();
SourcePojo sp = new SourcePojo();
sp.setUrl("http://test");
sp.setKey(sp.generateSourceKey());
sp.addToCommunityIds(new ObjectId("a0000000000000000000000a"));
sp.addToCommunityIds(new ObjectId("c0000000000000000000000c"));
//CHECK THIS DOESN'T COMPILE
//rp1.setData(sp); // (Not allowed SourcePojo isn't a BaseApiPojo)
////////////////////////////////////////////////
//CANONICAL EXAMPLE:
Set<ObjectId> communities = new HashSet<ObjectId>();
communities.add(new ObjectId("a0000000000000000000000a"));
rp1.setData(sp, new SourcePojoApiMap(null, communities, communities));
String sRPSingleObject = rp1.toApi();
System.out.println("RPa=" + sRPSingleObject); // ("chris" removed, toApi handles RepsonsePojo specially)
////////////////////////////////////////////////
System.out.println("RPb=" + ResponsePojo.toApi(rp1, rp1.getMapper())); // ("chris" removed because of mapper)
System.out.println("RPc=" + ResponsePojo.toApi(rp1)); // ("chris" removed, toApi handles RepsonsePojo specially)
//API: Get an non-API object
String sJson = "{ 'url':'http://test2', 'isApproved': false, 'harvestBadSource': true, 'created': 'Feb 14, 2013 9:24:34 PM' } ";
//sp = BaseApiPojo.mapFromApi(sJson, SourcePojo.class, null);
// Equivalent to:
SourcePojo sp2 = ApiManager.mapFromApi(sJson, SourcePojo.class, new SourcePojoApiMap(null, new HashSet<ObjectId>(), new HashSet<ObjectId>()));
System.out.println("RPd="+new Gson().toJson(sp2)); // "alex" and "chris" both removed
//API: add a list to the response Pojo
List<SourcePojo> list = Arrays.asList(sp, sp2);
//CHECK THIS DOESN'T COMPILE
//rp1.setData(list); // (Not allowed SourcePojo isn't a BaseApiPojo)
sp2.addToCommunityIds(new ObjectId("a0000000000000000000000a")); // (alex will be allowed again)
rp1.setData(list, new SourcePojoApiMap(null, communities, communities));
String sRPList = rp1.toApi();
sp2.setCommunityIds(null);
//API: And get as a list
String listJson = BaseApiPojo.getDefaultBuilder().create().toJson(rp1.getData());
System.out.println("RP=" + listJson); // include "alex" and "chris" - no mapping applied
////////////////////////////////////////////////
//CANONICAL EXAMPLE:
list = ApiManager.mapListFromApi(listJson, SourcePojo.listType(), null);
////////////////////////////////////////////////
System.out.println("SPL=" + BaseApiPojo.getDefaultBuilder().create().toJson(list)); // both "alex" and "chris", no mapping "from API"
Set<SourcePojo> set = ApiManager.mapListFromApi(BaseApiPojo.getDefaultBuilder().create().toJson(rp1.getData()), new TypeToken<Set<SourcePojo>>(){}, null);
System.out.println("SPS=" + BaseApiPojo.getDefaultBuilder().create().toJson(set)); // both "alex" and "chris", no mapping "from API"
// API: finally transform to a JSON list (applies mapping)
try {
System.out.println("SPJ=" + ApiManager.mapListToApi(set, new TypeToken<Set<SourcePojo>>(){}, new SourcePojoApiMap(null, null, communities)));
// should fail because one of the communities does not have
System.out.println("**********FAILED SHOULD HAVE THROWN SECURITY EXCEPTION");
}
catch (RuntimeException e) {
// Add "communities" to object with missing val
for (SourcePojo spSet: set) {
if (null == spSet.getCommunityIds()) {
spSet.setCommunityIds(communities);
}
}
// Try again:
System.out.println("SPJ=" + ApiManager.mapListToApi(set, new TypeToken<Set<SourcePojo>>(){}, new SourcePojoApiMap(null, communities, communities)));
// (just has "alex")
}
// And now in the other direction, ie deserializing....
ResponsePojo rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class, SourcePojo.class, new SourcePojoApiMap(null, communities, communities));
System.out.println("RECREATED RP_SRC=" + rpRecreated.toApi());
System.out.println("RECREATED SRC(RP_SRC)=" + ((BasicDBObject)((SourcePojo)rpRecreated.getData()).toDb()).toString());
rpRecreated = ResponsePojo.listFromApi(sRPList, ResponsePojo.class, SourcePojo.listType(), new SourcePojoApiMap(null, communities, communities));
System.out.println("RECREATED RP_LSRC=" + rpRecreated.toApi());
rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class);
System.out.println("RECREATED RAW(RP_SRC)=" + ((JsonElement)rpRecreated.getData()));
sp = ApiManager.mapFromApi((JsonElement)rpRecreated.getData(), SourcePojo.class, new SourcePojoApiMap(null, communities, communities));
System.out.println("RECREATED SRC(RAW(RP_SRC))=" + ((JsonElement)rpRecreated.getData()));
// Real-life source pojo testing:
// No longer needed - if commented in will fail because communities not assigned in mapTo/FromApi
// BasicDBObject srcQuery = new BasicDBObject("useExtractor", "ModusOperandi");
// SourcePojo fileSource = SourcePojo.fromDb(DbManager.getConfig().getSource().findOne(srcQuery), SourcePojo.class);
// System.out.println("MODUS=" + ApiManager.mapToApi(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>())));
// ResponsePojo testRP = new ResponsePojo();
// testRP.setData(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>()));
// System.out.println("MODUS2=" + testRP.toApi());
// srcQuery = new BasicDBObject("useExtractor", "none");
// fileSource = SourcePojo.fromDb(DbManager.getConfig().getSource().findOne(srcQuery), SourcePojo.class);
// System.out.println("SAH=" + ApiManager.mapToApi(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>())));
// testRP.setData(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>()));
// System.out.println("SAH2=" + testRP.toApi());
//API: Get an API object
////////////////////////////////////////////////
//CANONICAL EXAMPLE:
String sQueryJson = "{ 'logic': 'alex' }";
AdvancedQueryPojo aqp = AdvancedQueryPojo.fromApi(sQueryJson, AdvancedQueryPojo.class);
//AdvancedQueryPojo aqp = AdvancedQueryPojo.fromApi(sQueryJson, new TypeToken<AdvancedQueryPojo>(){});
//^^^ (equivalent, needed for types that are generics)
////////////////////////////////////////////////
System.out.println("AQP="+new Gson().toJson(aqp));
// Testing ResponsePojo code on serialization
rp1.setData(aqp);
sRPSingleObject = rp1.toApi();
rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class, AdvancedQueryPojo.class);
System.out.println("RECREATED RP_AQP=" + rpRecreated.toApi());
System.out.println("RECREATED AQP(RP_AQP)=" + ((AdvancedQueryPojo)(rpRecreated.getData())).toApi());
//API: Get a list of objects
sQueryJson = "{ 'logic': 'chris' }";
AdvancedQueryPojo aqp2 = AdvancedQueryPojo.fromApi(sQueryJson, AdvancedQueryPojo.class);
List<AdvancedQueryPojo> list2 = Arrays.asList(aqp, aqp2);
rp1.setData(list2);
sRPList = rp1.toApi();
System.out.println("RP=" + sRPList);
// This won't compile because didn't write a listType() for AQP (since it's never actually used in this way)