use of com.networknt.exception.ClientException in project light-codegen by networknt.
the class FrameworkListHandlerTest method testGenerator.
@Test
public void testGenerator() throws ClientException, ApiException, UnsupportedEncodingException {
String s = "{\"host\":\"lightapi.net\",\"service\":\"codegen\",\"action\":\"listFramework\",\"version\":\"0.0.1\"}";
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/api/json");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, s));
}
});
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("IOException: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
Assert.assertTrue(body.contains("swagger"));
}
}
use of com.networknt.exception.ClientException in project light-codegen by networknt.
the class GeneratorServiceHandlerTest method testInvalidFramework.
@Test
public void testInvalidFramework() throws ClientException, IOException {
Map<String, Object> params = new HashMap<>();
params.put("host", "lightapi.net");
params.put("service", "codegen");
params.put("action", "generate");
params.put("version", "0.0.1");
params.put("data", new ObjectMapper().readValue("[{\"framework\": \"invalid\", \"model\": {\"key\":\"value\"}, \"config\": {\"key\":\"value\"}}]", List.class));
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
try {
connection.getIoThread().execute(() -> {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/api/json");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.AUTHORIZATION, auth);
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
try {
connection.sendRequest(request, client.createClientCallback(reference, latch, new ObjectMapper().writeValueAsString(params)));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
});
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("IOException: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
Assert.assertTrue(body.contains("ERR11100"));
}
}
use of com.networknt.exception.ClientException in project light-codegen by networknt.
the class SchemaGetHandlerTest method testGetSchema.
@Test
public void testGetSchema() throws ClientException, ApiException, UnsupportedEncodingException {
String s = "{\"host\":\"lightapi.net\",\"service\":\"codegen\",\"action\":\"getSchema\",\"version\":\"0.0.1\",\"data\":{\"framework\":\"swagger\"}}";
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/api/json");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.AUTHORIZATION, auth);
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, s));
}
});
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("IOException: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
System.out.println("body = " + body);
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
Assert.assertTrue(body.contains("{"));
}
}
use of com.networknt.exception.ClientException in project light-portal by networknt.
the class UserPostHandler method verifyEmail.
public boolean verifyEmail(String email) throws Exception {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
System.out.println("rest URL:" + apibHost);
connection = client.connect(new URI(apibHost), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/v1/user/email?email=" + email).setMethod(Methods.GET);
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
System.out.println("response:" + body);
if (NO_USER_USER_EMAIL.equalsIgnoreCase(body)) {
return true;
} else {
return false;
}
}
use of com.networknt.exception.ClientException in project light-portal by networknt.
the class CreateFormTest method testCreateForm.
@Test
public void testCreateForm() throws ClientException, ApiException {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
FormRequest formRequest = new FormRequest("lightapi.net", "form", "createForm", "0.1.0");
Form form = new Form();
form.setDescription("test Form");
form.setVersion("1.0");
Action action = new Action("lightapi.net", "form", "createForm", "0.1.0", "POST");
form.setAction(action);
FormField formField = new FormField("Key_field", "text", false, "1");
form.addFormField(formField);
Property property = new Property("Key", "text", "Key_field", false, 1);
Schema schema = new Schema("FormSchema", "Form Schema");
schema.addProperties(property);
form.setSchema(schema);
formRequest.setData(form);
String json = JSonMapper.toJson(formRequest);
System.out.println("\n request json string: " + json);
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, json));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(200, statusCode);
Assert.assertNotNull(body);
}
Aggregations