Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE guard in AgentAttributeSender.removeAttribute() #1762

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ protected Map<String, Object> getAttributeMap() {
}

public void removeAttribute(String key) {
Map<String, Object> attributeMap = getAttributeMap();
meiao marked this conversation as resolved.
Show resolved Hide resolved
if (attributeMap == null) {
return;
}
getAttributeMap().remove(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,21 @@ public void setUserId_withInvalidId_doesNotSetAttribute() {
Mockito.verify(mockAgentSender, Mockito.times(0)).addAttribute("enduser.id", "123", "setUserId");
}

@Test
public void setUserId_withoutActiveTxn_isNoOp() {
mockOutServices();
AttributeSender mockSender = Mockito.mock(AttributeSender.class);
AgentAttributeSender mockAgentSender = new AgentAttributeSender();
NewRelicApiImplementation target = new NewRelicApiImplementation(mockSender, mockAgentSender);

try(MockedStatic<Transaction> mockTxn = Mockito.mockStatic(Transaction.class)) {
mockTxn.when(() -> Transaction.getTransaction(false)).thenReturn(null);
//These used to throw an NPE when there was no active transaction so the "assertion" is that these method calls execute without an exception
target.setUserId(null);
target.setUserId("");
}
}

@Test
public void setUserName_withValidName_setsNameAttribute() {
mockOutServices();
Expand Down
Loading