org.bson.json.JsonParseException Java Examples

The following examples show how to use org.bson.json.JsonParseException. 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: AggregateQueryProvider2Test.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  AggregateQuerySupportingRepositoryFactory factory = new AggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                    queryExecutor);
  QueryLookupStrategy lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example #2
Source File: ReactiveAggregateQueryProviderTest.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  ReactiveAggregateQuerySupportingRepositoryFactory factory = new ReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                                    queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Assert.isTrue(lookupStrategy.isPresent(), "Lookup strategy must not be null");
  Method method = ReactiveTestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(ReactiveTestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example #3
Source File: AggregateQueryProvider2Test.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  NonReactiveAggregateQuerySupportingRepositoryFactory factory =
      new NonReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations, queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);
  assertTrue(lookupStrategy.isPresent(), "Expecting non null lookupStrategy");
  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example #4
Source File: MongoSink.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void flush() {
    final List<Document> docsToInsert = new ArrayList<>();
    final List<Record<byte[]>> recordsToInsert;

    synchronized (this) {
        if (incomingList.isEmpty()) {
            return;
        }

        recordsToInsert = incomingList;
        incomingList = Lists.newArrayList();
    }

    final Iterator<Record<byte[]>> iter = recordsToInsert.iterator();

    while (iter.hasNext()) {
        final Record<byte[]> record = iter.next();

        try {
            final byte[] docAsBytes = record.getValue();
            final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
            docsToInsert.add(doc);
        }
        catch (JsonParseException | BSONException e) {
            log.error("Bad message", e);
            record.fail();
            iter.remove();
        }
    }

    if (docsToInsert.size() > 0) {
        collection.insertMany(docsToInsert).subscribe(new DocsToInsertSubscriber(docsToInsert,recordsToInsert));
    }
}
 
Example #5
Source File: JsonBsonDocumentReader.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
public BsonDocument readDocument(AllocationType allocationType, String source) throws
    BsonDocumentReaderException {
  try {
    org.bson.BsonDocument doc = org.bson.BsonDocument.parse(source);
    return MongoBsonTranslator.translate(doc);
  } catch (JsonParseException ex) {
    throw new BsonDocumentReaderException(ex);
  }
}
 
Example #6
Source File: DynamicMockRestController.java    From microcks with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/{service}/{version}/{resource}", method = RequestMethod.POST)
public ResponseEntity<String> createResource(
      @PathVariable("service") String serviceName,
      @PathVariable("version") String version,
      @PathVariable("resource") String resource,
      @RequestParam(value="delay", required=false) Long delay,
      @RequestBody(required=true) String body,
      HttpServletRequest request
) {
   log.debug("Creating a new resource '{}' for service '{}-{}'", resource, serviceName, version);
   long startTime = System.currentTimeMillis();

   serviceName = sanitizeServiceName(serviceName);

   MockContext mockContext = getMockContext(serviceName, version, "POST /" + resource);
   if (mockContext != null) {
      Document document = null;
      GenericResource genericResource = null;

      try {
         // Try parsing body payload that should be json.
         document = Document.parse(body);
         // Now create a generic resource.
         genericResource = new GenericResource();
         genericResource.setServiceId(mockContext.service.getId());
         genericResource.setPayload(document);

         genericResource = genericResourceRepository.save(genericResource);
      } catch (JsonParseException jpe) {
         // Return a 422 code : unprocessable entity.
         return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
      }

      // Append id and wait if specified before returning.
      document.append(ID_FIELD, genericResource.getId());
      waitForDelay(startTime, delay, mockContext);
      return new ResponseEntity<>(document.toJson(), HttpStatus.CREATED);
   }
   // Return a 400 code : bad request.
   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
 
Example #7
Source File: DynamicMockRestController.java    From microcks with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/{service}/{version}/{resource}/{resourceId}", method = RequestMethod.PUT)
public ResponseEntity<String> updateResource(
      @PathVariable("service") String serviceName,
      @PathVariable("version") String version,
      @PathVariable("resource") String resource,
      @PathVariable("resourceId") String resourceId,
      @RequestParam(value="delay", required=false) Long delay,
      @RequestBody(required=true) String body,
      HttpServletRequest request
) {
   log.debug("Update resource '{}:{}' for service '{}-{}'", resource, resourceId, serviceName, version);
   long startTime = System.currentTimeMillis();

   serviceName = sanitizeServiceName(serviceName);

   MockContext mockContext = getMockContext(serviceName, version, "PUT /" + resource + "/:id");
   if (mockContext != null) {
      // Get the requested generic resource.
      GenericResource genericResource = genericResourceRepository.findById(resourceId).orElse(null);
      if (genericResource != null) {
         Document document = null;

         try {
            // Try parsing body payload that should be json.
            document = Document.parse(body);
            document.remove(ID_FIELD);

            // Now update the generic resource payload.
            genericResource.setPayload(document);

            genericResourceRepository.save(genericResource);
         } catch (JsonParseException jpe) {
            // Return a 422 code : unprocessable entity.
            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
         }
         // Wait if specified before returning.
         waitForDelay(startTime, delay, mockContext);

         // Return the updated resource as well as a 200 code.
         return new ResponseEntity<>(transformToResourceJSON(genericResource), HttpStatus.OK);

      } else {
         // Wait if specified before returning.
         waitForDelay(startTime, delay, mockContext);

         // Return a 404 code : not found.
         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      }
   }

   // Return a 400 code : bad request.
   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}