Skip to content

Commit

Permalink
Merge pull request #1762 from newrelic/setuserid-npe-fix
Browse files Browse the repository at this point in the history
NPE guard in AgentAttributeSender.removeAttribute()
  • Loading branch information
jtduffy authored Feb 26, 2024
2 parents 624ff4c + b819262 commit ae03a7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ protected Map<String, Object> getAttributeMap() {
}

public void removeAttribute(String key) {
getAttributeMap().remove(key);
Map<String, Object> attributeMap = getAttributeMap();
if (attributeMap == null) {
return;
}
attributeMap.remove(key);
}

}
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

0 comments on commit ae03a7c

Please sign in to comment.