Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Oct 22, 2024
1 parent 848f5e7 commit f613346
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class MethodHookConfigurationResolver {
public MethodHookConfiguration resolve(
MethodDescription method, ClassInstrumentationConfiguration classConfig) {

// Why the hell steht hier null???

Set<InstrumentationRule> matchedRules =
classConfig.activeRules().stream()
.filter(rule -> rule.methodMatcher().matches(method))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.bytebuddy.matcher.ElementMatcher;
import rocks.inspectit.gepard.agent.instrumentation.state.configuration.InspectitConfigurationHolder;
import rocks.inspectit.gepard.agent.instrumentation.state.configuration.resolver.rules.RuleResolver;
import rocks.inspectit.gepard.agent.instrumentation.state.configuration.resolver.rules.scopes.ScopeResolver;
import rocks.inspectit.gepard.agent.internal.instrumentation.InstrumentedType;
import rocks.inspectit.gepard.agent.internal.instrumentation.model.ClassInstrumentationConfiguration;
import rocks.inspectit.gepard.agent.internal.instrumentation.model.rules.InstrumentationRule;
Expand All @@ -22,18 +23,20 @@ public class ConfigurationResolver {

private final RuleResolver ruleResolver;

private ConfigurationResolver(InspectitConfigurationHolder holder) {
private ConfigurationResolver(InspectitConfigurationHolder holder, RuleResolver ruleResolver) {
this.holder = holder;
this.ruleResolver = new RuleResolver();
this.ruleResolver = ruleResolver;
}

/**
* Factory method to create an {@link ConfigurationResolver}
* Factory method to create an {@link ConfigurationResolver}.
*
* @return the created resolver
*/
public static ConfigurationResolver create(InspectitConfigurationHolder holder) {
return new ConfigurationResolver(holder);
ScopeResolver scopeResolver = new ScopeResolver();
RuleResolver ruleResolver = new RuleResolver(scopeResolver);
return new ConfigurationResolver(holder, ruleResolver);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class RuleResolver {

private final ScopeResolver scopeResolver;

public RuleResolver() {
this.scopeResolver = new ScopeResolver();
public RuleResolver(ScopeResolver scopeResolver) {
this.scopeResolver = scopeResolver;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,81 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.instrumentation.hook.configuration.resolver;

import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import java.util.Set;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import rocks.inspectit.gepard.agent.instrumentation.hook.configuration.MethodHookConfiguration;
import rocks.inspectit.gepard.agent.internal.instrumentation.model.ClassInstrumentationConfiguration;
import rocks.inspectit.gepard.agent.internal.instrumentation.model.rules.InstrumentationRule;
import rocks.inspectit.gepard.config.model.instrumentation.rules.RuleTracingConfiguration;

@ExtendWith(MockitoExtension.class)
class MethodHookConfigurationResolverTest {
// TODO

@Mock private MethodDescription methodDescription;

@Mock private InstrumentationRule activeRule;

@Mock private ClassInstrumentationConfiguration classConfiguration;

private final MethodHookConfigurationResolver resolver = new MethodHookConfigurationResolver();

private final String methodName = "method";

private final ElementMatcher.Junction<MethodDescription> matcher = isMethod();

@BeforeEach
void beforeEach() {
when(methodDescription.getName()).thenReturn(methodName);
when(activeRule.methodMatcher()).thenReturn(matcher);
when(classConfiguration.activeRules()).thenReturn(Set.of(activeRule));
}

@Test
void shouldResolveConfigWithEnabledTracing() {
RuleTracingConfiguration tracing = new RuleTracingConfiguration(true);

when(methodDescription.isMethod()).thenReturn(true);
when(activeRule.tracing()).thenReturn(tracing);

MethodHookConfiguration hookConfig = resolver.resolve(methodDescription, classConfiguration);
boolean tracingEnabled = hookConfig.tracing().getStartSpan();

assertEquals(methodName, hookConfig.methodName());
assertTrue(tracingEnabled);
}

@Test
void shouldResolveConfigWithoutEnabledTracing() {
RuleTracingConfiguration tracing = RuleTracingConfiguration.NO_TRACING;

when(methodDescription.isMethod()).thenReturn(true);
when(activeRule.tracing()).thenReturn(tracing);

MethodHookConfiguration hookConfig = resolver.resolve(methodDescription, classConfiguration);
boolean tracingEnabled = hookConfig.tracing().getStartSpan();

assertEquals(methodName, hookConfig.methodName());
assertFalse(tracingEnabled);
}

@Test
void shouldResolveConfigWithoutEnabledTracingWhenNoRulesMatch() {
when(methodDescription.isMethod()).thenReturn(false);

MethodHookConfiguration hookConfig = resolver.resolve(methodDescription, classConfiguration);
boolean tracingEnabled = hookConfig.tracing().getStartSpan();

assertEquals(methodName, hookConfig.methodName());
assertFalse(tracingEnabled);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.instrumentation.hook.util;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
Expand All @@ -18,13 +18,31 @@ class MethodHookGeneratorTest {
@Mock private MethodHookConfiguration hookConfiguration;

@Test
void shouldCreateMethodHook() {
void shouldCreateMethodHookWithoutTracing() {
String methodName = "method";
when(hookConfiguration.methodName()).thenReturn(methodName);
when(hookConfiguration.tracing()).thenReturn(RuleTracingConfiguration.NO_TRACING);

MethodHook hook = MethodHookGenerator.createHook(hookConfiguration);
boolean tracingEnabled = hook.getConfiguration().tracing().getStartSpan();

assertNotNull(hook);
assertEquals(methodName, hook.getConfiguration().methodName());
assertFalse(tracingEnabled);
}

// TODO
@Test
void shouldCreateMethodHookWithTracing() {
String methodName = "method";
RuleTracingConfiguration tracing = new RuleTracingConfiguration(true);
when(hookConfiguration.methodName()).thenReturn(methodName);
when(hookConfiguration.tracing()).thenReturn(tracing);

MethodHook hook = MethodHookGenerator.createHook(hookConfiguration);
boolean tracingEnabled = hook.getConfiguration().tracing().getStartSpan();

assertNotNull(hook);
assertEquals(methodName, hook.getConfiguration().methodName());
assertTrue(tracingEnabled);
}
}
Loading

0 comments on commit f613346

Please sign in to comment.