@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));
}
java类com.mongodb.util.JSON的实例源码
UnorganizedTests.java 文件源码
项目:digital-display-garden-iteration-3-sixguysburgers-fries
阅读 29
收藏 0
点赞 0
评论 0
PlantController.java 文件源码
项目:digital-display-garden-iteration-4-dorfner-v2
阅读 34
收藏 0
点赞 0
评论 0
/**
* 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);
}
MongoCrudServletTest.java 文件源码
项目:nomopojo
阅读 51
收藏 0
点赞 0
评论 0
@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());
}
PlantController.java 文件源码
项目:digital-display-garden-iteration-4-revolverenguardia-1
阅读 35
收藏 0
点赞 0
评论 0
/**
* 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);
}
PlantController.java 文件源码
项目:digital-display-garden-iteration-3-sixguysburgers-fries
阅读 27
收藏 0
点赞 0
评论 0
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);
}
PlantController.java 文件源码
项目:digital-display-garden-iteration-4-dorfner-v2
阅读 32
收藏 0
点赞 0
评论 0
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);
}
FlowerController.java 文件源码
项目:digital-display-garden-iteration-2-spraguesanborn
阅读 35
收藏 0
点赞 0
评论 0
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);
}
StoreInMongoIT.java 文件源码
项目:nifi-nars
阅读 27
收藏 0
点赞 0
评论 0
@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"));
}
StoreInMongoIT.java 文件源码
项目:nifi-nars
阅读 35
收藏 0
点赞 0
评论 0
@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"));
}
QueryMongoIT.java 文件源码
项目:nifi-nars
阅读 26
收藏 0
点赞 0
评论 0
@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"));
}
ModuleSerializer.java 文件源码
项目:fiware-sinfonier
阅读 25
收藏 0
点赞 0
评论 0
@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;
}
DeviceConfigRestController.java 文件源码
项目:konker-platform
阅读 19
收藏 0
点赞 0
评论 0
@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());
}
}
ApplicationDocumentStoreRestController.java 文件源码
项目:konker-platform
阅读 23
收藏 0
点赞 0
评论 0
@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());
}
}
ApplicationDocumentStoreRestController.java 文件源码
项目:konker-platform
阅读 26
收藏 0
点赞 0
评论 0
@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());
}
}
DeviceCustomDataRestController.java 文件源码
项目:konker-platform
阅读 21
收藏 0
点赞 0
评论 0
@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());
}
}
DeviceCustomDataRestController.java 文件源码
项目:konker-platform
阅读 21
收藏 0
点赞 0
评论 0
@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());
}
}
DeviceCustomDataRestControllerTest.java 文件源码
项目:konker-platform
阅读 31
收藏 0
点赞 0
评论 0
@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))));
}
ApplicationDocumentStoreRestControllerTest.java 文件源码
项目:konker-platform
阅读 27
收藏 0
点赞 0
评论 0
@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))));
}
AbstractAggregateQueryProvider.java 文件源码
项目:mongodb-aggregate-query-support
阅读 29
收藏 0
点赞 0
评论 0
/**
* 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;
}
MongoDBConnector.java 文件源码
项目:Cotton
阅读 27
收藏 0
点赞 0
评论 0
/**
* 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;
}
MongoDBConnector.java 文件源码
项目:Cotton
阅读 28
收藏 0
点赞 0
评论 0
/**
* 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;
}
MongoDbRepository.java 文件源码
项目:hawkcd
阅读 24
收藏 0
点赞 0
评论 0
@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;
}
MongoAdapter.java 文件源码
项目:OntologyBasedInormationExtractor
阅读 25
收藏 0
点赞 0
评论 0
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);
}
DBApiLayer.java 文件源码
项目:chariot
阅读 26
收藏 0
点赞 0
评论 0
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 );
}
DBApiLayer.java 文件源码
项目:chariot
阅读 34
收藏 0
点赞 0
评论 0
@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 );
}
MongoDbBasicConverters.java 文件源码
项目:Camel
阅读 35
收藏 0
点赞 0
评论 0
@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;
}
MongoDbOperationsTest.java 文件源码
项目:Camel
阅读 22
收藏 0
点赞 0
评论 0
@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);
}
TestDocumentDecoder.java 文件源码
项目:toolbox
阅读 31
收藏 0
点赞 0
评论 0
@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)));
}
MongoQueryRunnerTest.java 文件源码
项目:prototype-20150626
阅读 24
收藏 0
点赞 0
评论 0
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());
}
MongoQueryRunnerTest.java 文件源码
项目:prototype-20150626
阅读 28
收藏 0
点赞 0
评论 0
@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"));
}