YamasJsonUtil { public static YamasArrayJsonModel toYamasArray(Collection<MetricsPacket> metricsPackets) { YamasArrayJsonModel yamasArray = toYamasArray(metricsPackets, false); yamasArray.metrics.stream().findFirst().map(YamasJsonModel::getYamasConsumers) .ifPresent(consumers -> yamasArray.add(getStatusYamasModel("Data collected successfully", 0, consumers))); return yamasArray; }  static MetricsPacket.Builder toMetricsPacketBuilder(YamasJsonModel jsonModel); static YamasArrayJsonModel toYamasArray(Collection<MetricsPacket> metricsPackets); static YamasArrayJsonModel toYamasArray(Collection<MetricsPacket> metricsPackets, boolean addStatus); static List<MetricsPacket.Builder> toMetricsPackets(String jsonString);  }
@Test public void json_model_gets_null_status_by_default() { MetricsPacket packet = new MetricsPacket.Builder(toServiceId("foo")) .build(); YamasJsonModel jsonModel = YamasJsonUtil.toYamasArray(singleton(packet)).metrics.get(0); assertNull(jsonModel.status_code); assertNull(jsonModel.status_msg); }
@Test public void status_is_included_in_json_model_when_explicitly_asked_for() { MetricsPacket packet = new MetricsPacket.Builder(toServiceId("foo")) .build(); YamasJsonModel jsonModel = YamasJsonUtil.toYamasArray(singleton(packet), true).metrics.get(0); assertNotNull(jsonModel.status_code); assertNotNull(jsonModel.status_msg); }
@Test public void timestamp_0_in_packet_is_translated_to_null_in_json_model() { MetricsPacket packet = new MetricsPacket.Builder(toServiceId("foo")) .timestamp(0L) .build(); YamasJsonModel jsonModel = YamasJsonUtil.toYamasArray(singleton(packet)).metrics.get(0); assertNull(jsonModel.timestamp); }
@Test public void empty_consumers_is_translated_to_null_routing_in_json_model() { MetricsPacket packet = new MetricsPacket.Builder(toServiceId("foo")) .build(); YamasJsonModel jsonModel = YamasJsonUtil.toYamasArray(singleton(packet)).metrics.get(0); assertNull(jsonModel.routing); }
@Test public void default_public_consumer_is_filtered_from_yamas_routing() { MetricsPacket packet = new MetricsPacket.Builder(toServiceId("foo")) .addConsumers(Set.of(vespaMetricsConsumerId, defaultMetricsConsumerId)) .build(); YamasJsonModel jsonModel = YamasJsonUtil.toYamasArray(singleton(packet)).metrics.get(0); List<String> namespaces = jsonModel.routing.get(YAMAS_ROUTING).namespaces; assertEquals(1, namespaces.size()); assertEquals(vespaMetricsConsumerId.id, namespaces.get(0)); }
@Test public void only_default_public_consumer_yields_null_routing_in_json_model() { MetricsPacket packet = new MetricsPacket.Builder(toServiceId("foo")) .addConsumers(Set.of(defaultMetricsConsumerId)) .build(); YamasJsonModel jsonModel = YamasJsonUtil.toYamasArray(singleton(packet)).metrics.get(0); assertNull(jsonModel.routing); }