Java Code Examples for io.vertx.core.json.Json#encodePrettily()

The following examples show how to use io.vertx.core.json.Json#encodePrettily() . 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: BeanTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeploymentDescriptor3() {
  int fail = 0;
  final String docSampleDeployment = "{" + LS
    + "  \"srvcId\" : \"sample-module-1\"," + LS
    + "  \"descriptor\" : {" + LS
    + "    \"exec\" : "
    + "\"java -Dport=%p -jar ../okapi-test-module/target/okapi-test-module-fat.jar\"" + LS
    + "  }" + LS
    + "}";

  try {
    final DeploymentDescriptor md = Json.decodeValue(docSampleDeployment,
      DeploymentDescriptor.class);
    String pretty = Json.encodePrettily(md);
    assertEquals(docSampleDeployment, pretty);
  } catch (DecodeException ex) {
    ex.printStackTrace();
    fail = 400;
  }
  assertEquals(0, fail);
}
 
Example 2
Source File: MyFirstVerticleTest.java    From introduction-to-eclipse-vertx with Apache License 2.0 6 votes vote down vote up
@Test
public void checkThatWeCanAdd(TestContext context) {
    Async async = context.async();
    final String json = Json.encodePrettily(new Article("Some title", "Some url"));
    vertx.createHttpClient().post(port, "localhost", "/api/articles")
        .putHeader("Content-Type", "application/json")
        .putHeader("Content-Length", Integer.toString(json.length()))
        .handler(response -> {
            context.assertEquals(response.statusCode(), 201);
            context.assertTrue(response.headers().get("content-type").contains("application/json"));
            response.bodyHandler(body -> {
                Article article = Json.decodeValue(body.toString(), Article.class);
                context.assertEquals(article.getTitle(), "Some title");
                context.assertEquals(article.getUrl(), "Some url");
                context.assertNotNull(article.getId());
                async.complete();
            });
        })
        .write(json)
        .end();
}
 
Example 3
Source File: MyFirstVerticleTest.java    From introduction-to-eclipse-vertx with Apache License 2.0 6 votes vote down vote up
@Test
public void checkThatWeCanAdd(TestContext context) {
    Async async = context.async();
    final String json = Json.encodePrettily(new Article("Some title", "Some url"));
    vertx.createHttpClient().post(port, "localhost", "/api/articles")
        .putHeader("Content-Type", "application/json")
        .putHeader("Content-Length", Integer.toString(json.length()))
        .handler(response -> {
            context.assertEquals(response.statusCode(), 201);
            context.assertTrue(response.headers().get("content-type").contains("application/json"));
            response.bodyHandler(body -> {
                Article article = Json.decodeValue(body.toString(), Article.class);
                context.assertEquals(article.getTitle(), "Some title");
                context.assertEquals(article.getUrl(), "Some url");
                context.assertNotNull(article.getId());
                async.complete();
            });
        })
        .write(json)
        .end();
}
 
Example 4
Source File: MyFirstVerticleTest.java    From df_data_service with Apache License 2.0 6 votes vote down vote up
@Test
public void checkThatWeCanAdd(TestContext context) {
  Async async = context.async();
  final String json = Json.encodePrettily(new DFJobPOPJ("Jameson", "Ireland","Register"));
  vertx.createHttpClient().post(port, "localhost", "/api/df")
      .putHeader("content-type", "application/json")
      .putHeader("content-length", Integer.toString(json.length()))
      .handler(response -> {
        context.assertEquals(response.statusCode(), 201);
        context.assertTrue(response.headers().get("content-type").contains("application/json"));
        response.bodyHandler(body -> {
          final DFJobPOPJ DFJob = Json.decodeValue(body.toString(), DFJobPOPJ.class);
          context.assertEquals(DFJob.getName(), "Jameson");
          context.assertEquals(DFJob.getConnectUid(), "Ireland");
          context.assertNotNull(DFJob.getId());
          async.complete();
        });
      })
      .write(json)
      .end();
}
 
Example 5
Source File: BeanTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeploymentDescriptor2() {
  int fail = 0;
  final String docSampleDeployment = "{" + LS
    + "  \"srvcId\" : \"sample-module-1\"," + LS
    + "  \"descriptor\" : {" + LS
    + "    \"exec\" : "
    + "\"java -Dport=%p -jar ../okapi-test-module/target/okapi-test-module-fat.jar\"," + LS
    + "    \"env\" : [ {" + LS
    + "      \"name\" : \"helloGreeting\"" + LS
    + "    } ]" + LS
    + "  }" + LS
    + "}";

  try {
    final DeploymentDescriptor md = Json.decodeValue(docSampleDeployment,
      DeploymentDescriptor.class);
    String pretty = Json.encodePrettily(md);
    assertEquals(docSampleDeployment, pretty);
  } catch (DecodeException ex) {
    ex.printStackTrace();
    fail = 400;
  }
  assertEquals(0, fail);
}
 
Example 6
Source File: BeanTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeploymentDescriptor1() {
  int fail = 0;
  final String docSampleDeployment = "{" + LS
    + "  \"srvcId\" : \"sample-module-1\"," + LS
    + "  \"descriptor\" : {" + LS
    + "    \"exec\" : "
    + "\"java -Dport=%p -jar ../okapi-test-module/target/okapi-test-module-fat.jar\"," + LS
    + "    \"env\" : [ {" + LS
    + "      \"name\" : \"helloGreeting\"," + LS
    + "      \"value\" : \"hej\"" + LS
    + "    } ]" + LS
    + "  }" + LS
    + "}";

  try {
    final DeploymentDescriptor md = Json.decodeValue(docSampleDeployment,
      DeploymentDescriptor.class);
    String pretty = Json.encodePrettily(md);
    assertEquals(docSampleDeployment, pretty);
  } catch (DecodeException ex) {
    ex.printStackTrace();
    fail = 400;
  }
  assertEquals(0, fail);
}
 
Example 7
Source File: MongoUtil.java    From okapi with Apache License 2.0 6 votes vote down vote up
public void add(T env, String id, Handler<ExtendedAsyncResult<Void>> fut) {
  JsonObject jq = new JsonObject().put("_id", id);
  String s = Json.encodePrettily(env);
  JsonObject document = new JsonObject(s);
  encode(document, null); // _id can not be put for Vert.x 3.5.1
  UpdateOptions options = new UpdateOptions().setUpsert(true);
  cli.updateCollectionWithOptions(collection, jq,
      new JsonObject().put("$set", document), options, res -> {
        if (res.succeeded()) {
          fut.handle(new Success<>());
        } else {
          logger.warn("MongoUtil.add {} failed: {}", id, res.cause().getMessage());
          logger.warn("Document: {}", document.encodePrettily());
          fut.handle(new Failure<>(ErrorType.INTERNAL, res.cause()));
        }
      });
}
 
Example 8
Source File: JsonRpcHttpService.java    From besu with Apache License 2.0 5 votes vote down vote up
private String serialize(final JsonRpcResponse response) {

    if (response.getType() == JsonRpcResponseType.NONE) {
      return EMPTY_RESPONSE;
    }

    return Json.encodePrettily(response);
  }
 
Example 9
Source File: BeanTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeploymentDescriptor4() {
  int fail = 0;
  final String docSampleDeployment = "{" + LS
    + "  \"srvcId\" : \"sample-module-1\"," + LS
    + "  \"descriptor\" : {" + LS
    + "    \"dockerImage\" : \"my-image\"," + LS
    + "    \"dockerArgs\" : {" + LS
    + "      \"Hostname\" : \"localhost\"," + LS
    + "      \"User\" : \"nobody\"" + LS
    + "    }," + LS
    + "    \"dockerCMD\" : [ \"a\", \"b\" ]" + LS
    + "  }" + LS
    + "}";

  try {
    final DeploymentDescriptor md = Json.decodeValue(docSampleDeployment,
      DeploymentDescriptor.class);
    assertEquals("b", md.getDescriptor().getDockerCmd()[1]);
    String pretty = Json.encodePrettily(md);
    assertEquals(docSampleDeployment, pretty);
  } catch (DecodeException ex) {
    ex.printStackTrace();
    fail = 400;
  }
  assertEquals(0, fail);
}
 
Example 10
Source File: BeanTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testModuleDescriptorTimers2() {
  int fail = 0;

  final String docModuleDescriptor = "{" + LS
    + "  \"id\" : \"sample-module-1\"," + LS
    + "  \"provides\" : [ {" + LS
    + "    \"id\" : \"_timer\"," + LS
    + "    \"version\" : \"1.0\"," + LS
    + "    \"handlers\" : [ {" + LS
    + "      \"methods\" : [ \"GET\" ]," + LS
    + "      \"pathPattern\" : \"/test\"," + LS
    + "      \"delay\" : \"1\"," + LS
    + "      \"unit\" : \"second1\"" + LS
    + "    } ]" + LS
    + "  } ]" + LS
    + "}";

  try {
    final ModuleDescriptor md = Json.decodeValue(docModuleDescriptor,
      ModuleDescriptor.class);
    String pretty = Json.encodePrettily(md);
  } catch (DecodeException ex) {
    fail = 400;
  }
  assertEquals(400, fail);
}
 
Example 11
Source File: GraphQLHttpService.java    From besu with Apache License 2.0 5 votes vote down vote up
private String serialise(final GraphQLResponse response) {

    if (response.getType() == GraphQLResponseType.NONE) {
      return EMPTY_RESPONSE;
    }

    return Json.encodePrettily(response.getResult());
  }
 
Example 12
Source File: DFAPIMessage.java    From df_data_service with Apache License 2.0 4 votes vote down vote up
public static String getCustomizedResponseMessage(String responseKey, String responseVal) {
    return Json.encodePrettily(new JsonObject().put(responseKey, responseVal));
}
 
Example 13
Source File: TenantManager.java    From okapi with Apache License 2.0 4 votes vote down vote up
private void invokePermissionsForModule(Tenant tenant, ModuleDescriptor mdTo,
                                        ModuleDescriptor permsModule, ProxyContext pc,
                                        Handler<ExtendedAsyncResult<Void>> fut) {

  pc.debug("Loading permissions for " + mdTo.getName()
      + " (using " + permsModule.getName() + ")");
  String moduleTo = mdTo.getId();
  PermissionList pl = null;
  InterfaceDescriptor permInt = permsModule.getSystemInterface("_tenantPermissions");
  if (permInt.getVersion().equals("1.0")) {
    pl = new PermissionList(moduleTo, mdTo.getPermissionSets());
  } else {
    pl = new PermissionList(moduleTo, mdTo.getExpandedPermissionSets());
  }
  String pljson = Json.encodePrettily(pl);
  pc.debug("tenantPerms Req: " + pljson);
  String permPath = "";
  List<RoutingEntry> routingEntries = permInt.getAllRoutingEntries();
  ModuleInstance permInst = null;
  if (!routingEntries.isEmpty()) {
    for (RoutingEntry re : routingEntries) {
      if (re.match(null, "POST")) {
        permPath = re.getStaticPath();
        permInst = new ModuleInstance(permsModule, re, permPath, HttpMethod.POST, true);
      }
    }
  }
  if (permInst == null) {
    fut.handle(new Failure<>(ErrorType.USER,
        "Bad _tenantPermissions interface in module " + permsModule.getId()
            + ". No path to POST to"));
    return;
  }
  pc.debug("tenantPerms: " + permsModule.getId() + " and " + permPath);
  proxyService.callSystemInterface(tenant, permInst,
      pljson, pc, cres -> {
        if (cres.failed()) {
          fut.handle(new Failure<>(ErrorType.USER, cres.cause()));
        } else {
          pc.passOkapiTraceHeaders(cres.result());
          pc.debug("tenantPerms request to " + permsModule.getName()
              + " succeeded for module " + moduleTo + " and tenant " + tenant.getId());
          fut.handle(new Success<>());
        }
      });
}
 
Example 14
Source File: DFAPIMessage.java    From df_data_service with Apache License 2.0 4 votes vote down vote up
public static String getResponseMessage(int responseCode, String comments) {
    return Json.encodePrettily(getResponseJsonObj(responseCode, comments, ""));
}
 
Example 15
Source File: ResourceResponse.java    From vertx-swagger with Apache License 2.0 4 votes vote down vote up
public String toJson() {
    if(response == null || response instanceof Void) return null;
    if(response instanceof JsonObject) return ((JsonObject) response).encodePrettily();
    if(response instanceof JsonArray) return ((JsonArray) response).encodePrettily();
    return Json.encodePrettily(response);
}
 
Example 16
Source File: ResourceResponse.java    From vertx-swagger with Apache License 2.0 4 votes vote down vote up
public String toJson() {
    if(response == null || response instanceof Void) return null;
    if(response instanceof JsonObject) return ((JsonObject) response).encodePrettily();
    if(response instanceof JsonArray) return ((JsonArray) response).encodePrettily();
    return Json.encodePrettily(response);
}
 
Example 17
Source File: BeanTest.java    From okapi with Apache License 2.0 4 votes vote down vote up
@Test
public void testModuleDescriptorTimers() {
  int fail = 0;

  final String docModuleDescriptor = "{" + LS
    + "  \"id\" : \"sample-module-1\"," + LS
    + "  \"provides\" : [ {" + LS
    + "    \"id\" : \"_timer\"," + LS
    + "    \"version\" : \"1.0\"," + LS
    + "    \"handlers\" : [ {" + LS
    + "      \"methods\" : [ \"GET\" ]," + LS
    + "      \"pathPattern\" : \"/test\"," + LS
    + "      \"delay\" : \"1\"," + LS
    + "      \"unit\" : \"second\"" + LS
    + "    }, {" + LS
    + "      \"methods\" : [ \"GET\" ]," + LS
    + "      \"pathPattern\" : \"/test\"," + LS
    + "      \"delay\" : \"1\"," + LS
    + "      \"unit\" : \"minute\"" + LS
    + "    }, {" + LS
    + "      \"methods\" : [ \"GET\" ]," + LS
    + "      \"pathPattern\" : \"/test\"," + LS
    + "      \"delay\" : \"1\"," + LS
    + "      \"unit\" : \"hour\"" + LS
    + "    }, {" + LS
    + "      \"methods\" : [ \"GET\" ]," + LS
    + "      \"pathPattern\" : \"/test\"," + LS
    + "      \"delay\" : \"1\"," + LS
    + "      \"unit\" : \"day\"" + LS
    + "    }, {" + LS
    + "      \"methods\" : [ \"GET\" ]," + LS
    + "      \"pathPattern\" : \"/test\"," + LS
    + "      \"delay\" : \"1\"" + LS
    + "    } ]" + LS
    + "  } ]" + LS
    + "}";

  try {
    final ModuleDescriptor md = Json.decodeValue(docModuleDescriptor,
      ModuleDescriptor.class);
    String pretty = Json.encodePrettily(md);
  } catch (DecodeException ex) {
    ex.printStackTrace();
    fail = 400;
  }
  assertEquals(0, fail);
}
 
Example 18
Source File: ResourceResponse.java    From vertx-swagger with Apache License 2.0 4 votes vote down vote up
public String toJson() {
    if(response == null || response instanceof Void) return null;
    if(response instanceof JsonObject) return ((JsonObject) response).encodePrettily();
    if(response instanceof JsonArray) return ((JsonArray) response).encodePrettily();
    return Json.encodePrettily(response);
}
 
Example 19
Source File: BeanTest.java    From okapi with Apache License 2.0 4 votes vote down vote up
@Test
public void testModuleDescriptor1() {
  int fail = 0;

  final String docModuleDescriptor = "{" + LS
    + "  \"id\" : \"sample-module-1\"," + LS
    + "  \"name\" : \"sample module\"," + LS
    + "  \"provides\" : [ {" + LS
    + "    \"id\" : \"sample\"," + LS
    + "    \"version\" : \"1.0\"," + LS
    + "    \"handlers\" : [ {" + LS
    + "      \"methods\" : [ \"GET\", \"POST\" ]," + LS
    + "      \"pathPattern\" : \"/users/{id}\"," + LS
    + "      \"level\" : \"30\"," + LS
    + "      \"type\" : \"request-response\"," + LS
    + "      \"permissionsRequired\" : [ \"sample.needed\" ]," + LS
    + "      \"permissionsDesired\" : [ \"sample.extra\" ]," + LS
    + "      \"modulePermissions\" : [ \"sample.modperm\" ]" + LS
    + "    } ]" + LS
    + "  }, {" + LS
    + "    \"id\" : \"_tenant\"," + LS
    + "    \"version\" : \"1.0\"," + LS
    + "    \"interfaceType\" : \"system\"," + LS
    + "    \"handlers\" : [ {" + LS
    + "      \"methods\" : [ \"POST\", \"DELETE\" ]," + LS
    + "      \"path\" : \"/_/tenant\"," + LS
    + "      \"level\" : \"10\"," + LS
    + "      \"type\" : \"system\"" + LS
    + "    } ]" + LS
    + "  } ]," + LS
    + "  \"optional\" : [ {" + LS
    + "    \"id\" : \"foo\"," + LS
    + "    \"version\" : \"1.0\"" + LS
    + "  } ]," + LS
    + "  \"env\" : [ {" + LS
    + "    \"name\" : \"DB_HOST\"," + LS
    + "    \"value\" : \"localhost\"" + LS
    + "  } ]," + LS
    + "  \"metadata\" : {" + LS
    + "    \"scm\" : \"https://github.com/folio-org/mod-something\"," + LS
    + "    \"language\" : \"java\"" + LS
    + "  }," + LS
    + "  \"replaces\" : [ \"old-module\", \"other-module\" ]" + LS
    + "}";

  try {
    final ModuleDescriptor md = Json.decodeValue(docModuleDescriptor,
      ModuleDescriptor.class);
    String pretty = Json.encodePrettily(md);
    assertEquals(docModuleDescriptor, pretty);
  } catch (DecodeException ex) {
    ex.printStackTrace();
    fail = 400;
  }
  assertEquals(0, fail);
}
 
Example 20
Source File: TimeoutHandlerTest.java    From besu with Apache License 2.0 4 votes vote down vote up
private static String body(final RpcMethod method) {
  return Json.encodePrettily(new JsonRpcRequest("2.0", method.getMethodName(), null));
}