PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/infinit.e.data_model/src/com/ikanow/infinit/e/data_model/test/TestCode.java

https://github.com/IKANOW/Infinit.e
Java | 508 lines | 320 code | 59 blank | 129 comment | 40 complexity | cfe6a2a3247b63c983007ad4c38a88c8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright 2012 The Infinit.e Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. package com.ikanow.infinit.e.data_model.test;
  17. import java.io.IOException;
  18. import java.net.UnknownHostException;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.bson.types.ObjectId;
  27. import org.elasticsearch.action.bulk.BulkResponse;
  28. import org.elasticsearch.common.settings.ImmutableSettings;
  29. import com.google.gson.Gson;
  30. import com.google.gson.GsonBuilder;
  31. import com.google.gson.JsonElement;
  32. import com.google.gson.reflect.TypeToken;
  33. import com.ikanow.infinit.e.data_model.Globals;
  34. import com.ikanow.infinit.e.data_model.Globals.Identity;
  35. import com.ikanow.infinit.e.data_model.store.BaseDbPojo;
  36. import com.ikanow.infinit.e.data_model.store.DbManager;
  37. import com.ikanow.infinit.e.data_model.store.social.community.CommunityPojo;
  38. import com.ikanow.infinit.e.data_model.store.social.sharing.SharePojo;
  39. import com.ikanow.infinit.e.data_model.api.ApiManager;
  40. import com.ikanow.infinit.e.data_model.api.BaseApiPojo;
  41. import com.ikanow.infinit.e.data_model.api.BasePojoApiMap;
  42. import com.ikanow.infinit.e.data_model.api.ResponsePojo;
  43. import com.ikanow.infinit.e.data_model.api.ResponsePojo.ResponseObject;
  44. import com.ikanow.infinit.e.data_model.api.config.SourcePojoApiMap;
  45. import com.ikanow.infinit.e.data_model.api.knowledge.AdvancedQueryPojo;
  46. import com.ikanow.infinit.e.data_model.api.knowledge.AdvancedQueryPojo.QueryTermPojo;
  47. import com.ikanow.infinit.e.data_model.api.knowledge.DocumentPojoApiMap;
  48. import com.ikanow.infinit.e.data_model.api.social.sharing.SharePojoApiMap;
  49. import com.ikanow.infinit.e.data_model.driver.InfiniteDriver;
  50. import com.ikanow.infinit.e.data_model.index.ElasticSearchManager;
  51. import com.ikanow.infinit.e.data_model.index.IndexManager;
  52. import com.ikanow.infinit.e.data_model.index.document.DocumentPojoIndexMap;
  53. import com.ikanow.infinit.e.data_model.store.config.source.SourcePojo;
  54. import com.ikanow.infinit.e.data_model.store.document.DocumentPojo;
  55. import com.ikanow.infinit.e.data_model.store.document.EntityPojo;
  56. import com.ikanow.infinit.e.data_model.store.feature.entity.EntityFeaturePojo;
  57. import com.mongodb.BasicDBList;
  58. import com.mongodb.BasicDBObject;
  59. import com.mongodb.DBCollection;
  60. import com.mongodb.MongoException;
  61. public class TestCode {
  62. /**
  63. * @param args
  64. * @throws IOException
  65. * @throws MongoException
  66. * @throws UnknownHostException
  67. */
  68. public static void main(String[] args) throws UnknownHostException, MongoException, IOException {
  69. System.out.println(Arrays.toString(args));
  70. Globals.setIdentity(Identity.IDENTITY_SERVICE);
  71. Globals.overrideConfigLocation(args[0]);
  72. /////////////////////////////////////////////////////////////////////////////
  73. // SOME DRIVER TESTING
  74. // Redirects
  75. boolean testRedirects = false;
  76. if (testRedirects) {
  77. // GET
  78. String rootUrl = args[1];
  79. String username = args[2];
  80. String password = args[3];
  81. InfiniteDriver infDriver = new InfiniteDriver(rootUrl);
  82. System.out.println("LOGIN=" + infDriver.login(username, password, new ResponseObject()));
  83. //POST
  84. AdvancedQueryPojo query = new AdvancedQueryPojo();
  85. QueryTermPojo qt = new QueryTermPojo();
  86. qt.etext = "*";
  87. query.qt = Arrays.asList(qt);
  88. ResponseObject response = new ResponseObject();
  89. ObjectId communityId = new ObjectId("4c927585d591d31d7b37097a");
  90. ResponsePojo retVal = infDriver.sendQuery(query, communityId, response);
  91. System.out.println("RETURNED " + retVal.getStats().found + " docs");
  92. return;
  93. }
  94. // Aliases:
  95. boolean bTestAliases = false;
  96. if (bTestAliases) { // will need to set some variables...
  97. String rootUrl = "http://infinite.ROOT.com/api/";
  98. String apiKey = "API_KEY";
  99. InfiniteDriver infDriver = new InfiniteDriver(rootUrl, apiKey);
  100. String aliasCommIdStr = "4d38b72c054548f038a0414a";
  101. String emptyAliasCommIdStr = "506afc85e4b01d98fcf9bf5f";
  102. ResponseObject response = new ResponseObject();
  103. Map<String, List<SharePojo>> aliasMapping = new HashMap<String, List<SharePojo>>();
  104. Map<String, EntityFeaturePojo> aliases = infDriver.getAliases(aliasCommIdStr, aliasMapping, response);
  105. if (response.isSuccess()) {
  106. System.out.println("ALIASES: " + new com.google.gson.Gson().toJson(aliases));
  107. System.out.println("ALIAS MAPPINGS: " + new com.google.gson.Gson().toJson(aliasMapping));
  108. }
  109. else {
  110. System.out.println("FAIL: " + response.getMessage());
  111. }
  112. // Change 1 of the aliases
  113. Iterator<EntityFeaturePojo> it = aliases.values().iterator();
  114. EntityFeaturePojo alias = it.next();
  115. alias.addToSemanticLinks(Arrays.asList("AlexTest"));
  116. EntityFeaturePojo alias2 = it.next();
  117. alias2.addAlias("XXXXX/person");
  118. Set<String> failures = infDriver.updateAliases(Arrays.asList(alias, alias2), aliasCommIdStr, false, response);
  119. if (!response.isSuccess()) {
  120. System.out.println("UPDATE TOTAL FAILURE: " + response.getMessage());
  121. }
  122. else if (!failures.isEmpty()) {
  123. System.out.println("UPDATE FAILURES: " + Arrays.toString(failures.toArray()));
  124. }
  125. else {
  126. System.out.println("UPDATED " + alias.getIndex() + " AND " + alias2.getIndex());
  127. }
  128. // Check upserting (failure and success) into an existing share
  129. // upsert fail:
  130. String savedAlias = alias.getIndex();
  131. alias.setIndex("testXXX/person");
  132. if (aliases.containsKey("testXXX/person")) {
  133. System.out.println("(skipping upsert fail, target index exists)");
  134. }
  135. else {
  136. failures = infDriver.updateAliases(Arrays.asList(alias, alias2), aliasCommIdStr, false, response);
  137. if (!response.isSuccess()) {
  138. System.out.println("UPDATE TOTAL FAILURE (CORRECT IF ONLY 1 ALIAS SPECIFIED): " + response.getMessage() + " " + Arrays.toString(failures.toArray()));
  139. }
  140. else if (!failures.isEmpty()) {
  141. System.out.println("UPSERT CORRECTLY FAILS: " + Arrays.toString(failures.toArray()));
  142. }
  143. else {
  144. System.out.println("INCORRECTLY UPDATED " + alias.getIndex());
  145. }
  146. }
  147. // upsert success:
  148. failures = infDriver.updateAliases(Arrays.asList(alias), aliasCommIdStr, true, response);
  149. if (!response.isSuccess()) {
  150. System.out.println("UPDATE TOTAL FAILURE: " + response.getMessage());
  151. }
  152. else if (!failures.isEmpty()) {
  153. System.out.println("UPDATE FAILURES: " + Arrays.toString(failures.toArray()));
  154. }
  155. else {
  156. System.out.println("UPDATED " + alias.getIndex());
  157. }
  158. // Check upserting into a new community
  159. failures = infDriver.updateAliases(Arrays.asList(alias), emptyAliasCommIdStr, true, response);
  160. if (!response.isSuccess()) {
  161. System.out.println("UPDATE TOTAL FAILURE: " + response.getMessage());
  162. }
  163. else if (!failures.isEmpty()) {
  164. System.out.println("UPDATE FAILURES: " + Arrays.toString(failures.toArray()));
  165. }
  166. else {
  167. System.out.println("UPDATED " + alias.getIndex());
  168. }
  169. // Check removal
  170. boolean bRemoval = true;
  171. if (bRemoval) {
  172. failures = infDriver.removeAliases(Arrays.asList(alias.getIndex()), emptyAliasCommIdStr, response);
  173. if (!response.isSuccess()) {
  174. System.out.println("REMOVE TOTAL FAILURE: " + response.getMessage());
  175. }
  176. else if (!failures.isEmpty()) {
  177. System.out.println("REMOVE FAILURES: " + Arrays.toString(failures.toArray()));
  178. }
  179. else {
  180. System.out.println("REMOVE " + alias.getIndex());
  181. }
  182. failures = infDriver.removeAliases(Arrays.asList(alias.getIndex()), aliasCommIdStr, response);
  183. if (!response.isSuccess()) {
  184. System.out.println("REMOVE TOTAL FAILURE: " + response.getMessage());
  185. }
  186. else if (!failures.isEmpty()) {
  187. System.out.println("REMOVE FAILURES: " + Arrays.toString(failures.toArray()));
  188. }
  189. else {
  190. System.out.println("REMOVE " + alias.getIndex());
  191. }
  192. // Reset old shares back again
  193. alias.setIndex(savedAlias);
  194. if (null != alias.getSemanticLinks()) {
  195. alias.getSemanticLinks().remove("AlexTest");
  196. }
  197. alias.getAlias().remove("XXXXX/person");
  198. if (null != alias2.getSemanticLinks()) {
  199. alias2.getSemanticLinks().remove("AlexTest");
  200. }
  201. alias2.getAlias().remove("XXXXX/person");
  202. failures = infDriver.updateAliases(Arrays.asList(alias, alias2), aliasCommIdStr, false, response);
  203. System.out.println("RESET: " + response.isSuccess() + " " + (2 - failures.size()));
  204. }
  205. }//(end alias testing)
  206. /////////////////////////////////////////////////////////////////////////////
  207. // API testing:
  208. // API: Variable community source pojo...
  209. ResponsePojo rp1 = new ResponsePojo();
  210. SourcePojo sp = new SourcePojo();
  211. sp.setUrl("http://test");
  212. sp.setKey(sp.generateSourceKey());
  213. sp.addToCommunityIds(new ObjectId("a0000000000000000000000a"));
  214. sp.addToCommunityIds(new ObjectId("c0000000000000000000000c"));
  215. //CHECK THIS DOESN'T COMPILE
  216. //rp1.setData(sp); // (Not allowed SourcePojo isn't a BaseApiPojo)
  217. ////////////////////////////////////////////////
  218. //CANONICAL EXAMPLE:
  219. Set<ObjectId> communities = new HashSet<ObjectId>();
  220. communities.add(new ObjectId("a0000000000000000000000a"));
  221. rp1.setData(sp, new SourcePojoApiMap(null, communities, communities));
  222. String sRPSingleObject = rp1.toApi();
  223. System.out.println("RPa=" + sRPSingleObject); // ("chris" removed, toApi handles RepsonsePojo specially)
  224. ////////////////////////////////////////////////
  225. System.out.println("RPb=" + ResponsePojo.toApi(rp1, rp1.getMapper())); // ("chris" removed because of mapper)
  226. System.out.println("RPc=" + ResponsePojo.toApi(rp1)); // ("chris" removed, toApi handles RepsonsePojo specially)
  227. //API: Get an non-API object
  228. String sJson = "{ 'url':'http://test2', 'isApproved': false, 'harvestBadSource': true, 'created': 'Feb 14, 2013 9:24:34 PM' } ";
  229. //sp = BaseApiPojo.mapFromApi(sJson, SourcePojo.class, null);
  230. // Equivalent to:
  231. SourcePojo sp2 = ApiManager.mapFromApi(sJson, SourcePojo.class, new SourcePojoApiMap(null, new HashSet<ObjectId>(), new HashSet<ObjectId>()));
  232. System.out.println("RPd="+new Gson().toJson(sp2)); // "alex" and "chris" both removed
  233. //API: add a list to the response Pojo
  234. List<SourcePojo> list = Arrays.asList(sp, sp2);
  235. //CHECK THIS DOESN'T COMPILE
  236. //rp1.setData(list); // (Not allowed SourcePojo isn't a BaseApiPojo)
  237. sp2.addToCommunityIds(new ObjectId("a0000000000000000000000a")); // (alex will be allowed again)
  238. rp1.setData(list, new SourcePojoApiMap(null, communities, communities));
  239. String sRPList = rp1.toApi();
  240. sp2.setCommunityIds(null);
  241. //API: And get as a list
  242. String listJson = BaseApiPojo.getDefaultBuilder().create().toJson(rp1.getData());
  243. System.out.println("RP=" + listJson); // include "alex" and "chris" - no mapping applied
  244. ////////////////////////////////////////////////
  245. //CANONICAL EXAMPLE:
  246. list = ApiManager.mapListFromApi(listJson, SourcePojo.listType(), null);
  247. ////////////////////////////////////////////////
  248. System.out.println("SPL=" + BaseApiPojo.getDefaultBuilder().create().toJson(list)); // both "alex" and "chris", no mapping "from API"
  249. Set<SourcePojo> set = ApiManager.mapListFromApi(BaseApiPojo.getDefaultBuilder().create().toJson(rp1.getData()), new TypeToken<Set<SourcePojo>>(){}, null);
  250. System.out.println("SPS=" + BaseApiPojo.getDefaultBuilder().create().toJson(set)); // both "alex" and "chris", no mapping "from API"
  251. // API: finally transform to a JSON list (applies mapping)
  252. try {
  253. System.out.println("SPJ=" + ApiManager.mapListToApi(set, new TypeToken<Set<SourcePojo>>(){}, new SourcePojoApiMap(null, null, communities)));
  254. // should fail because one of the communities does not have
  255. System.out.println("**********FAILED SHOULD HAVE THROWN SECURITY EXCEPTION");
  256. }
  257. catch (RuntimeException e) {
  258. // Add "communities" to object with missing val
  259. for (SourcePojo spSet: set) {
  260. if (null == spSet.getCommunityIds()) {
  261. spSet.setCommunityIds(communities);
  262. }
  263. }
  264. // Try again:
  265. System.out.println("SPJ=" + ApiManager.mapListToApi(set, new TypeToken<Set<SourcePojo>>(){}, new SourcePojoApiMap(null, communities, communities)));
  266. // (just has "alex")
  267. }
  268. // And now in the other direction, ie deserializing....
  269. ResponsePojo rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class, SourcePojo.class, new SourcePojoApiMap(null, communities, communities));
  270. System.out.println("RECREATED RP_SRC=" + rpRecreated.toApi());
  271. System.out.println("RECREATED SRC(RP_SRC)=" + ((BasicDBObject)((SourcePojo)rpRecreated.getData()).toDb()).toString());
  272. rpRecreated = ResponsePojo.listFromApi(sRPList, ResponsePojo.class, SourcePojo.listType(), new SourcePojoApiMap(null, communities, communities));
  273. System.out.println("RECREATED RP_LSRC=" + rpRecreated.toApi());
  274. rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class);
  275. System.out.println("RECREATED RAW(RP_SRC)=" + ((JsonElement)rpRecreated.getData()));
  276. sp = ApiManager.mapFromApi((JsonElement)rpRecreated.getData(), SourcePojo.class, new SourcePojoApiMap(null, communities, communities));
  277. System.out.println("RECREATED SRC(RAW(RP_SRC))=" + ((JsonElement)rpRecreated.getData()));
  278. // Real-life source pojo testing:
  279. // No longer needed - if commented in will fail because communities not assigned in mapTo/FromApi
  280. // BasicDBObject srcQuery = new BasicDBObject("useExtractor", "ModusOperandi");
  281. // SourcePojo fileSource = SourcePojo.fromDb(DbManager.getConfig().getSource().findOne(srcQuery), SourcePojo.class);
  282. // System.out.println("MODUS=" + ApiManager.mapToApi(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>())));
  283. // ResponsePojo testRP = new ResponsePojo();
  284. // testRP.setData(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>()));
  285. // System.out.println("MODUS2=" + testRP.toApi());
  286. // srcQuery = new BasicDBObject("useExtractor", "none");
  287. // fileSource = SourcePojo.fromDb(DbManager.getConfig().getSource().findOne(srcQuery), SourcePojo.class);
  288. // System.out.println("SAH=" + ApiManager.mapToApi(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>())));
  289. // testRP.setData(fileSource, new SourcePojoApiMap(new HashSet<ObjectId>()));
  290. // System.out.println("SAH2=" + testRP.toApi());
  291. //API: Get an API object
  292. ////////////////////////////////////////////////
  293. //CANONICAL EXAMPLE:
  294. String sQueryJson = "{ 'logic': 'alex' }";
  295. AdvancedQueryPojo aqp = AdvancedQueryPojo.fromApi(sQueryJson, AdvancedQueryPojo.class);
  296. //AdvancedQueryPojo aqp = AdvancedQueryPojo.fromApi(sQueryJson, new TypeToken<AdvancedQueryPojo>(){});
  297. //^^^ (equivalent, needed for types that are generics)
  298. ////////////////////////////////////////////////
  299. System.out.println("AQP="+new Gson().toJson(aqp));
  300. // Testing ResponsePojo code on serialization
  301. rp1.setData(aqp);
  302. sRPSingleObject = rp1.toApi();
  303. rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class, AdvancedQueryPojo.class);
  304. System.out.println("RECREATED RP_AQP=" + rpRecreated.toApi());
  305. System.out.println("RECREATED AQP(RP_AQP)=" + ((AdvancedQueryPojo)(rpRecreated.getData())).toApi());
  306. //API: Get a list of objects
  307. sQueryJson = "{ 'logic': 'chris' }";
  308. AdvancedQueryPojo aqp2 = AdvancedQueryPojo.fromApi(sQueryJson, AdvancedQueryPojo.class);
  309. List<AdvancedQueryPojo> list2 = Arrays.asList(aqp, aqp2);
  310. rp1.setData(list2);
  311. sRPList = rp1.toApi();
  312. System.out.println("RP=" + sRPList);
  313. // This won't compile because didn't write a listType() for AQP (since it's never actually used in this way)
  314. //rpRecreated = ResponsePojo.listFromApi(sRPList, ResponsePojo.class, AdvancedQueryPojo.listType());
  315. rpRecreated = ResponsePojo.listFromApi(sRPList, ResponsePojo.class, new TypeToken<List<AdvancedQueryPojo>>(){});
  316. System.out.println("RECREATED RP_AQP=" + rpRecreated.toApi());
  317. ////////////////////////////////////////////////
  318. //CANONICAL EXAMPLE:
  319. sQueryJson = " [ { 'logic': 'alex' } , { 'logic': 'chris' } ] ";
  320. list2 = AdvancedQueryPojo.listFromApi(sQueryJson, new TypeToken<List<AdvancedQueryPojo>>(){});
  321. ////////////////////////////////////////////////
  322. System.out.println("APL=" + new Gson().toJson(list2));
  323. //API: BasicDBList (like feeds in the full system)
  324. BasicDBList dbl = new BasicDBList();
  325. BasicDBObject db01 = new BasicDBObject("index", 1);
  326. BasicDBObject db02 = new BasicDBObject("index", 2);
  327. BasicDBObject db03 = new BasicDBObject("index", 3);
  328. dbl.addAll(Arrays.asList(db01, db02, db03));
  329. ResponsePojo rp2 = new ResponsePojo(null, dbl, (BasePojoApiMap<BasicDBList>)null);
  330. System.out.println("DBO=" + rp2.toApi());
  331. //API: test the V0 DocumentPojo, which has a few differences (including a static version for raw modification)
  332. BasicDBObject docApiDbo = (BasicDBObject) DbManager.getDocument().getMetadata().findOne();
  333. // (remove a few things to tidy up display)
  334. docApiDbo.remove("entities");
  335. docApiDbo.remove("associations");
  336. docApiDbo.remove("metadata");
  337. // (remove things as a test)
  338. // docApiDbo.remove("sourceKey");
  339. // docApiDbo.remove("source");
  340. // docApiDbo.remove("mediaType");
  341. // (sourceKey in <key>#<community> format)
  342. // docApiDbo.put("sourceKey", docApiDbo.getString("sourceKey")+"#doc_api_test");
  343. // (display results of API mappings)
  344. DocumentPojo docApi = DocumentPojo.fromDb(docApiDbo, DocumentPojo.class);
  345. ResponsePojo rp3 = new ResponsePojo(null, docApi, new DocumentPojoApiMap());
  346. System.out.println("TIME_DOC_API1_CREATED="+docApi.getCreated());
  347. System.out.println("DOC_API1=" + rp3.toApi());
  348. DocumentPojoApiMap.mapToApi(docApiDbo);
  349. System.out.println("DOC_API2=" + BaseApiPojo.getDefaultBuilder().setPrettyPrinting().create().toJson(docApiDbo));
  350. DocumentPojo docFromApi = ApiManager.mapFromApi(ApiManager.mapToApi(docApi, null), DocumentPojo.class, null);
  351. System.out.println("TIME_DOC_API1_CREATED_INV="+docFromApi.getCreated());
  352. /////////////////////////////////////////////////////////////////////////////
  353. // DB testing:
  354. System.out.println("Open Community DB collection");
  355. //OLD:
  356. //CollectionManager cm = new CollectionManager();
  357. //DBCollection communityDb = cm.getCommunities();
  358. //NEW:
  359. DBCollection communityDb = DbManager.getSocial().getCommunity();
  360. //DB: read/write community object
  361. ////////////////////////////////////////////////
  362. //CANONICAL EXAMPLE:
  363. CommunityPojo cp = CommunityPojo.fromDb(communityDb.findOne(), CommunityPojo.class);
  364. System.out.println("CP1=" + cp.toDb()); // (converts DBObject to string ie BSON->JSON - should have { $oid } and { $date } objectid/date formats)
  365. ////////////////////////////////////////////////
  366. System.out.println("CP2=" + new Gson().toJson(cp)); // (will have complex object id format and string dates)
  367. //DB: read/write list of community objects
  368. ////////////////////////////////////////////////
  369. //CANONICAL EXAMPLE:
  370. List<CommunityPojo> cpl = CommunityPojo.listFromDb(communityDb.find().limit(3), CommunityPojo.listType());
  371. System.out.println("CPL1=" + CommunityPojo.listToDb(cpl, CommunityPojo.listType()));
  372. ////////////////////////////////////////////////
  373. System.out.println("CPL2=" + BaseDbPojo.getDefaultBuilder().create().toJson(cpl)); // (will have complex object id format and string dates)
  374. //Expect to see another delay here with the old method, new method should roll on...
  375. System.out.println("Open Document DB collection");
  376. //OLD:
  377. //CollectionManager cm2 = new CollectionManager();
  378. //DBCollection documentDb = cm2.getFeeds();
  379. //NEW:
  380. DBCollection documentDb = DbManager.getDocument().getMetadata();
  381. //DB: Read/write feed with metadata
  382. BasicDBObject query = new BasicDBObject("metadata", new BasicDBObject("$exists", true)); // (complex query so can't represent using pojos)
  383. query.put("entities", new BasicDBObject("$size", 3));
  384. ////////////////////////////////////////////////
  385. //CANONICAL EXAMPLE:
  386. DocumentPojo doc = DocumentPojo.fromDb(documentDb.findOne(query), DocumentPojo.class);
  387. System.out.println("DOC1="+doc.toDb());
  388. BasicDBList dblTest = (BasicDBList) doc.toDb().get("entities");
  389. BasicDBObject dboTest = (BasicDBObject) dblTest.get(0);
  390. if (!dboTest.get("doccount").getClass().toString().equals("class java.lang.Long")) {
  391. throw new RuntimeException(dboTest.get("doccount").getClass().toString() + " SHOULD BE LONG");
  392. }
  393. ////////////////////////////////////////////////
  394. System.out.println("DOC2="+new Gson().toJson(doc));
  395. doc = DocumentPojo.fromDb(documentDb.findOne(query), new TypeToken<DocumentPojo>(){}); // (alternative to the prettier DocumentPojo.class, needed for container classes)
  396. System.out.println("DOC3="+doc.toDb());
  397. //DB: list example for doc
  398. Set<DocumentPojo> docset = DocumentPojo.listFromDb(documentDb.find(query).limit(3), new TypeToken<Set<DocumentPojo>>(){});
  399. System.out.println("DOCSET="+DocumentPojo.listToDb(docset, new TypeToken<Set<DocumentPojo>>(){}));
  400. // Shares - demonstrate mapping of _ids across to the API (and that binary data is discarded):
  401. List<SharePojo> shares = SharePojo.listFromDb(DbManager.getSocial().getShare().find().limit(10), SharePojo.listType());
  402. System.out.println("SHARE="+ApiManager.mapListToApi(shares, SharePojo.listType(), new SharePojoApiMap(null)));
  403. // Index testing:
  404. ////////////////////////////////////////////////
  405. //CANONICAL EXAMPLE:
  406. DocumentPojoIndexMap docMap = new DocumentPojoIndexMap();
  407. System.out.println("DOC_INDEX=" + IndexManager.mapToIndex(doc, docMap));
  408. ////////////////////////////////////////////////
  409. // Check use of enums in Entity pojo works
  410. EntityPojo testEnt = new EntityPojo();
  411. testEnt.setDimension(EntityPojo.Dimension.Where);
  412. System.out.println("ENT1=" + new GsonBuilder().setPrettyPrinting().create().toJson(testEnt));
  413. System.out.println("DIM=" + testEnt.getDimension());
  414. BasicDBObject testEntDb = new BasicDBObject("dimension", "Who");
  415. testEnt = new Gson().fromJson(testEntDb.toString(), EntityPojo.class);
  416. System.out.println("ENT2=" + new GsonBuilder().setPrettyPrinting().create().toJson(testEnt));
  417. try {
  418. testEntDb = new BasicDBObject("dimension", "what");
  419. testEnt = BaseDbPojo.getDefaultBuilder().create().fromJson(testEntDb.toString(), EntityPojo.class);
  420. System.out.println("***FAIL=" + BaseDbPojo.getDefaultBuilder().setPrettyPrinting().create().toJson(testEnt));
  421. }
  422. catch (Exception e) {
  423. System.out.println("ENT3: Correctly failed with illegal dimension type");
  424. }
  425. ////////////////////////////////////////////////
  426. // Metadata transformations based on type:
  427. String metadataObjStr = "{ 'test__long': 3, 'test_long': '3', 'error__long': { 'field1': 'no'}, "+
  428. "'test_arrayObj': [ { 'field1': 'test' } ], 'test_nestedArrayObj': [ [ { 'field1': 'test' } ] ], "+
  429. "'test_array': [ 'val' ], 'test_nestedArray': [ [ 'val' ] ], "+
  430. "'test_obj': { 'field1': 'string' }, 'test_nestedObj': { 'field1': 'string', 'field2': { 'field3': 'string' }},"+
  431. "'test_null1': {}, test_null2: null"+
  432. "}";
  433. BasicDBObject metadataObj = (BasicDBObject) com.mongodb.util.JSON.parse(metadataObjStr);
  434. doc.addToMetadata("TestMeta", metadataObj);
  435. System.out.println("DOC_META=" + docMap.extendBuilder(BaseApiPojo.getDefaultBuilder()).setPrettyPrinting().create().toJson(doc));
  436. // Changes to new ElasticSearch construct (particularly for bulk add)
  437. ElasticSearchManager indexManager = IndexManager.createIndex("test", null, false, null, null, ImmutableSettings.settingsBuilder());
  438. BulkResponse result = null;
  439. // All docs
  440. result = indexManager.bulkAddDocuments(IndexManager.mapListToIndex(docset, new TypeToken<Set<DocumentPojo>>(){},
  441. new DocumentPojoIndexMap()), "_id", null, true);
  442. if (result.hasFailures()) {
  443. System.out.print("****** FAILED: ");
  444. System.out.println(result.buildFailureMessage());
  445. }
  446. //Delete index (once testing complete)
  447. indexManager.deleteMe();
  448. }
  449. }