From c0b07226995dec45ce601418fa13a88a3aa68954 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Wed, 14 Sep 2022 17:24:27 +0200 Subject: [PATCH] Some assertions refactoring (#6618) --- .../graphql/AbstractGraphqlTest.java | 16 ++++------------ .../junit/http/AbstractHttpClientTest.java | 19 +++++++++---------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/instrumentation/graphql-java-12.0/testing/src/main/java/io/opentelemetry/instrumentation/graphql/AbstractGraphqlTest.java b/instrumentation/graphql-java-12.0/testing/src/main/java/io/opentelemetry/instrumentation/graphql/AbstractGraphqlTest.java index 4ec8189b35d5..b6fd174aaa56 100644 --- a/instrumentation/graphql-java-12.0/testing/src/main/java/io/opentelemetry/instrumentation/graphql/AbstractGraphqlTest.java +++ b/instrumentation/graphql-java-12.0/testing/src/main/java/io/opentelemetry/instrumentation/graphql/AbstractGraphqlTest.java @@ -209,12 +209,8 @@ void parseError() { assertThat(attrs) .containsEntry("exception.type", "InvalidSyntax"); String message = - String.valueOf( - attrs - .asMap() - .get( - AttributeKey.stringKey( - "exception.message"))); + attrs.get( + AttributeKey.stringKey("exception.message")); assertThat(message).startsWith("Invalid Syntax"); })))); } @@ -256,12 +252,8 @@ void validationError() { .containsEntry( "exception.type", "ValidationError"); String message = - String.valueOf( - attrs - .asMap() - .get( - AttributeKey.stringKey( - "exception.message"))); + attrs.get( + AttributeKey.stringKey("exception.message")); assertThat(message) .startsWith( "Validation error of type FieldUndefined"); diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java index 77183b3df044..2a8596b20db5 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java @@ -921,10 +921,10 @@ SpanDataAssert assertClientSpan( if (uri.getPort() == PortUtils.UNUSABLE_PORT || uri.getHost().equals("192.0.2.1")) { // TODO: net.peer.name and net.peer.port should always be populated from the URI or // the Host header, verify these assertions below - if (attrs.asMap().containsKey(SemanticAttributes.NET_PEER_NAME)) { + if (attrs.get(SemanticAttributes.NET_PEER_NAME) != null) { assertThat(attrs).containsEntry(SemanticAttributes.NET_PEER_NAME, uri.getHost()); } - if (attrs.asMap().containsKey(SemanticAttributes.NET_PEER_PORT)) { + if (attrs.get(SemanticAttributes.NET_PEER_PORT) != null) { if (uri.getPort() > 0) { assertThat(attrs) .containsEntry(SemanticAttributes.NET_PEER_PORT, (long) uri.getPort()); @@ -945,13 +945,12 @@ SpanDataAssert assertClientSpan( // In these cases the peer connection is not established, so the HTTP client should // not report any socket-level attributes - // TODO https://github.com/open-telemetry/opentelemetry-java/pull/4723 - assertThat(attrs.asMap()) - .doesNotContainKey(AttributeKey.stringKey("net.sock.family")) + assertThat(attrs) + .doesNotContainKey("net.sock.family") // TODO netty sometimes reports net.sock.peer.addr in connection error test - // .doesNotContainKey(AttributeKey.stringKey("net.sock.peer.addr")) - .doesNotContainKey(AttributeKey.stringKey("net.sock.peer.name")) - .doesNotContainKey(AttributeKey.stringKey("net.sock.peer.port")); + // .doesNotContainKey("net.sock.peer.addr") + .doesNotContainKey("net.sock.peer.name") + .doesNotContainKey("net.sock.peer.port"); } else { if (httpClientAttributes.contains(SemanticAttributes.NET_PEER_NAME)) { @@ -962,11 +961,11 @@ SpanDataAssert assertClientSpan( } // TODO: Move to test knob rather than always treating as optional - if (attrs.asMap().containsKey(AttributeKey.stringKey("net.sock.peer.addr"))) { + if (attrs.get(AttributeKey.stringKey("net.sock.peer.addr")) != null) { assertThat(attrs) .containsEntry(AttributeKey.stringKey("net.sock.peer.addr"), "127.0.0.1"); } - if (attrs.asMap().containsKey(AttributeKey.stringKey("net.sock.peer.port"))) { + if (attrs.get(AttributeKey.stringKey("net.sock.peer.port")) != null) { assertThat(attrs) .containsEntry( AttributeKey.longKey("net.sock.peer.port"),