Skip to content

Commit

Permalink
refactoring: better api for ContextManager
Browse files Browse the repository at this point in the history
  • Loading branch information
giulong committed Sep 1, 2024
1 parent 15fefc6 commit 672a39f
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.giulong.spectrum.extensions.interceptors;

import com.aventstack.extentreports.ExtentTest;
import io.github.giulong.spectrum.utils.TestContext;
import io.github.giulong.spectrum.types.TestData;
import io.github.giulong.spectrum.utils.*;
import io.github.giulong.spectrum.utils.events.EventsDispatcher;
Expand Down Expand Up @@ -44,12 +43,11 @@ public void interceptDynamicTest(final Invocation<Void> invocation, final Dynami
final String testName = context.getDisplayName();
final Path dynamicVideoPath = Path.of(String.format("%s-%s.mp4", fileUtils.removeExtensionFrom(testData.getVideoPath().toString()), testName));
final ExtentTest currentNode = statefulExtentTest.createNode(testName);
final TestContext parentTestContext = contextManager.get(context.getParent().orElseThrow().getUniqueId());

testData.setDisplayName(testName);
testData.setDynamicVideoPath(dynamicVideoPath);
statefulExtentTest.setDisplayName(testName);
contextManager.put(context.getUniqueId(), parentTestContext);
contextManager.initWithParentFor(context);

if (!video.isDisabled() && videoExtentTest.isAttach()) {
final String fullId = String.format("%s-%s", testData.getTestId(), testName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public WebDriver resolveParameter(final ParameterContext arg0, final ExtensionCo
final WebDriver decoratedDriver = new EventFiringDecorator<>(eventListener).decorate(driver);

store.put(DRIVER, decoratedDriver);
contextManager.get(context.getUniqueId()).put(DRIVER, decoratedDriver);
contextManager.put(context, DRIVER, decoratedDriver);

return decoratedDriver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public StatefulExtentTest resolveParameter(final ParameterContext arg0, final Ex

extentReporter.logTestStartOf(extentTest);
store.put(STATEFUL_EXTENT_TEST, statefulExtentTest);
contextManager.get(context.getUniqueId()).put(STATEFUL_EXTENT_TEST, statefulExtentTest);
contextManager.put(context, STATEFUL_EXTENT_TEST, statefulExtentTest);

return statefulExtentTest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ public class TestContextResolver extends TypeBasedParameterResolver<TestContext>
public TestContext resolveParameter(final ParameterContext arg0, final ExtensionContext context) throws ParameterResolutionException {
log.debug("Resolving {}", TEST_CONTEXT);

final TestContext testContext = new TestContext();

final TestContext testContext = contextManager.initFor(context);
context.getStore(GLOBAL).put(TEST_CONTEXT, testContext);
contextManager.put(context.getUniqueId(), testContext);

return testContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TestData resolveParameter(final ParameterContext arg0, final ExtensionCon
.build();

store.put(TEST_DATA, testData);
contextManager.get(context.getUniqueId()).put(TEST_DATA, testData);
contextManager.put(context, TEST_DATA, testData);
return testData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import static lombok.AccessLevel.PRIVATE;

Expand All @@ -21,15 +21,35 @@ public static ContextManager getInstance() {

private final Map<String, TestContext> testContexts = new ConcurrentHashMap<>();

public void put(final String uniqueId, final TestContext testContext) {
testContexts.put(uniqueId, testContext);
public TestContext initFor(final ExtensionContext context, final TestContext testContext) {
testContexts.put(context.getUniqueId(), testContext);

return testContext;
}

public TestContext initFor(final ExtensionContext context) {
return initFor(context, new TestContext());
}

public TestContext initWithParentFor(final ExtensionContext context) {
return initFor(context, testContexts.get(context.getParent().orElseThrow().getUniqueId()));
}

public TestContext get(final String uniqueId) {
return testContexts.get(uniqueId);
public void put(final ExtensionContext context, final String key, final Object value) {
get(context).put(key, value);
}

public TestContext get(final ExtensionContext context) {
final TestContext testContext = testContexts.get(context.getUniqueId());

if (testContext != null) {
return testContext;
}

return initFor(context);
}

public TestContext computeIfAbsent(final String key, final Function<String, TestContext> mappingFunction) {
return testContexts.computeIfAbsent(key, mappingFunction);
public <T> T get(final ExtensionContext context, final String key, final Class<T> clazz) {
return get(context).get(key, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public Path getReportPathFrom(final Configuration.Extent extent) {
}

public ExtentTest createExtentTestFrom(final ExtensionContext context) {
final TestData testData = contextManager.get(context.getUniqueId()).get(TEST_DATA, TestData.class);
final TestData testData = contextManager.get(context, TEST_DATA, TestData.class);

return extentReports
.createTest(String.format("<div id=\"%s\">%s</div>%s", testData.getTestId(), testData.getClassDisplayName(), testData.getDisplayName()))
Expand All @@ -183,7 +183,7 @@ protected ExtentColor getColorOf(final Status status) {
}

public void logTestEnd(final ExtensionContext context, final Status status) {
final TestContext testContext = contextManager.computeIfAbsent(context.getUniqueId(), k -> new TestContext());
final TestContext testContext = contextManager.get(context);
final StatefulExtentTest statefulExtentTest = testContext.computeIfAbsent(STATEFUL_EXTENT_TEST, k -> {
final String className = context.getRequiredTestClass().getSimpleName();
final String methodName = context.getRequiredTestMethod().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TestBookConsumer extends EventsConsumer {
@Override
public void accept(final Event event) {
final TestBook testBook = configuration.getTestBook();
final TestData testData = contextManager.get(event.getContext().getUniqueId()).get(TEST_DATA, TestData.class);
final TestData testData = contextManager.get(event.getContext(), TEST_DATA, TestData.class);

testBook.updateWithResult(testData.getClassDisplayName(), testData.getDisplayName(), event.getResult());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.fasterxml.jackson.annotation.JsonView;
import io.github.giulong.spectrum.internals.jackson.views.Views.Internal;
import io.github.giulong.spectrum.pojos.events.Event;
import io.github.giulong.spectrum.utils.TestContext;
import io.github.giulong.spectrum.types.TestData;
import io.github.giulong.spectrum.utils.Configuration;
import io.github.giulong.spectrum.utils.ContextManager;
import io.github.giulong.spectrum.utils.video.Video;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jcodec.api.awt.AWTSequenceEncoder;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;

Expand Down Expand Up @@ -49,8 +49,8 @@ public class VideoConsumer extends EventsConsumer {
@Override
public void accept(final Event event) {
final Video video = configuration.getVideo();
final TestContext testContext = contextManager.get(event.getContext().getUniqueId());
final TestData testData = testContext.get(TEST_DATA, TestData.class);
final ExtensionContext context = event.getContext();
final TestData testData = contextManager.get(context, TEST_DATA, TestData.class);

if (video.isDisabled() || event.getResult().equals(DISABLED)) {
log.debug("Video is disabled or test is skipped. Returning");
Expand All @@ -76,7 +76,7 @@ public void accept(final Event event) {
final URL noVideoPng = Objects.requireNonNull(classLoader.getResource("no-video.png"));
encoder.encodeImage(ImageIO.read(noVideoPng));
} else {
final Dimension dimension = chooseDimensionFor(testContext.get(DRIVER, WebDriver.class), video);
final Dimension dimension = chooseDimensionFor(contextManager.get(context, DRIVER, WebDriver.class), video);

for (File frame : frames) {
encoder.encodeImage(resize(ImageIO.read(frame), dimension));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ class SpectrumInterceptorTest {
private final String className = "className";
private final String displayName = "displayName";
private final String fileName = "fileName";
private final String uniqueId = "uniqueId";
private final Path dynamicVideoPath = Path.of(String.format("%s-%s.mp4", fileName, displayName));

@Mock
private ContextManager contextManager;

@Mock
private TestContext parentTestContext;

@Mock
private InvocationInterceptor.Invocation<Void> invocation;

Expand Down Expand Up @@ -107,8 +103,6 @@ public void beforeEach() {
}

private void commonStubs() {
final String parentUniqueId = "parentUniqueId";

when(context.getStore(GLOBAL)).thenReturn(store);
when(store.get(TEST_DATA, TestData.class)).thenReturn(testData);
when(store.get(STATEFUL_EXTENT_TEST, StatefulExtentTest.class)).thenReturn(statefulExtentTest);
Expand All @@ -123,10 +117,6 @@ private void commonStubs() {
when(parentContext.getDisplayName()).thenReturn(className);
when(statefulExtentTest.createNode(displayName)).thenReturn(extentTest);

when(context.getUniqueId()).thenReturn(uniqueId);
when(parentContext.getUniqueId()).thenReturn(parentUniqueId);
when(contextManager.get(parentUniqueId)).thenReturn(parentTestContext);

when(fileUtils.removeExtensionFrom(videoPath.toString())).thenReturn(fileName);
}

Expand All @@ -139,7 +129,7 @@ private void commonVerifications() throws Throwable {

verify(eventsDispatcher).fire(className, displayName, BEFORE, null, Set.of(DYNAMIC_TEST), context);
verify(invocation).proceed();
verify(contextManager).put(uniqueId, parentTestContext);
verify(contextManager).initWithParentFor(context);
}

@DisplayName("interceptDynamicTest should fire the proper events and create nodes in the current extent test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ class DriverResolverTest {
@Mock
private ContextManager contextManager;

@Mock
private TestContext testContext;

@Mock
private ParameterContext parameterContext;

@Mock
private ExtensionContext extensionContext;
private ExtensionContext context;

@Mock
private ExtensionContext rootContext;
Expand Down Expand Up @@ -119,10 +116,9 @@ public void afterEach() {
@SuppressWarnings("unchecked")
public void resolveParameter() {
final String locatorRegex = "locatorRegex";
final String uniqueId = "uniqueId";

when(extensionContext.getStore(GLOBAL)).thenReturn(store);
when(extensionContext.getRoot()).thenReturn(rootContext);
when(context.getStore(GLOBAL)).thenReturn(store);
when(context.getRoot()).thenReturn(rootContext);
when(rootContext.getStore(GLOBAL)).thenReturn(rootStore);
when(rootStore.get(CONFIGURATION, Configuration.class)).thenReturn(configuration);
when(configuration.getRuntime()).thenReturn(runtime);
Expand All @@ -147,18 +143,15 @@ public void resolveParameter() {
when(eventsListenerBuilder.events(events)).thenReturn(eventsListenerBuilder);
when(eventsListenerBuilder.build()).thenReturn(eventsListener);

when(extensionContext.getUniqueId()).thenReturn(uniqueId);
when(contextManager.get(uniqueId)).thenReturn(testContext);

//noinspection rawtypes
MockedConstruction<EventFiringDecorator> mockedConstruction = mockConstruction(EventFiringDecorator.class, (mock, context) -> {
assertEquals(eventsListener, ((WebDriverListener[]) context.arguments().getFirst())[0]);

when(mock.decorate(webDriver)).thenReturn(decoratedWebDriver);
});
WebDriver actual = driverResolver.resolveParameter(parameterContext, extensionContext);
WebDriver actual = driverResolver.resolveParameter(parameterContext, context);
verify(store).put(DRIVER, actual);
verify(testContext).put(DRIVER, actual);
verify(contextManager).put(context, DRIVER, actual);

assertEquals(decoratedWebDriver, actual);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ class StatefulExtentTestResolverTest {
@Mock
private ContextManager contextManager;

@Mock
private TestContext testContext;

@Mock
private ExtentReporter extentReporter;

@Mock
private ParameterContext parameterContext;

@Mock
private ExtensionContext extensionContext;
private ExtensionContext context;

@Mock
private ExtensionContext.Store store;
Expand Down Expand Up @@ -92,8 +89,7 @@ public void afterEach() {
@Test
@DisplayName("resolveParameter should return the initialized ExtentTest adding the video")
public void testResolveParameter() {
final String uniqueId = "uniqueId";
when(extensionContext.getStore(GLOBAL)).thenReturn(store);
when(context.getStore(GLOBAL)).thenReturn(store);

when(store.get(CONFIGURATION, Configuration.class)).thenReturn(configuration);
when(configuration.getVideo()).thenReturn(video);
Expand All @@ -103,28 +99,25 @@ public void testResolveParameter() {
when(store.get(TEST_DATA, TestData.class)).thenReturn(testData);

when(ExtentReporter.getInstance()).thenReturn(extentReporter);
when(extentReporter.createExtentTestFrom(extensionContext)).thenReturn(extentTest);
when(extentReporter.createExtentTestFrom(context)).thenReturn(extentTest);

when(StatefulExtentTest.builder()).thenReturn(statefulExtentTestBuilder);
when(statefulExtentTestBuilder.currentNode(extentTest)).thenReturn(statefulExtentTestBuilder);
when(statefulExtentTestBuilder.build()).thenReturn(statefulExtentTest);

when(extensionContext.getUniqueId()).thenReturn(uniqueId);
when(contextManager.get(uniqueId)).thenReturn(testContext);

StatefulExtentTest actual = statefulExtentTestResolver.resolveParameter(parameterContext, extensionContext);
StatefulExtentTest actual = statefulExtentTestResolver.resolveParameter(parameterContext, context);

verify(extentReporter).logTestStartOf(extentTest);
verify(store).put(STATEFUL_EXTENT_TEST, actual);
verify(contextManager).put(context, STATEFUL_EXTENT_TEST, actual);
assertEquals(statefulExtentTest, actual);
}

@DisplayName("resolveParameter should return the initialized ExtentTest without adding the video")
@ParameterizedTest(name = "with video disabled {0} and video attach {1}")
@MethodSource("noVideoValuesProvider")
public void testResolveParameterNoVideo(final boolean disabled, final boolean attach) {
final String uniqueId = "uniqueId";
when(extensionContext.getStore(GLOBAL)).thenReturn(store);
when(context.getStore(GLOBAL)).thenReturn(store);

when(store.get(CONFIGURATION, Configuration.class)).thenReturn(configuration);
when(configuration.getVideo()).thenReturn(video);
Expand All @@ -133,21 +126,19 @@ public void testResolveParameterNoVideo(final boolean disabled, final boolean at
when(store.get(TEST_DATA, TestData.class)).thenReturn(testData);

when(ExtentReporter.getInstance()).thenReturn(extentReporter);
when(extentReporter.createExtentTestFrom(extensionContext)).thenReturn(extentTest);
when(extentReporter.createExtentTestFrom(context)).thenReturn(extentTest);

when(StatefulExtentTest.builder()).thenReturn(statefulExtentTestBuilder);
when(statefulExtentTestBuilder.currentNode(extentTest)).thenReturn(statefulExtentTestBuilder);
when(statefulExtentTestBuilder.build()).thenReturn(statefulExtentTest);

lenient().when(videoExtentTest.isAttach()).thenReturn(attach);

when(extensionContext.getUniqueId()).thenReturn(uniqueId);
when(contextManager.get(uniqueId)).thenReturn(testContext);

StatefulExtentTest actual = statefulExtentTestResolver.resolveParameter(parameterContext, extensionContext);
StatefulExtentTest actual = statefulExtentTestResolver.resolveParameter(parameterContext, context);

verifyNoMoreInteractions(extentTest);
verify(store).put(STATEFUL_EXTENT_TEST, actual);
verify(contextManager).put(context, STATEFUL_EXTENT_TEST, actual);
assertEquals(statefulExtentTest, actual);
}

Expand Down
Loading

0 comments on commit 672a39f

Please sign in to comment.