com.mongodb.util.JSON Java Examples

The following examples show how to use com.mongodb.util.JSON. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AggregateQueryProvider.java    From mongodb-aggregate-query-support with Apache License 2.0 6 votes vote down vote up
/**
 * Replaced the parameter place-holders with the actual parameter values from the given {@link ParameterBinding}s.
 *
 * @param query - the query string with placeholders
 * @return - the string with values replaced
 */
@SuppressWarnings({"Duplicates", "WeakerAccess"})
protected String replacePlaceholders(String query) {
  List<ParameterBinding> queryParameterBindings = parameterBindingParser.parseParameterBindingsFrom(query, JSON::parse);

  if (queryParameterBindings.isEmpty()) {
    return query;
  }
  String lquery = query;
  if(query.contains("@@")) {
    // strip quotes from the query
    lquery = query.replace("\"", "").replace("@@", "@");
  }
  StringBuilder result = new StringBuilder(lquery);

  for (ParameterBinding binding : queryParameterBindings) {
    String parameter = binding.getParameter();
    int idx = result.indexOf(parameter);
    String parameterValueForBinding = getParameterValueForBinding(convertingParameterAccessor, binding);
    if (idx != -1) {
      result.replace(idx, idx + parameter.length(), parameterValueForBinding);
    }
  }
  LOGGER.debug("Query after replacing place holders - {}", result);
  return result.toString();
}
 
Example #2
Source File: FeedResource.java    From hvdf with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/{feed}/{channel}/data")
public List<Sample> queryChannel(
        @PathParam("feed") String feedId,
        @PathParam("channel") String channelId,
        @QueryParam("source") String sourceString,
        @QueryParam("ts") long timeStart,
        @QueryParam("range") long timeRange,
        @QueryParam("query") JSONParam query,
    	@QueryParam("proj") JSONParam projection,
    	@QueryParam("limit") @DefaultValue("100") int limit) {

    // Find the correct channel implementation
	Channel channel = channelService.getChannel(feedId, channelId);
	
	// The source may be null or JSON
	Object source = null;
	if(sourceString != null){
		source = JSON.parse(sourceString);
	}
	    	
    // push it to the channel correct
	DBObject dbQuery = query != null ? query.toDBObject() : null;
	DBObject dbProjection = projection != null ? projection.toDBObject() : null;
	return channel.query(source, timeStart, timeRange, dbQuery, dbProjection, limit);    
}
 
Example #3
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
@Test public void testTopLevelObjectStructureNoNestedDocs() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = asList( mf( "field1", true, "" ), mf( "field2", true, "" ) );

  RowMetaInterface rmi = new RowMeta();
  rmi.addValueMeta( new ValueMetaString( "field1" ) );
  rmi.addValueMeta( new ValueMetaInteger( "field2" ) );

  Object[] row = new Object[ 2 ];
  row[ 0 ] = "value1";
  row[ 1 ] = 12L;
  VariableSpace vs = new Variables();

  for ( MongoDbOutputMeta.MongoField f : paths ) {
    f.init( vs );
  }

  DBObject result = kettleRowToMongo( paths, rmi, row, MongoDbOutputData.MongoTopLevel.RECORD, false );

  assertEquals( JSON.serialize( result ), "{ \"field1\" : \"value1\" , \"field2\" : 12}" );
}
 
Example #4
Source File: JsonTreeModelTest.java    From nosql4idea with Apache License 2.0 6 votes vote down vote up
@Test
    public void buildDBObjectFromTreeWithSubNodes() throws Exception {
        DBObject jsonObject = (DBObject) JSON.parse(IOUtils.toString(getClass().getResourceAsStream("simpleDocumentWithInnerNodes.json")));

//        Hack to convert _id fron string to ObjectId
        jsonObject.put("_id", new ObjectId(String.valueOf(jsonObject.get("_id"))));

        NoSqlTreeNode treeNode = (NoSqlTreeNode) JsonTreeModel.buildJsonTree(jsonObject);

//      Simulate updating from the treeNode
        NoSqlTreeNode innerDocNode = (NoSqlTreeNode) treeNode.getChildAt(4);
        NoSqlTreeNode soldOutNode = (NoSqlTreeNode) innerDocNode.getChildAt(2);
        soldOutNode.getDescriptor().setValue("false");

        DBObject dbObject = JsonTreeModel.buildDBObject(treeNode);

        assertEquals("{ \"_id\" : { \"$oid\" : \"50b8d63414f85401b9268b99\"} , \"label\" : \"toto\" , \"visible\" : false , \"image\" :  null  , \"innerdoc\" : { \"title\" : \"What?\" , \"numberOfPages\" : 52 , \"soldOut\" : false}}",
                dbObject.toString());
    }
 
Example #5
Source File: PrivateStorageRestController.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@GetMapping(path = "/{collectionName}")
@ApiOperation(value = "List all data from collection")
@PreAuthorize("hasAuthority('SHOW_PRIVATE_STORAGE')")
public Object list(
        @PathVariable("application") String applicationId,
        @PathVariable("collectionName") String collection) throws BadServiceResponseException, NotFoundResponseException {

    Tenant tenant = user.getTenant();
    Application application = getApplication(applicationId);

    ServiceResponse<List<PrivateStorage>> response = null;
    try {
        response = privateStorageService.findAll(tenant, application, user.getParentUser(), collection);

        if (!response.isOk()) {
            throw new NotFoundResponseException(response);
        } else {
            return response.getResult()
                    .stream()
                    .map(p -> JSON.parse(p.getCollectionContent()))
                    .collect(Collectors.toList());
        }
    } catch (JsonProcessingException e) {
        throw new NotFoundResponseException(response);
    }
}
 
Example #6
Source File: RascalMetricsTest.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private JSONArray getJSONArrayFromDB(String db, String col) {
	try {
		JSONArray result = new JSONArray(); 
		DBCollection collection = mongo.getDB(db).getCollectionFromString(col);
		DBCursor cursor = collection.find();

		while(cursor.hasNext()) {
			DBObject obj = cursor.next();
			JSONObject json = new JSONObject(JSON.serialize(obj));
			result.put(json);
		}
		return result;
	}
	catch(Exception e) {
		System.out.println("We got an error when creating a JSONArray: " + e);
		return null;
	}
}
 
Example #7
Source File: MongoDbInputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
@Test public void testFindWithMoreResults() throws KettleException, MongoDbException {
  // no query or fields defined, should do a collection.find()
  setupReturns();
  when( mockCursor.hasNext() ).thenReturn( true );
  ServerAddress serverAddress = mock( ServerAddress.class );
  when( serverAddress.toString() ).thenReturn( "serveraddress" );
  when( mockCursor.getServerAddress() ).thenReturn( serverAddress );
  DBObject nextDoc = (DBObject) JSON.parse( "{ 'foo' : 'bar' }" );
  when( mockCursor.next() ).thenReturn( nextDoc );
  dbInput.setStopped( false );
  dbInput.init( stepMetaInterface, stepDataInterface );

  assertTrue( "more results -> should return true", dbInput.processRow( stepMetaInterface, stepDataInterface ) );
  verify( mongoCollectionWrapper ).find();
  verify( mockCursor ).next();
  verify( mockLog ).logBasic( stringCaptor.capture() );
  assertThat( stringCaptor.getValue(), containsString( "serveraddress" ) );
  assertThat( stepDataInterface.cursor, equalTo( mockCursor ) );
  assertThat( putRow[0], CoreMatchers.<Object>equalTo( JSON.serialize( nextDoc ) ) );
}
 
Example #8
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Testing the use of Environment Substitution during initialization of fields.
 * @throws Exception
 */
@Test public void testTopLevelArrayWithEnvironmentSubstitution() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = asList( mf( "${ENV_FIELD}", true, "[0]" ),
          mf( "field2", true, "${ENV_DOC_PATH}" ) );

  RowMetaInterface rmi = new RowMeta();
  rmi.addValueMeta( new ValueMetaString( "field1" ) );
  rmi.addValueMeta( new ValueMetaInteger( "field2" ) );

  Object[] row = new Object[ 2 ];
  row[ 0 ] = "value1";
  row[ 1 ] = 12L;
  VariableSpace vs = new Variables();
  vs.setVariable( "ENV_FIELD", "field1" );
  vs.setVariable( "ENV_DOC_PATH", "[1]" );

  for ( MongoDbOutputMeta.MongoField f : paths ) {
    f.init( vs );
  }

  DBObject result = kettleRowToMongo( paths, rmi, row, MongoDbOutputData.MongoTopLevel.ARRAY, false );
  assertEquals( JSON.serialize( result ), "[ { \"field1\" : \"value1\"} , { \"field2\" : 12}]" );
}
 
Example #9
Source File: MongodbInputDiscoverFieldsImplTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
@Test public void testPipelineQueryIsLimited() throws KettleException, MongoDbException {
  setupPerform();

  String query = "{$sort : 1}";

  // Setup DBObjects collection
  List<DBObject> dbObjects = new ArrayList<DBObject>();
  DBObject firstOp = (DBObject) JSON.parse( query );
  DBObject[] remainder = { new BasicDBObject( "$limit", NUM_DOCS_TO_SAMPLE ) };
  dbObjects.add( firstOp );
  Collections.addAll( dbObjects, remainder );
  AggregationOptions options = AggregationOptions.builder().build();

  //when( MongodbInputDiscoverFieldsImpl.jsonPipelineToDBObjectList( query ) ).thenReturn( dbObjects );
  when( collection.aggregate( anyList(), any( AggregationOptions.class ) ) )
      .thenReturn( cursor );

  discoverFields.discoverFields( new MongoProperties.Builder(), "mydb", "mycollection", query, "", true,
      NUM_DOCS_TO_SAMPLE, inputMeta );

  verify( collection ).aggregate( anyList(), any( AggregationOptions.class ) );
}
 
Example #10
Source File: ApplicationDocumentStoreRestController.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@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());
    }

}
 
Example #11
Source File: PrivateStorageRestControllerTest.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldListData() throws Exception {
    when(privateStorageService.findAll(any(Tenant.class), any(Application.class), any(User.class), anyString()))
            .thenReturn(ServiceResponseBuilder.<List<PrivateStorage>>ok()
                    .withResult(allData).build());

    getMockMvc().perform(MockMvcRequestBuilders.get(MessageFormat.format("/{0}/{1}/{2}", application.getName(), BASEPATH, "customers"))
                .contentType("application/json")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.code", is(HttpStatus.OK.value())))
                .andExpect(jsonPath("$.status", is("success")))
                .andExpect(jsonPath("$.timestamp",greaterThan(1400000000)))
                .andExpect(jsonPath("$.result").isArray())
                .andExpect(jsonPath("$.result[0]", is(JSON.parse(json1))))
                .andExpect(jsonPath("$.result[1]", is(JSON.parse(json2))));

}
 
Example #12
Source File: PrivateStorageRestControllerTest.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadData() throws Exception {
    when(privateStorageService.findByQuery(any(Tenant.class), any(Application.class), any(User.class), anyString(), any(Map.class), anyString(), any(Integer.class), any(Integer.class)))
            .thenReturn(ServiceResponseBuilder.<List<PrivateStorage>>ok()
                    .withResult(Collections.singletonList(privateStorage1)).build());

    getMockMvc().perform(MockMvcRequestBuilders.get(MessageFormat.format("/{0}/{1}/{2}/search", application.getName(), BASEPATH, "customers"))
            .contentType("application/json")
            .param("q", "customers=konker")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$.code", is(HttpStatus.OK.value())))
            .andExpect(jsonPath("$.status", is("success")))
            .andExpect(jsonPath("$.timestamp",greaterThan(1400000000)))
            .andExpect(jsonPath("$.result").isArray())
            .andExpect(jsonPath("$.result[0]", is(JSON.parse(json1))));

}
 
Example #13
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _deleteByCriteria(Table table) {
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
        QueryCriteria queryCriteria = table.getQueryCriteria();
        com.mongodb.client.MongoDatabase db = database.getMongoDB();
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		db.getCollection(queryCriteria.getTable()).deleteMany(Document.parse((JSON.serialize(coditon))));
		LOG.debug("deleteByCriteria->collection:"+table.getTableName()+",condition:"+JSON.serialize(coditon));
	} catch (MongoException e) {
		LOG.error("mongo delete error", e);
	}
	return 1;
}
 
Example #14
Source File: DriverUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
static Object parseJSONExpr( String jsonExpr ) throws OdaException
{
    try
    {
        return JSON.parse( jsonExpr );
    }
    catch( JSONParseException ex )
    {
        String errMsg = Messages.bind( Messages.driverUtil_parsingError,
                jsonExpr );
        DriverUtil.getLogger().log( Level.INFO, errMsg, ex ); // caller may choose to ignore it; log at INFO level

        OdaException newEx = new OdaException( errMsg );
        newEx.initCause( ex );
        throw newEx;
    }
}
 
Example #15
Source File: NonReactiveAggregateQueryProvider.java    From mongodb-aggregate-query-support with Apache License 2.0 6 votes vote down vote up
/**
 * Replaced the parameter place-holders with the actual parameter values from the given {@link ParameterBinding}s.
 *
 * @param query - the query string with placeholders
 * @return - the string with values replaced
 */
@SuppressWarnings({"Duplicates", "WeakerAccess"})
protected String replacePlaceholders(String query) {
  List<ParameterBinding> queryParameterBindings = parameterBindingParser.parseParameterBindingsFrom(query, JSON::parse);

  if (queryParameterBindings.isEmpty()) {
    return query;
  }
  String lquery = query;
  if(query.contains("@@")) {
    // strip quotes from the query
    lquery = query.replace("\"", "").replace("@@", "@");
  }
  StringBuilder result = new StringBuilder(lquery);

  for (ParameterBinding binding : queryParameterBindings) {
    String parameter = binding.getParameter();
    int idx = result.indexOf(parameter);
    String parameterValueForBinding = getParameterValueForBinding(convertingParameterAccessor, binding);
    if (idx != -1) {
      result.replace(idx, idx + parameter.length(), parameterValueForBinding);
    }
  }
  LOGGER.debug("Query after replacing place holders - {}", result);
  return result.toString();
}
 
Example #16
Source File: ApplicationDocumentStoreRestControllerTest.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@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))));
}
 
Example #17
Source File: Tester.java    From topic-detection with Apache License 2.0 6 votes vote down vote up
public static List<Item> loadItemsFromFile(String filename){
    List<Item> items=new ArrayList<Item>();
    try{
        BufferedReader br=new BufferedReader(new FileReader(filename));
        String line=null;
        while((line=br.readLine())!=null){
            if(line.trim()!=""){
                Item new_item=new Item();
                DBObject dbObject = (DBObject) JSON.parse(line);
                String id=(String) dbObject.get("id_str");
                new_item.setId(id);
                String text=(String) dbObject.get("text");
                new_item.setTitle(text);
                DBObject tmp_obj=(DBObject) dbObject.get("user");
                String uploader=(String) tmp_obj.get("screen_name");
                new_item.setAuthorScreenName(uploader);
                items.add(new_item);
            }
        }
        br.close();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return items;
}
 
Example #18
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _countByCriteria(Table table) {
	int count = 0;
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
		QueryCriteria queryCriteria = table.getQueryCriteria();
		com.mongodb.client.MongoDatabase db = database.getMongoDB();
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		long size = db.getCollection(queryCriteria.getTable()).count(Document.parse((JSON.serialize(coditon))));
		LOG.debug("countByCriteria->collection:"+queryCriteria.getTable()+",script:"+JSON.serialize(coditon));
		count = (int) size;
	} catch (MongoException e) {
		LOG.error("mongo find error", e);
	}
	LOG.debug("_countByCriteria->result:"+count);
	return count;
}
 
Example #19
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
@Test public void testTopLevelObjectStructureOneLevelNestedDoc() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = asList( mf( "field1", true, "" ), mf( "field2", true, "nestedDoc" ) );

  RowMetaInterface rmi = new RowMeta();
  rmi.addValueMeta( new ValueMetaString( "field1" ) );
  rmi.addValueMeta( new ValueMetaInteger( "field2" ) );

  Object[] row = new Object[ 2 ];
  row[ 0 ] = "value1";
  row[ 1 ] = 12L;
  VariableSpace vs = new Variables();

  for ( MongoDbOutputMeta.MongoField f : paths ) {
    f.init( vs );
  }

  DBObject result = kettleRowToMongo( paths, rmi, row, MongoDbOutputData.MongoTopLevel.RECORD, false );

  assertEquals( JSON.serialize( result ), "{ \"field1\" : \"value1\" , \"nestedDoc\" : { \"field2\" : 12}}" );
}
 
Example #20
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _updateByCriteria(Table table) {
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
        QueryCriteria queryCriteria = table.getQueryCriteria();
        com.mongodb.client.MongoDatabase db = database.getMongoDB();
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		Map<String, Object> vaule = new HashMap<String, Object>();
		vaule.put("$set", table.getParams());
		db.getCollection(queryCriteria.getTable()).updateMany(Document.parse((JSON.serialize(coditon))), Document.parse(JSON.serialize(vaule)));
		LOG.debug("updateByCriteria->collection:"+table.getTableName()+",value:"+JSON.serialize(vaule)+",condition:"+JSON.serialize(coditon));
	} catch (MongoException e) {
		LOG.error("mongo update error", e);
	}
	return 1;
}
 
Example #21
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _updateByPrimaryKey(Table table) {
	try {
		if(table.getConditions().containsKey("_id")){
			com.mongodb.client.MongoDatabase db = database.getMongoDB();
			Object id = table.getConditions().get("_id");
			table.getParams().remove("id");//id被永久屏蔽
			Map<String, Object> vaule = new HashMap<String, Object>();
			vaule.put("$set", table.getParams());
			db.getCollection(table.getTableName()).updateOne(eq("_id", new ObjectId(String.valueOf(id))), Document.parse(JSON.serialize(vaule)));
			LOG.debug("updateByPrimaryKey->collection:"+table.getTableName()+",value:"+JSON.serialize(vaule)+",condition:"+eq("_id", new ObjectId(String.valueOf(id)).toString()));
			return 1;
		}
	} catch (MongoException e) {
		LOG.error("mongo update error", e);
	}
	return 0;
}
 
Example #22
Source File: DeviceCustomDataRestControllerTest.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@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))));
}
 
Example #23
Source File: PrivateStorageRestControllerTest.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCreateData() throws Exception {
    when(privateStorageService.save(any(Tenant.class), any(Application.class), any(User.class), anyString(), anyString()))
            .thenReturn(ServiceResponseBuilder.<PrivateStorage>ok().withResult(privateStorage1).build());

    getMockMvc().perform(MockMvcRequestBuilders.post(MessageFormat.format("/{0}/{1}/{2}", application.getName(), BASEPATH, "customers"))
                                               .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))));
}
 
Example #24
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 5 votes vote down vote up
@Test public void testGetQueryObjectThatContainsJsonNestedDoc() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = new ArrayList<MongoDbOutputMeta.MongoField>( 3 );

  MongoDbOutputMeta.MongoField mf = mf( "field1", true, "" );
  mf.m_updateMatchField = true;
  paths.add( mf );

  mf = mf( "field2", true, "" );
  mf.m_updateMatchField = true;
  paths.add( mf );

  mf = mf( "jsonField", true, "" );
  mf.m_updateMatchField = true;
  mf.m_JSON = true;
  paths.add( mf );

  RowMetaInterface rmi = new RowMeta();
  rmi.addValueMeta( new ValueMetaString( "field1" ) );
  rmi.addValueMeta( new ValueMetaInteger( "field2" ) );
  rmi.addValueMeta( new ValueMetaString( "jsonField" ) );

  Object[] row = new Object[ 3 ];
  row[ 0 ] = "value1";
  row[ 1 ] = 12L;
  row[ 2 ] = "{\"jsonDocField1\" : \"aval\", \"jsonDocField2\" : 42}";
  VariableSpace vs = new Variables();

  for ( MongoDbOutputMeta.MongoField f : paths ) {
    f.init( vs );
  }

  DBObject query = MongoDbOutputData.getQueryObject( paths, rmi, row, vs, MongoDbOutputData.MongoTopLevel.RECORD );

  assertEquals( JSON.serialize( query ),
    "{ \"field1\" : \"value1\" , \"field2\" : 12 , \"jsonField\" : { \"jsonDocField1\" : \"aval\" , "
      + "\"jsonDocField2\" : 42}}" );
}
 
Example #25
Source File: MongoSettings.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
@Override
public void updateSettings(JsonNode value) {
	BasicDBObject find = new BasicDBObject("nation", nation);
	DBObject obj = (DBObject)JSON.parse(value.toString());
	BasicDBObject update = new BasicDBObject("$set", obj);
	this.users.update(find, update);
}
 
Example #26
Source File: CmdIdxAccessStats.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private TableDto getIndexStats(MongoCollection<Document> collection, String dbsLabel){
    final TableDto result = new TableDto();
    final MongoIterable<Document> stats = collection
            .aggregate(Arrays.asList(
                    new Document("$indexStats", new Document()),
                    new Document("$sort", new Document("accesses.ops", 1))
            ));
    final HashMap<String, Document> indexesProperties = getIndexesProperties(collection);

    for(Document doc : stats){
        LOG.info("doc: {}", JSON.serialize(doc));
        final ArrayList<Object> row = new ArrayList<Object>();
        row.add(dbsLabel);
        row.add(doc.getString("host"));
        row.add(collection.getNamespace().getDatabaseName());
        row.add(collection.getNamespace().getCollectionName());
        final String indexName = doc.getString("name");
        row.add(indexName);
        row.add(((Document)doc.get("key")).toJson());
        row.add(Boolean.toString(isTTL(indexesProperties, indexName)));
        final Object accesses = doc.get("accesses");
        if(accesses instanceof Document){
            final Document accDoc = (Document) accesses;
            row.add(accDoc.getLong("ops"));
            final Date date = accDoc.getDate("since");
            final LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            row.add(localDateTime.format(DATE_TIME_FORMATTER));
        }else{
            row.add(0L);
            row.add("");
        }

        result.addRow(row);

    }

    return result;
}
 
Example #27
Source File: MongoDBEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<Reader> entityStates()
{
    return StreamSupport
        .stream( db.getCollection( collectionName ).find().spliterator(), false )
        .map( eachEntity ->
              {
                  Document bsonState = (Document) eachEntity.get( STATE_COLUMN );
                  String jsonState = JSON.serialize( bsonState );
                  return new StringReader( jsonState );
              } );
}
 
Example #28
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 5 votes vote down vote up
@Test public void testInsertKettleFieldThatContainsJsonIntoOneLevelNestedDoc() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = new ArrayList<MongoDbOutputMeta.MongoField>( 3 );

  MongoDbOutputMeta.MongoField mf = mf( "field1", true, "" );
  paths.add( mf );

  mf = mf( "field2", true, "nestedDoc" );
  paths.add( mf );

  mf = mf( "jsonField", true, "nestedDoc" );
  mf.m_JSON = true;
  paths.add( mf );

  RowMetaInterface rmi = new RowMeta();
  rmi.addValueMeta( new ValueMetaString( "field1" ) );
  rmi.addValueMeta( new ValueMetaInteger( "field2" ) );
  rmi.addValueMeta( new ValueMetaString( "jsonField" ) );

  Object[] row = new Object[ 3 ];
  row[ 0 ] = "value1";
  row[ 1 ] = 12L;
  row[ 2 ] = "{\"jsonDocField1\" : \"aval\", \"jsonDocField2\" : 42}";
  VariableSpace vs = new Variables();

  for ( MongoDbOutputMeta.MongoField f : paths ) {
    f.init( vs );
  }

  DBObject result = kettleRowToMongo( paths, rmi, row, MongoDbOutputData.MongoTopLevel.RECORD, false );

  assertEquals( JSON.serialize( result ),
    "{ \"field1\" : \"value1\" , \"nestedDoc\" : { \"field2\" : 12 , \"jsonField\" : { \"jsonDocField1\" : \"aval\""
      + " , \"jsonDocField2\" : 42}}}" );
}
 
Example #29
Source File: DeviceConfigSetupServiceImpl.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
private boolean isInvalidJson(String json) {

        if (StringUtils.isBlank(json)) {
            return true;
        }

        try {
            JSON.parse(json);
        } catch (JSONParseException e) {
            return true;
        }

        return false;
    }
 
Example #30
Source File: UpdateMongePmids.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static void main2(String[] args) throws Exception {

		MongoConnection mongo = new MongoConnection(MONGO_FT_CONNECTION);
		int cntSum = 0;
		for (int i = 0; i <= 99; i++) {
			int cnt = mongo.coll.find(
					(DBObject) JSON.parse("{ \"pmid\": {\"$gt\": \"" + i
							+ "\", \"$lt\": \"" + (i + 1) + "\" } }")).count();
			System.err.println(i + "\t" + cnt);
			cntSum += cnt;
		}
		System.err.println("sum " + cntSum);
	}