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

Fix npe of audit log get operator #5153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -43,7 +43,7 @@ public ApolloAuditTracer(ApolloAuditScopeManager manager,
this.operatorSupplier = operatorSupplier;
}

public ApolloAuditScopeManager scopeManager() {
protected ApolloAuditScopeManager scopeManager() {
return manager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.ctrip.framework.apollo.audit.spi.defaultimpl;

import com.ctrip.framework.apollo.audit.constants.ApolloAuditConstants;
import com.ctrip.framework.apollo.audit.context.ApolloAuditSpan;
import com.ctrip.framework.apollo.audit.context.ApolloAuditTracer;
import com.ctrip.framework.apollo.audit.spi.ApolloAuditOperatorSupplier;
import org.springframework.web.context.request.RequestAttributes;
Expand All @@ -31,7 +32,8 @@ public String getOperator() {
Object tracer = requestAttributes.getAttribute(ApolloAuditConstants.TRACER,
RequestAttributes.SCOPE_REQUEST);
if (tracer != null) {
return ((ApolloAuditTracer) tracer).scopeManager().activeSpan().operator();
ApolloAuditSpan activeSpan = ((ApolloAuditTracer) tracer).getActiveSpan();
return activeSpan != null ? activeSpan.operator() : "anonymous";
BlackBear2003 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.audit.spi;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.ctrip.framework.apollo.audit.constants.ApolloAuditConstants;
import com.ctrip.framework.apollo.audit.context.ApolloAuditSpan;
import com.ctrip.framework.apollo.audit.context.ApolloAuditSpanContext;
import com.ctrip.framework.apollo.audit.context.ApolloAuditTracer;
import com.ctrip.framework.apollo.audit.spi.defaultimpl.ApolloAuditOperatorDefaultSupplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

@SpringBootTest
@ContextConfiguration(classes = ApolloAuditOperatorSupplier.class)
public class ApolloAuditOperatorSupplierTest {

@SpyBean
ApolloAuditOperatorDefaultSupplier defaultSupplier;

@MockBean
RequestAttributes requestAttributes;
@MockBean
ApolloAuditTracer tracer;

@BeforeEach
public void setUp() {
Mockito.when(requestAttributes.getAttribute(
Mockito.eq(ApolloAuditConstants.TRACER),
Mockito.eq(RequestAttributes.SCOPE_REQUEST))
).thenReturn(tracer);
RequestContextHolder.setRequestAttributes(requestAttributes);
}

@Test
public void testGetOperatorCaseActiveSpanExist() {
final String operator = "test";
{
ApolloAuditSpan activeSpan = new ApolloAuditSpan();
activeSpan.setContext(new ApolloAuditSpanContext(null, null, operator, null, null));
Mockito.when(tracer.getActiveSpan()).thenReturn(activeSpan);
}

assertEquals(operator, defaultSupplier.getOperator());
}

@Test
public void testGetOperatorCaseActiveSpanNotExist() {
assertEquals("anonymous", defaultSupplier.getOperator());
}

}
Loading