Skip to content

Commit

Permalink
Fixing add unit tests to legacy/std http attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
meiao committed Dec 15, 2023
1 parent 6c144c1 commit 52408c3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ public SpanEventFactory setHttpStatusText(String statusText) {
filter.shouldIncludeAgentAttribute(appName, AttributeNames.HTTP_STATUS_TEXT)) {
builder.putAgentAttribute(AttributeNames.HTTP_STATUS_TEXT, statusText);
}
if (attributesConfig.isStandardHttpAttr() &&
filter.shouldIncludeAgentAttribute(appName, AttributeNames.HTTP_STATUS_TEXT)) {
if (attributesConfig.isLegacyHttpAttr() &&
filter.shouldIncludeAgentAttribute(appName, AttributeNames.HTTP_STATUS_MESSAGE)) {
builder.putAgentAttribute(AttributeNames.HTTP_STATUS_MESSAGE, statusText);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -113,6 +114,7 @@ public void shouldNotSetSpanErrorWhenFiltered() {

@Test
public void shouldSetHttpParameters() {
mockAttributeConfig(HttpAttrMode.BOTH);
HttpParameters mockParameters = mock(HttpParameters.class);
when(mockParameters.getLibrary()).thenReturn("library");
when(mockParameters.getProcedure()).thenReturn("procedure");
Expand All @@ -138,13 +140,34 @@ public void doesNotSetHttpAgentAttributesWhenFiltering() {

@Test
public void shouldSetStatusCode() {
mockAttributeConfig(HttpAttrMode.BOTH);
SpanEvent spanEvent = spanEventFactory.setHttpStatusCode(418).build();

assertEquals(418, spanEvent.getAgentAttributes().get("http.statusCode"));
assertEquals(418, spanEvent.getAgentAttributes().get("httpResponseCode"));
}

@Test
public void shouldSetStandardStatusCode() {
mockAttributeConfig(HttpAttrMode.STANDARD);
SpanEvent spanEvent = spanEventFactory.setHttpStatusCode(418).build();

assertEquals(418, spanEvent.getAgentAttributes().get("http.statusCode"));
assertNull(spanEvent.getAgentAttributes().get("httpResponseCode"));
}

@Test
public void shouldSetLegacyStatusCode() {
mockAttributeConfig(HttpAttrMode.LEGACY);
SpanEvent spanEvent = spanEventFactory.setHttpStatusCode(418).build();

assertNull(spanEvent.getAgentAttributes().get("http.statusCode"));
assertEquals(418, spanEvent.getAgentAttributes().get("httpResponseCode"));
}

@Test
public void shouldNotSetStatusCodeWhenFiltering() {
mockAttributeConfig(HttpAttrMode.BOTH);
SpanEventFactory factory = new SpanEventFactory("blerb", new PassNothingAttributeFilter(), DEFAULT_SYSTEM_TIMESTAMP_SUPPLIER);
SpanEvent spanEvent = factory.setHttpStatusCode(418).build();

Expand All @@ -160,24 +183,47 @@ public void shouldNotSetNullStatusCode() {

@Test
public void shouldSetStatusText() {
mockAttributeConfig(HttpAttrMode.BOTH);
SpanEvent spanEvent = spanEventFactory.setHttpStatusText("I'm a teapot.").build();

assertEquals("I'm a teapot.", spanEvent.getAgentAttributes().get("http.statusText"));
assertEquals("I'm a teapot.", spanEvent.getAgentAttributes().get("httpResponseMessage"));
}

@Test
public void shouldSetStandardStatusText() {
mockAttributeConfig(HttpAttrMode.STANDARD);
SpanEvent spanEvent = spanEventFactory.setHttpStatusText("I'm a teapot.").build();

assertEquals("I'm a teapot.", spanEvent.getAgentAttributes().get("http.statusText"));
assertNull(spanEvent.getAgentAttributes().get("httpResponseMessage"));
}

@Test
public void shouldSetLegacyStatusText() {
mockAttributeConfig(HttpAttrMode.LEGACY);
SpanEvent spanEvent = spanEventFactory.setHttpStatusText("I'm a teapot.").build();

assertNull(spanEvent.getAgentAttributes().get("http.statusText"));
assertEquals("I'm a teapot.", spanEvent.getAgentAttributes().get("httpResponseMessage"));
}

@Test
public void shouldNotSetStatusTextWhenFiltering() {
mockAttributeConfig(HttpAttrMode.BOTH);
SpanEventFactory factory = new SpanEventFactory("blerb", new PassNothingAttributeFilter(), DEFAULT_SYSTEM_TIMESTAMP_SUPPLIER);
SpanEvent spanEvent = factory.setHttpStatusText("I'm a teapot.").build();

assertNull(spanEvent.getAgentAttributes().get("http.statusText"));
assertNull(spanEvent.getAgentAttributes().get("httpResponseMessage"));
}

@Test
public void shouldNotSetNullStatusText() {
SpanEvent spanEvent = spanEventFactory.setHttpStatusText(null).build();

assertFalse(spanEvent.getAgentAttributes().containsKey("http.statusText"));
assertFalse(spanEvent.getAgentAttributes().containsKey("httpResponseMessage"));
}

@Test
Expand Down Expand Up @@ -278,4 +324,25 @@ public boolean shouldIncludeAgentAttribute(String appName, String attributeName)
return Collections.emptyMap();
}
}

/**
* These should never be both false.
*/
private void mockAttributeConfig(HttpAttrMode httpAttrMode) {
MockServiceManager serviceManager = new MockServiceManager();
AgentConfig agentConfig = mock(AgentConfig.class, RETURNS_DEEP_STUBS);
serviceManager.setConfigService(new MockConfigService(agentConfig));

when(agentConfig.getAttributesConfig().isStandardHttpAttr())
.thenReturn(httpAttrMode != HttpAttrMode.LEGACY);

when(agentConfig.getAttributesConfig().isLegacyHttpAttr())
.thenReturn(httpAttrMode != HttpAttrMode.STANDARD);
}

private enum HttpAttrMode {
BOTH,
STANDARD,
LEGACY,
}
}

0 comments on commit 52408c3

Please sign in to comment.