StatusMapper { @Override public String toString() { if (status == null) return ""; String header = "# HELP datashare The datashare resources status\n" + String.format("# TYPE %s gauge\n", metricName); List<Field> declaredFields = Arrays.stream(this.status.getClass().getDeclaredFields()).filter(f -> !f.getName().startsWith("this")).collect(Collectors.toList()); StringBuilder fieldLines = new StringBuilder(); for (Field field : declaredFields) { try { Object value = field.get(status); Object numberValue = value; String status = ""; if (String.class.equals(field.getType())) { numberValue = "Nan"; } else if (boolean.class.equals(field.getType()) || Boolean.class.equals(field.getType())) { numberValue = (Boolean) value ? 1 : 0; status = String.format("status=\"%s\",", (Boolean) value ? "OK" : "KO"); } fieldLines.append(String.format("%s{%s%sresource=\"%s\"} %s %d\n", metricName, environment, status, field.getName(), numberValue, DatashareTime.getInstance().currentTimeMillis())); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } return header + fieldLines; } StatusMapper(String metricName, Object status);  StatusMapper(String metricName, Object status, String environment); @Override String toString();  }
@Test public void test_null() { assertThat(new StatusMapper(null, null).toString()).isEmpty(); }
@Test public void test_to_string_one_string_field() { class StringStatus { String field = "value";} assertThat(new StatusMapper("metric_name", new StringStatus()).toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{resource=\"field\"} Nan 1593531060000\n"); }
@Test public void test_to_string_one_int_field() { class IntStatus { int field = 123;} assertThat(new StatusMapper("metric_name", new IntStatus()).toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{resource=\"field\"} 123 1593531060000\n"); }
@Test public void test_to_string_one_Integer_field() { class IntStatus { Integer field = 456;} assertThat(new StatusMapper("metric_name", new IntStatus()).toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{resource=\"field\"} 456 1593531060000\n"); }
@Test public void test_to_string_with_platform() { class StringStatus { String field = "value";} assertThat(new StatusMapper("metric_name", new StringStatus(), "platform").toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{environment=\"platform\",resource=\"field\"} Nan 1593531060000\n"); }
@Test public void test_to_string_one_boolean_field() { class BooleanStatus { boolean field = true;} assertThat(new StatusMapper("metric_name", new BooleanStatus()).toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{status=\"OK\",resource=\"field\"} 1 1593531060000\n"); }
@Test public void test_to_string_one_Boolean_field() { class BooleanStatus { Boolean field = false;} assertThat(new StatusMapper("metric_name", new BooleanStatus()).toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{status=\"KO\",resource=\"field\"} 0 1593531060000\n"); }
@Test public void test_to_string_two_fields() { class BooleanStatus { Boolean field1 = true;Integer field2 = 3; } assertThat(new StatusMapper("metric_name", new BooleanStatus()).toString()).contains("" + "# HELP datashare The datashare resources status\n" + "# TYPE metric_name gauge\n" + "metric_name{status=\"OK\",resource=\"field1\"} 1 1593531060000\n" + "metric_name{resource=\"field2\"} 3 1593531060000\n"); }