@Test public void incrementMetadata() { //This method is called from inside of getPlantByPlantId(); boolean myPlant = plantController. incrementMetadata("58d1c36efb0cac4e15afd278", "pageViews"); assertFalse(myPlant); boolean myPlant2 = plantController.incrementMetadata("16001.0","pageViews"); assertTrue(myPlant2); //This is necessary to test the data separately from getPlantByPlantId(); Document searchDocument = new Document(); searchDocument.append("id", "16001.0"); MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase(databaseName); MongoCollection<Document> plantCollection = db.getCollection("plants"); String before = JSON.serialize(plantCollection.find(searchDocument)); plantController.incrementMetadata("16001.0","pageViews"); String after = JSON.serialize(plantCollection.find(searchDocument)); assertFalse(before.equals(after)); }
/** * Takes `uploadID` and returns all bed names as a json format string * @param uploadID - the year that the data was uploaded * @return String representation of json with all bed names */ public String getGardenLocationsAsJson(String uploadID){ AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.match(eq("uploadID", uploadID)), //!! Order is important here Aggregates.group("$gardenLocation"), Aggregates.sort(Sorts.ascending("_id")) )); List<Document> listDoc = new ArrayList<>(); for (Document doc : documents) { listDoc.add(doc); } listDoc.sort(new BedComparator()); return JSON.serialize(listDoc); }
@Test public void testGetZipsWithLimit() throws Exception { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); when(response.getWriter()).thenReturn(pw); when(request.getPathInfo()).thenReturn("/zips"); int limit = 50; @SuppressWarnings("serial") HashMap<String, String[]> parameterMap = new HashMap<String, String[]>() { { put("limit", new String[] { "50" }); } }; when(request.getParameterMap()).thenReturn(parameterMap); new MongoCrudServlet().doGet(request, response); String result = sw.getBuffer().toString().trim(); System.out.println("Json Result As String is : " + result.length() + " characters long"); assertTrue("somehow got a very small JSON resposne: " + result, result.length() > 20); System.out.println("first few lines of Json Result:\n" + result.substring(0, 400)); BasicDBList json = (BasicDBList) JSON.parse(result); assertTrue("json.size() should be " + limit + ", got " + json.size() + " instead", limit == json.size()); }
/** * List all plants within the database, filtered by uploadId, gardenLocation and commonName * @param queryParams * @param uploadId * @return */ public String listPlants(Map<String, String[]> queryParams, String uploadId) { if (!ExcelParser.isValidUploadId(db, uploadId)) return "null"; //Create a filter based on query params Document filterDoc = new Document(); filterDoc.append("uploadId", uploadId); if (queryParams.containsKey("gardenLocation")) { String location =(queryParams.get("gardenLocation")[0]); filterDoc = filterDoc.append("gardenLocation", location); } if (queryParams.containsKey("commonName")) { String commonName =(queryParams.get("commonName")[0]); filterDoc = filterDoc.append("commonName", commonName); } FindIterable<Document> matchingPlants = plantCollection.find(filterDoc); matchingPlants.sort(Sorts.ascending("commonName", "cultivar")); return JSON.serialize(matchingPlants); }
public String listPlants(Map<String, String[]> queryParams, String uploadId) { Document filterDoc = new Document(); filterDoc.append("uploadId", uploadId); if (queryParams.containsKey("gardenLocation")) { String location = (queryParams.get("gardenLocation")[0]); filterDoc = filterDoc.append("gardenLocation", location); } if (queryParams.containsKey("commonName")) { String commonName = (queryParams.get("commonName")[0]); filterDoc = filterDoc.append("commonName", commonName); } FindIterable<Document> matchingPlants = plantCollection.find(filterDoc); return JSON.serialize(matchingPlants); }
public String listPlants(Map<String, String[]> queryParams, String uploadID) { Document filterDoc = new Document(); filterDoc.append("uploadID", uploadID); if (queryParams.containsKey("gardenLocation")) { String location =(queryParams.get("gardenLocation")[0]); filterDoc = filterDoc.append("gardenLocation", location); } if (queryParams.containsKey("commonName")) { String commonName =(queryParams.get("commonName")[0]); filterDoc = filterDoc.append("commonName", commonName); } FindIterable<Document> matchingPlants = plantCollection.find(filterDoc); List<Document> sortedPlants = new ArrayList<>(); for (Document doc : matchingPlants) { sortedPlants.add(doc); } sortedPlants.sort(new PlantComparator()); return JSON.serialize(sortedPlants); }
public String listFlowers(Map<String, String[]> queryParams) { Document filterDoc = new Document(); if (queryParams.containsKey("cultivar")) { String targetCultivar = queryParams.get("cultivar")[0]; filterDoc = filterDoc.append("cultivar", targetCultivar); } if (queryParams.containsKey("source")) { String targetSource = queryParams.get("source")[0]; filterDoc = filterDoc.append("source", targetSource); } if (queryParams.containsKey("gardenLocation")) { String targetLocation = queryParams.get("gardenLocation")[0]; filterDoc = filterDoc.append("gardenLocation", targetLocation); } if (queryParams.containsKey("year")) { int targetYear = Integer.parseInt(queryParams.get("year")[0]); filterDoc = filterDoc.append("year", targetYear); } FindIterable<Document> matchingFlowers = flowerCollection.find(filterDoc); return JSON.serialize(matchingFlowers); }
@Test public void insert_refined_payload_test() throws Exception { final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo()); addMongoService(runner); runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME); runner.setProperty(MongoProps.COLLECTION, "insert_test"); String contents = FileUtils.readFileToString(Paths.get("src/test/resources/payload.json").toFile()); runner.enqueue(contents.getBytes()); runner.run(); runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 0); runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 1); // Verify Wrapped Payload MockFlowFile out = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_SUCCESS).get(0); BasicDBObject actual = (BasicDBObject) JSON.parse(new String(out.toByteArray(), StandardCharsets.UTF_8)); assertNotNull(actual.getString("d")); }
@Test public void insert_test() throws Exception { final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo()); addMongoService(runner); runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME); runner.setProperty(MongoProps.COLLECTION, "insert_test"); runner.enqueue("{\"a\":\"a\"}".getBytes()); runner.run(); runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 0); runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 1); // Verify Wrapped Payload MockFlowFile out = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_SUCCESS).get(0); BasicDBObject actual = (BasicDBObject) JSON.parse(new String(out.toByteArray(), StandardCharsets.UTF_8)); assertEquals("a", actual.getString("a")); }
@Test public void testQuery() throws Exception { final TestRunner runner = TestRunners.newTestRunner(new QueryMongo()); addMongoService(runner); runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME); runner.setProperty(MongoProps.COLLECTION, "insert_test"); runner.setProperty(MongoProps.QUERY, "{\"criteria\": \"${test_attribute}\"}"); ProcessSession session = runner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); ff = session.putAttribute(ff, "test_attribute", "12345"); runner.enqueue(ff); runner.run(); runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 0); runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 1); MockFlowFile out = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_SUCCESS).get(0); BasicDBObject actual = (BasicDBObject) JSON.parse(new String(out.toByteArray(), StandardCharsets.UTF_8)); assertEquals("[ \"12345\" , \"23456\" , \"34567\"]", actual.getString("criteria")); }
@Override public JsonElement serialize(Module module, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject object = new JsonObject(); JsonParser parser = new JsonParser(); object.add(FIELD_CLASS, new JsonPrimitive(module.getClazz())); object.add(FIELD_ABSTRACTION_ID, new JsonPrimitive(module.getAbstractionId())); object.add(FIELD_PARALLELISM, new JsonPrimitive(module.getParallelism())); if (module.getParams().size() > 0) { object.add(FIELD_PARAMS, parser.parse(JSON.serialize(module.getParams())).getAsJsonObject()); } if (module.getSources().size() > 0) { object.add(FIELD_SOURCES, parser.parse(JSON.serialize(module.getSources())).getAsJsonObject()); } return object; }
@GetMapping(path = "/{deviceModelName}/{locationName}") @ApiOperation( value = "Get a device config by guid", response = RestResponse.class ) @PreAuthorize("hasAuthority('SHOW_DEVICE_CONFIG')") public Object read( @PathVariable("application") String applicationId, @PathVariable("deviceModelName") String deviceModelName, @PathVariable("locationName") String locationName) throws BadServiceResponseException, NotFoundResponseException { Tenant tenant = user.getTenant(); Application application = getApplication(applicationId); DeviceModel deviceModel = getDeviceModel(tenant, application, deviceModelName); Location location = getLocation(tenant, application, locationName); ServiceResponse<String> restDestinationResponse = deviceConfigSetupService.findByModelAndLocation(tenant, application, deviceModel, location); if (!restDestinationResponse.isOk()) { throw new NotFoundResponseException(restDestinationResponse); } else { return JSON.parse(restDestinationResponse.getResult()); } }
@GetMapping @ApiOperation( value = "Get a application document by collection and key", response = RestResponse.class ) @PreAuthorize("hasAuthority('SHOW_APPLICATION')") public Object read( @PathVariable("application") String applicationId, @PathVariable("collection") String collection, @PathVariable("key") String key) throws BadServiceResponseException, NotFoundResponseException { Tenant tenant = user.getTenant(); Application application = getApplication(applicationId); ServiceResponse<ApplicationDocumentStore> deviceResponse = applicationDocumentStoreService.findUniqueByTenantApplication(tenant, application, collection, key); if (!deviceResponse.isOk()) { throw new NotFoundResponseException(deviceResponse); } else { return JSON.parse(deviceResponse.getResult().getJson()); } }
@PostMapping @ApiOperation(value = "Create a application document") @PreAuthorize("hasAuthority('ADD_APPLICATION')") public Object create( @PathVariable("application") String applicationId, @PathVariable("collection") String collection, @PathVariable("key") String key, @ApiParam(name = "body", required = true) @RequestBody String jsonCustomData) throws BadServiceResponseException, NotFoundResponseException { Tenant tenant = user.getTenant(); Application application = getApplication(applicationId); ServiceResponse<ApplicationDocumentStore> deviceResponse = applicationDocumentStoreService.save(tenant, application, collection, key, jsonCustomData); if (!deviceResponse.isOk()) { throw new BadServiceResponseException( deviceResponse, validationsCode); } else { return JSON.parse(deviceResponse.getResult().getJson()); } }
@GetMapping @ApiOperation( value = "Get a custom data by device guid", response = RestResponse.class ) @PreAuthorize("hasAuthority('SHOW_DEVICE')") public Object read( @PathVariable("application") String applicationId, @PathVariable("deviceGuid") String deviceGuid) throws BadServiceResponseException, NotFoundResponseException { Tenant tenant = user.getTenant(); Application application = getApplication(applicationId); Device device = getDevice(tenant, application, deviceGuid); ServiceResponse<DeviceCustomData> deviceResponse = deviceCustomDataService.getByTenantApplicationAndDevice(tenant, application, device); if (!deviceResponse.isOk()) { throw new NotFoundResponseException(deviceResponse); } else { return JSON.parse(deviceResponse.getResult().getJson()); } }
@PostMapping @ApiOperation(value = "Create a device custom data") @PreAuthorize("hasAuthority('ADD_DEVICE')") public Object create( @PathVariable("application") String applicationId, @PathVariable("deviceGuid") String deviceGuid, @ApiParam(name = "body", required = true) @RequestBody String jsonCustomData) throws BadServiceResponseException, NotFoundResponseException { Tenant tenant = user.getTenant(); Application application = getApplication(applicationId); Device device = getDevice(tenant, application, deviceGuid); ServiceResponse<DeviceCustomData> deviceResponse = deviceCustomDataService.save(tenant, application, device, jsonCustomData); if (!deviceResponse.isOk()) { throw new BadServiceResponseException( deviceResponse, validationsCode); } else { return JSON.parse(deviceResponse.getResult().getJson()); } }
@Test public void shouldCreateDevice() throws Exception { when(deviceCustomDataService.save(tenant, application, device1, json1)) .thenReturn(ServiceResponseBuilder.<DeviceCustomData>ok().withResult(deviceCustomData1).build()); getMockMvc().perform(MockMvcRequestBuilders.post(MessageFormat.format("/{0}/{1}/{2}/{3}", application.getName(), BASEPATH, device1.getGuid(), CUSTOMDATAPATH)) .content(json1) .contentType("application/json") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.code", is(HttpStatus.CREATED.value()))) .andExpect(jsonPath("$.status", is("success"))) .andExpect(jsonPath("$.timestamp",greaterThan(1400000000))) .andExpect(jsonPath("$.result").isMap()) .andExpect(jsonPath("$.result", is(JSON.parse(json1)))); }
@Test public void shouldCreateDevice() throws Exception { when(applicationDocumentStoreService.save(tenant, application, "collection1", "keyA", json1)) .thenReturn(ServiceResponseBuilder.<ApplicationDocumentStore>ok().withResult(deviceCustomData1).build()); getMockMvc().perform(MockMvcRequestBuilders.post(MessageFormat.format("/{0}/{1}/{2}/{3}", application.getName(), BASEPATH, "collection1", "keyA", CUSTOMDATAPATH)) .content(json1) .contentType("application/json") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.code", is(HttpStatus.CREATED.value()))) .andExpect(jsonPath("$.status", is("success"))) .andExpect(jsonPath("$.timestamp",greaterThan(1400000000))) .andExpect(jsonPath("$.result").isMap()) .andExpect(jsonPath("$.result", is(JSON.parse(json1)))); }
/** * Returns a list of {@link ParameterBinding}s found in the given {@code input} or an * {@link Collections#emptyList()}. * * @param input - the string with parameter bindings * @return - the list of parameters */ public List<ParameterBinding> parseParameterBindingsFrom(String input) { if (!StringUtils.hasText(input)) { return Collections.emptyList(); } List<ParameterBinding> bindings = new ArrayList<>(); String parseableInput = makeParameterReferencesParseable(input); try { collectParameterReferencesIntoBindings(bindings, JSON.parse(parseableInput)); } catch(JSONParseException e) { // the parseable input is not JSON - some stages like $unwind and $count only have strings. // nothing to do here. LOGGER.trace("JSONParseException:", e); } return bindings; }
/** * Returns data from the database. * * @param searchKeys JSONObject containing the information regarding what data the return needs to contain. * @return returns an JSONObject containing desired data. */ @Override public synchronized JSONObject getDataFromDatabase (JSONObject searchKeys){ JSONArray retArray = new JSONArray(); JSONObject returnValue = new JSONObject(); BasicDBObject dbObject = (BasicDBObject) JSON.parse(searchKeys.toString()); FindIterable<Document> request = db.getCollection("requestTable").find(dbObject); for(Document d: request){ retArray.put(new JSONObject(d.toJson())); } returnValue.put("dataArray", retArray); return returnValue; }
/** * Adds user into the database. * * @param newUserData Contains user information like username, and password. * @return returns true od false depending on success. */ @Override public synchronized boolean addUserInDatabase (JSONObject newUserData){ boolean success = true; MongoCollection<Document> collection = db.getCollection("userTable"); JSONObject nameCheck = new JSONObject(); nameCheck.put("username", newUserData.get("username")); BasicDBObject dbObject = (BasicDBObject) JSON.parse(nameCheck.toString()); Document nameInDB = collection.find(dbObject).first(); if(nameInDB != null){ return false; } Document userInput = Document.parse(newUserData.toString()); try { collection.insertOne(userInput); }catch (MongoWriteException e){ //TODO logg error success = false; } return success; }
@Override public List<T> getAll() { T resultElement; List<T> result = new ArrayList<>(); try { FindIterable documents = this.collection.find(); for (Object document : documents) { String documentToJson = JSON.serialize(document); resultElement = this.jsonConverter.fromJson(documentToJson, this.entryType); result.add(resultElement); } } catch (RuntimeException e) { LOGGER.error(e); } return result; }
public DBCursor getDocsWith_betweenRange(Document doc,String key, Double score1,Double score2){ DBCollection dbcollection; dbcollection = db.getCollection(doc.getCategory()); BasicDBList list = new BasicDBList(); Gson gson = new Gson(); String json = gson.toJson(doc); BasicDBObject doc_query= (BasicDBObject)JSON.parse(json); list.add(doc_query); BasicDBObject less_query_and = new BasicDBObject(key,new BasicDBObject("$gt",score1)); list.add(less_query_and); BasicDBObject greater_query_and = new BasicDBObject(key,new BasicDBObject("$lt",score2)); list.add(greater_query_and); BasicDBObject query = new BasicDBObject("$and",list); System.out.println(query); return dbcollection.find(query); }
public WriteResult remove( DBObject o , com.mongodb.WriteConcern concern ) throws MongoException { if ( willTrace() ) trace( "remove: " + _fullNameSpace + " " + JSON.serialize( o ) ); OutMessage om = new OutMessage( _mongo , 2006 ); om.writeInt( 0 ); // reserved om.writeCString( _fullNameSpace ); Collection<String> keys = o.keySet(); if ( keys.size() == 1 && keys.iterator().next().equals( "_id" ) && o.get( keys.iterator().next() ) instanceof ObjectId ) om.writeInt( 1 ); else om.writeInt( 0 ); om.putObject( o ); return _connector.say( _db , om , concern ); }
@Override Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize, int limit , int options ) throws MongoException { if ( ref == null ) ref = new BasicDBObject(); if ( willTrace() ) trace( "find: " + _fullNameSpace + " " + JSON.serialize( ref ) ); OutMessage query = OutMessage.query( _mongo , options , _fullNameSpace , numToSkip , chooseBatchSize(batchSize, limit, 0) , ref , fields ); Response res = _connector.call( _db , this , query , null , 2 ); if ( res.size() == 0 ) return null; if ( res.size() == 1 ){ BSONObject foo = res.get(0); MongoException e = MongoException.parse( foo ); if ( e != null && ! _name.equals( "$cmd" ) ) throw e; } return new Result( this , res , batchSize, limit , options ); }
@Converter public static BasicDBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) { BasicDBObject answer = null; try { byte[] input = IOConverter.toBytes(is); if (isBson(input)) { BSONCallback callback = new JSONCallback(); new BasicBSONDecoder().decode(input, callback); answer = (BasicDBObject) callback.get(); } else { answer = (BasicDBObject) JSON.parse(IOConverter.toString(input, exchange)); } } catch (Exception e) { LOG.warn("String -> DBObject conversion selected, but the following exception occurred. Returning null.", e); } finally { // we need to make sure to close the input stream IOHelper.close(is, "InputStream", LOG); } return answer; }
@Test public void testCountOperation() throws Exception { // Test that the collection has 0 documents in it assertEquals(0, testCollection.count()); Object result = template.requestBody("direct:count", "irrelevantBody"); assertTrue("Result is not of type Long", result instanceof Long); assertEquals("Test collection should not contain any records", 0L, result); // Insert a record and test that the endpoint now returns 1 testCollection.insertOne((BasicDBObject) JSON.parse("{a:60}")); result = template.requestBody("direct:count", "irrelevantBody"); assertTrue("Result is not of type Long", result instanceof Long); assertEquals("Test collection should contain 1 record", 1L, result); testCollection.deleteOne(new BasicDBObject()); // test dynamicity dynamicCollection.insertOne((BasicDBObject) JSON.parse("{a:60}")); result = template.requestBodyAndHeader("direct:count", "irrelevantBody", MongoDbConstants.COLLECTION, dynamicCollectionName); assertTrue("Result is not of type Long", result instanceof Long); assertEquals("Dynamic collection should contain 1 record", 1L, result); }
@Test public void testUnions() throws Exception { Schema schema = Unions.SCHEMA$; String avroJson = "{\"union1\": {\"int\": 1}, \"union2\": {\"test.Union2\": {\"union21\": {\"long\": 2}}}, \"union3\": {\"array\": [{\"boolean\": true}, {\"boolean\": false}, {\"null\": null}]}, \"union4\": {\"map\": {\"a\": {\"string\": \"A\"}, \"b\": {\"string\": \"B\"}, \"c\": {\"string\": \"C\"}}}, \"union5\": {\"null\": null}, \"union6\": {\"null\": null}}"; Decoder decoder = DecoderFactory.get().jsonDecoder(schema, avroJson); GenericDatumReader<Record> reader = new GenericDatumReader<Record>(schema); Record record1 = reader.read(null, decoder); String mongoJson = "{\"union1\": 1, \"union2\": {\"union21\": 2}, \"union3\": [true, false, null], \"union4\": {\"a\": \"A\", \"b\": \"B\", \"c\": \"C\"}, \"union5\": null, \"union6\": null}"; DBObject object = (DBObject) JSON.parse(mongoJson); Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader()); assertThat(record2, is(record1)); assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1))); }
public static void seedDatabase() throws MongoException, IOException { mongoClient = new Mongo("localhost", 27017); DB database = mongoClient.getDB("dbname"); collection = database.getCollection("fda_enforcement"); collection.remove(new BasicDBObject()); assertEquals(0, collection.getCount()); BufferedReader br = new BufferedReader(new FileReader("src/test/resources/test_records.json")); String line = null; while ((line = br.readLine()) != null) { DBObject record = (DBObject) JSON.parse(line); collection.insert(record); } br.close(); assertEquals(50, collection.getCount()); }
@Test public void testQueryLimitFields() throws UnknownHostException, MongoException { assertEquals(50, collection.getCount()); MongoQueryRunner qr = new MongoQueryRunner(); String result = qr.query("localhost", "dbname", "fda_enforcement", "{\"recall_area\":\"California\"}", "recall_area", null); DBObject record = (DBObject) JSON.parse(result); assertTrue(record.containsField("count")); assertEquals(1, record.get("count")); assertTrue(record.containsField("results")); assertEquals(1, ((BasicDBList)record.get("results")).size()); BasicDBList results = (BasicDBList)record.get("results"); DBObject result1 = (DBObject)results.get(0); assertEquals(1, result1.keySet().size()); assertTrue(result1.containsField("recall_area")); }
private void loadTestDataIntoMongo(File testdata) throws IOException { System.out.println("loadTestDataIntoMongo(" + testdata.getName() + ") invoked"); long startTS = System.currentTimeMillis(); MongoCollection<BasicDBObject> zips = db.getCollection("zips", BasicDBObject.class); zips.drop(); long dropTS = System.currentTimeMillis(); List<String> zipLines = Files.readAllLines(testdata.toPath()); zipDocuments.clear(); for (String zipLine : zipLines) { BasicDBObject doc = (BasicDBObject) JSON.parse(zipLine); zipDocuments.add(doc); } zips.insertMany(zipDocuments); long insertTS = System.currentTimeMillis(); System.out.println(" took " + (dropTS - startTS) + "ms to drop zips collection"); System.out.println(" took " + (insertTS - dropTS) + "ms to insert " + zipDocuments.size() + " documents to zips collection"); System.out.println(" took " + ((1000.0 * zipDocuments.size()) / (insertTS - dropTS)) + " rows per second"); }
@Override @SuppressWarnings("unchecked") public Context constructObjects(DBObject next) { Context context = new Context(); List<Map<String, Double>> ticks = (List<Map<String, Double>>) next.get("tickers"); List<USMFStatus> tweets = new ArrayList<USMFStatus>(); List<Object> objl = (List<Object>) next.get("tweets"); for (Object object : objl) { USMFStatus status = new USMFStatus(); status.fillFromString(JSON.serialize(object)); tweets.add(status); } Long timestamp = (Long) next.get("timestamp"); context.put("timestamp", timestamp); context.put("usmfstatuses",tweets); context.put("ticks",ticks); return context; }
public static <S, T> T convertFromMongoDbType(Class<T> dataClass, S object) { if (object == null) { return null; } else if (dataClass.isInstance(object)) { return dataClass.cast(object); } else { @SuppressWarnings("unchecked") Converter<S, T> converter = (Converter<S, T>) CONVERTER_MAP.get(ImmutablePair.of(object.getClass(), dataClass)); if (converter != null) { return converter.convertFromMongoDbType(object); } else if (String.class.isAssignableFrom(dataClass) && object instanceof DBObject) { return dataClass.cast(JSON.serialize(object)); } else { return null; } } }
/** * Maps the given raw JSON string onto the provided class instance. * * @param jsonString raw JSON string * @param type object class * @param objectMapper custom object mapper * @return a list of instances of the provided class * @throws ResourceMappingException */ private List<T> mapJsonToInstance(String jsonString, Class<T> type, ObjectMapper objectMapper) throws ResourceMappingException { List<T> list = new ArrayList<>(); ObjectMapper mapper = objectMapper; if (mapper == null) { mapper = new ObjectMapper(); } try { for (DBObject dbObject: Transformer.convertToDbObject(jsonString)) { String toMap = JSON.serialize(dbObject); // map json string onto the given class type list.add(mapper.readValue(toMap, type)); } } catch (Exception e) { throw new ResourceMappingException(e.getMessage()); } return list; }
@Test public void testInvalidate() throws Exception { final Mache<String, String> mache = getMache(); final String key = "test-1"; final String expectedDescription = "test1-description"; this.mache.put(key, getJsonKey(key, expectedDescription)); assertEquals(expectedDescription, ((DBObject) JSON.parse(this.mache.get(key))).get("value")); assertEquals(expectedDescription, ((DBObject) JSON.parse(mache.get(key))).get("value")); final String expectedDescription2 = "test-description2"; mache.put(key, getJsonKey(key, expectedDescription2)); this.mache.invalidate(key); assertEquals(expectedDescription2, ((DBObject) JSON.parse(this.mache.get(key))).get("value")); assertEquals(expectedDescription2, ((DBObject) JSON.parse(mache.get(key))).get("value")); mache.close(); }
private String updateObject(String id, String entity) throws ServiceException { MongoDBHelper ds = new MongoDBHelper(); MongoDatabase db = ds.getConnection(); try { MongoCollection<Document> c = db.getCollection(this.collectionName); BasicDBObject q = (BasicDBObject) JSON.parse("{\"_id\": {\"$eq\": " + id + "}}"); Document d = Document.parse(entity); Document doc = d.get("o", Document.class); c.replaceOne(q, doc); //return object on success, to be consistent with add method return doc.toJson(); } catch (Exception ex) { ex.printStackTrace(); //wrap and bubble up throw ExceptionUtil.getException(Exceptions.ERR_DB, ex.getMessage()); } finally { if (ds !=null) { ds.closeConnection(); } } }
@Test public void testMaps() throws Exception { Schema schema = Maps.SCHEMA$; GenericRecordBuilder builder = new GenericRecordBuilder(schema); builder.set("maps", ImmutableMap.of("key1", ImmutableMap.of("value1", 1, "value2", 2), "key2", ImmutableMap.of(), "key3", ImmutableMap.of("value3", 3))); Record record1 = builder.build(); String json = "{\"maps\": {\"key1\": {\"value1\": 1, \"value2\": 2}, \"key2\": {}, \"key3\": {\"value3\": 3}}}"; DBObject object = (DBObject) JSON.parse(json); Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader()); // Convert into JsonNode before comparison, so the maps equal even if keys are reordered. assertThat(JSON.parse(AvroHelper.toSimpleJson(schema, record2)), is(JSON.parse(AvroHelper.toSimpleJson(schema, record1)))); }
@Test public void testProperty() throws Exception{ ExternalResourceDescription erd = ExternalResourceFactory.createExternalResourceDescription(MONGO, SharedFongoResource.class, FONGO_COLLECTION, MONGO_COLL, FONGO_DATA, JSON.serialize(GAZ_DATA)); AnalysisEngineDescription aed = AnalysisEngineFactory.createEngineDescription(MongoRegex.class, MONGO, erd, COLLECTION, MONGO_COLL, TYPE, LOCATION, REGEX, LONDON_REGEX); AnalysisEngine ae = AnalysisEngineFactory.createEngine(aed); jCas.setDocumentText(TEXT); ae.process(jCas); assertEquals(1, JCasUtil.select(jCas, Location.class).size()); Location l = JCasUtil.selectByIndex(jCas, Location.class, 0); assertEquals("London", l.getValue()); assertEquals("London", l.getCoveredText()); assertEquals("Property_Test", l.getGeoJson()); ae.destroy(); }
@Test public void testArrays() throws Exception { Schema schema = Arrays.SCHEMA$; GenericRecordBuilder builder = new GenericRecordBuilder(schema); builder.set("arrays", ImmutableList.of(ImmutableList.of(ImmutableList.of(1, 2, 3), ImmutableList.of()), ImmutableList.of(ImmutableList.of(4), ImmutableList.of()), ImmutableList.of(ImmutableList.of()))); Record record1 = builder.build(); String json = "{\"arrays\": [[[1, 2, 3], []], [[4], []], [[]]]}"; BSONObject object = (BSONObject) JSON.parse(json); Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader()); assertThat(record2, is(record1)); assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1))); }
@Test public void testEmptyFile() throws ResourceInitializationException, AnalysisEngineProcessException, IOException { // Due to limitations in the shared fongo resource we only test document count here! ExternalResourceDescription erd = ExternalResourceFactory.createExternalResourceDescription("mongo", SharedFongoResource.class, SharedFongoResource.PARAM_FONGO_COLLECTION, "documents", SharedFongoResource.PARAM_FONGO_DATA, JSON.serialize(DATA)); File tempFile = File.createTempFile("test", "mongostats"); try { AnalysisEngine task = create(MongoStats.class, "mongo", erd, "file", tempFile.getAbsolutePath()); execute(task); task.destroy(); List<String> lines = Files.readAllLines(tempFile.toPath()); assertEquals(2, lines.size()); assertEquals("timestamp,documents,entities,relations", lines.get(0)); String[] split = lines.get(1).split(","); assertEquals("3", split[1]); assertEquals("0", split[2]); assertEquals("0", split[3]); } finally { tempFile.delete(); } }