/** * Reads the readConfig file, which can be a classpath or file system resource. * * @param serviceName the name of the service to load * @return service config */ public static MicroserviceConfig readConfig(final String serviceName) { final Logger logger = LoggerFactory.getLogger(MicroserviceConfig.class); if (new File(resourceLocation).exists()) { final String json = IO.read(resourceLocation); return JsonFactory.fromJson(json, MicroserviceConfig.class); } else if (resourceLocationEnv != null && new File(resourceLocationEnv).exists()) { final String json = IO.read(resourceLocationEnv); return JsonFactory.fromJson(json, MicroserviceConfig.class); } else { logger.info("Reading config from classpath as it is not found on file system"); final String qbitEnv = System.getenv("QBIT_ENV"); final String resourceLocationOnClasspath = qbitEnv != null && !qbitEnv.isEmpty() ? serviceName + "_" + qbitEnv + ".json" : serviceName + ".json"; final String json = IO.read(Thread.currentThread().getContextClassLoader() .getResourceAsStream(resourceLocationOnClasspath)); return JsonFactory.fromJson(json, MicroserviceConfig.class); } }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HTTP.Response response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@Test public void testReturnEmployee() { final HttpTextResponse httpResponse = httpServerSimulator.get("/es/returnemployee"); assertEquals(200, httpResponse.code()); Employee employee = JsonFactory.fromJson(httpResponse.body(), Employee.class); assertEquals(1, employee.getId()); assertEquals("Rick", employee.getName()); }
@Test public void testReturnEmployeeCaseDoesNotMatter() { final HttpTextResponse httpResponse = httpServerSimulator.get("/es/Returnemployee"); assertEquals(200, httpResponse.code()); Employee employee = JsonFactory.fromJson(httpResponse.body(), Employee.class); assertEquals(1, employee.getId()); assertEquals("Rick", employee.getName()); }
@Test public void returnEmployeeCallback() { final HttpTextResponse httpResponse = httpServerSimulator.get("/es/returnEmployeeCallback"); assertEquals(200, httpResponse.code()); Employee employee = JsonFactory.fromJson(httpResponse.body(), Employee.class); assertEquals(1, employee.getId()); assertEquals("Rick", employee.getName()); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HttpTextResponse response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
@Test public void testMany() { Adder adder = new Adder(); ServiceQueue serviceQueue = Services.jsonService("test", adder); ReceiveQueue<Response<Object>> responses = serviceQueue.responses(); SendQueue<MethodCall<Object>> requests = serviceQueue.requests(); //noinspection unchecked requests.sendMany(MethodCallBuilder.method("add", "[1,2]"), MethodCallBuilder.method("add", "[4,5]")); Response<Object> response = responses.take(); Object o = fromJson(response.body().toString()); ok = o.equals(Integer.valueOf(3)) || die(response); response = responses.take(); ok = response != null || die(response); o = fromJson(response.body().toString()); ok = o.equals(Integer.valueOf(9)) || die(response); synchronized (adder) { ok = adder.all == 12 || die(adder.all); } }
@Test public void test() { Adder adder = new Adder(); ServiceQueue serviceQueue = Services.jsonService("test", adder); ReceiveQueue<Response<Object>> responses = serviceQueue.responses(); SendQueue<MethodCall<Object>> requests = serviceQueue.requests(); requests.send(MethodCallBuilder.method("add", "[1,2]")); requests.send(MethodCallBuilder.method("add", "[4,5]")); requests.flushSends(); Response<Object> response = responses.take(); Object o = fromJson(response.body().toString()); ok = o.equals(Integer.valueOf(3)) || die(response); response = responses.take(); ok = response != null || die(response); o = fromJson(response.body().toString()); ok = o.equals(Integer.valueOf(9)) || die(response); synchronized (adder) { ok = adder.all == 12 || die(adder.all); } }
@Test public void testBatch() { Adder adder = new Adder(); ServiceQueue serviceQueue = Services.jsonService("test", adder); ReceiveQueue<Response<Object>> responses = serviceQueue.responses(); SendQueue<MethodCall<Object>> requests = serviceQueue.requests(); List<MethodCall<Object>> methods = Lists.list(MethodCallBuilder.method("add", "[1,2]"), MethodCallBuilder.method("add", "[4,5]")); requests.sendBatch(methods); Response<Object> response = responses.take(); ok = response != null || die(response); Object o = fromJson(response.body().toString()); ok = o.equals(Integer.valueOf(3)) || die(response); response = responses.take(); ok = response != null || die(response); o = fromJson(response.body().toString()); ok = o.equals(Integer.valueOf(9)) || die(response); synchronized (adder) { ok = adder.all == 12 || die(adder.all); } }
@Test public void getEmployee() { httpServerSimulator.postBody("/hr/department/100/", new Department(100, Lists.list(new Employee(1, "Rick"), new Employee(2, "Diana")))); HttpTextResponse httpResponse = httpServerSimulator.get("/hr/department/100/employee/2"); assertEquals(200, httpResponse.code()); Employee employee = JsonFactory.fromJson(httpResponse.body(), Employee.class); assertEquals(2, employee.getId()); assertEquals("Diana", employee.getName()); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HTTP.Response response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
/** * Retrieves the host/port of the Consul leader. * <p> * GET /v1/status/leader * * @return The host/port of the leader. */ public String getLeader() { final URI uri = createURI("/leader"); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(null, null, RequestOptions.BLANK, ""); final HTTP.Response httpResponse = HTTP.getResponse(uri + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to retrieve the leader", uri, httpResponse.code(), httpResponse.body()); } return fromJson(httpResponse.body(), String.class).replace("\"", "").trim(); }
/** * <p> * Retrieves the Agent's configuration and member information. * </p> * GET /v1/agent/self * * @return The Agent information. */ public AgentInfo getAgentInfo() { final URI uri = createURI("/self"); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error getting info about this agent", uri, response.status(), response.statusMessageAsString(), response.payloadAsString()); } return fromJson(response.payloadAsString(), AgentInfo.class); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HttpTextResponse response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }