From 244de95898488926f2171478b366213446dcd6e3 Mon Sep 17 00:00:00 2001 From: Sergey Grigoriev Date: Fri, 4 Oct 2024 16:48:32 +0200 Subject: [PATCH] feat: current context for test classes (#180) --- .../generic/context/CurrentContextConfig.java | 12 ++++ .../context/CurrentContextExtension.java | 35 ++++++++++++ .../generic/properties/GetterFinderTest.java | 24 +------- .../settings/GenericNamedSettingsTest.java | 55 +++++++------------ 4 files changed, 69 insertions(+), 57 deletions(-) create mode 100644 app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextConfig.java create mode 100644 app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextExtension.java diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextConfig.java b/app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextConfig.java new file mode 100644 index 0000000..415a47b --- /dev/null +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextConfig.java @@ -0,0 +1,12 @@ +package ch.sbb.polarion.extension.generic.context; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface CurrentContextConfig { + String value() default "text-extension"; +} diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextExtension.java b/app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextExtension.java new file mode 100644 index 0000000..0501b03 --- /dev/null +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextExtension.java @@ -0,0 +1,35 @@ +package ch.sbb.polarion.extension.generic.context; + +import ch.sbb.polarion.extension.generic.rest.model.Context; +import ch.sbb.polarion.extension.generic.util.ContextUtils; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +public class CurrentContextExtension implements BeforeEachCallback, AfterEachCallback { + + private MockedStatic contextUtilsMockedStatic; + + @Override + public void beforeEach(ExtensionContext extensionContext) throws Exception { + contextUtilsMockedStatic = Mockito.mockStatic(ContextUtils.class); + + String contextName = "test-extension"; + CurrentContextConfig currentContextConfig = extensionContext.getRequiredTestClass().getAnnotation(CurrentContextConfig.class); + if (currentContextConfig != null) { + contextName = currentContextConfig.value(); + } + + Context context = new Context(contextName); + contextUtilsMockedStatic.when(ContextUtils::getContext).thenReturn(context); + } + + @Override + public void afterEach(ExtensionContext extensionContext) throws Exception { + if (contextUtilsMockedStatic != null) { + contextUtilsMockedStatic.close(); + } + } +} diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/properties/GetterFinderTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/properties/GetterFinderTest.java index 6345ee0..ae6c672 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/properties/GetterFinderTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/properties/GetterFinderTest.java @@ -1,36 +1,16 @@ package ch.sbb.polarion.extension.generic.properties; -import ch.sbb.polarion.extension.generic.rest.model.Context; -import ch.sbb.polarion.extension.generic.util.ContextUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import ch.sbb.polarion.extension.generic.context.CurrentContextExtension; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -@ExtendWith(MockitoExtension.class) +@ExtendWith({MockitoExtension.class, CurrentContextExtension.class}) class GetterFinderTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - MockedStatic contextUtils; - - @BeforeEach - void setUp() { - Context context = new Context("test-extension"); - contextUtils.when(ContextUtils::getContext).thenReturn(context); - } - - @AfterEach - void tearDown() { - contextUtils.close(); - } - @Test void testGetValue() { TestExtensionConfiguration configuration = new TestExtensionConfiguration(); diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/settings/GenericNamedSettingsTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/settings/GenericNamedSettingsTest.java index 7c71df1..dafe7e7 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/settings/GenericNamedSettingsTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/settings/GenericNamedSettingsTest.java @@ -1,18 +1,14 @@ package ch.sbb.polarion.extension.generic.settings; +import ch.sbb.polarion.extension.generic.context.CurrentContextConfig; +import ch.sbb.polarion.extension.generic.context.CurrentContextExtension; import ch.sbb.polarion.extension.generic.exception.ObjectNotFoundException; -import ch.sbb.polarion.extension.generic.rest.model.Context; import ch.sbb.polarion.extension.generic.settings.named_settings.TestModel; import ch.sbb.polarion.extension.generic.settings.named_settings.TestSettings; -import ch.sbb.polarion.extension.generic.util.ContextUtils; import ch.sbb.polarion.extension.generic.util.ScopeUtils; import com.polarion.subterra.base.location.ILocation; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; import org.mockito.MockedConstruction; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -31,23 +27,12 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; -@ExtendWith(MockitoExtension.class) +@ExtendWith({MockitoExtension.class, CurrentContextExtension.class}) +@CurrentContextConfig(GenericNamedSettingsTest.POLARION_TEXT_EXTENSION) @SuppressWarnings("unused") class GenericNamedSettingsTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - MockedStatic contextUtils; - - @BeforeEach - void setUp() { - Context context = new Context("test-extension"); - contextUtils.when(ContextUtils::getContext).thenReturn(context); - } - - @AfterEach - void tearDown() { - contextUtils.close(); - } + public static final String POLARION_TEXT_EXTENSION = "polarion-text-extension"; @Test void testBeforeAndAfterSave() { @@ -80,11 +65,11 @@ void testRead() { ILocation mockProjectLocation = mock(ILocation.class); ILocation mockProjectSettingsFolderLocation = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test")).thenReturn(mockProjectSettingsFolderLocation); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test")).thenReturn(mockProjectSettingsFolderLocation); ILocation mockProjectTest1Location = mock(ILocation.class); ILocation mockProjectTest2Location = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test/project_test1.settings")).thenReturn(mockProjectTest1Location); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test/project_test2.settings")).thenReturn(mockProjectTest2Location); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/project_test1.settings")).thenReturn(mockProjectTest1Location); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/project_test2.settings")).thenReturn(mockProjectTest2Location); mockScopeUtils.when(() -> ScopeUtils.getContextLocation("project/some_project/")).thenReturn(mockProjectLocation); when(settingsService.getLastRevision(mockProjectSettingsFolderLocation)).thenReturn("34"); @@ -92,11 +77,11 @@ void testRead() { ILocation mockDefaultLocation = mock(ILocation.class); ILocation mockDefaultSettingsFolderLocation = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test")).thenReturn(mockDefaultSettingsFolderLocation); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test")).thenReturn(mockDefaultSettingsFolderLocation); ILocation mockDefaultTest1Location = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test/default_test1.settings")).thenReturn(mockDefaultTest1Location); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/default_test1.settings")).thenReturn(mockDefaultTest1Location); ILocation mockDefaultDefaultLocation = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test/Default.settings")).thenReturn(mockDefaultDefaultLocation); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/Default.settings")).thenReturn(mockDefaultDefaultLocation); mockScopeUtils.when(() -> ScopeUtils.getContextLocation("")).thenReturn(mockDefaultLocation); when(settingsService.getLastRevision(mockDefaultSettingsFolderLocation)).thenReturn("42"); @@ -192,10 +177,10 @@ void testDelete() { ILocation mockProjectLocation = mock(ILocation.class); ILocation mockProjectSettingsFolderLocation = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test")).thenReturn(mockProjectSettingsFolderLocation); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test")).thenReturn(mockProjectSettingsFolderLocation); ILocation mockProjectTest1Location = mock(ILocation.class); ILocation mockProjectTest2Location = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test/project_delete1.settings")).thenReturn(mockProjectTest1Location); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/project_delete1.settings")).thenReturn(mockProjectTest1Location); mockScopeUtils.when(() -> ScopeUtils.getContextLocation("project/delete_project/")).thenReturn(mockProjectLocation); when(settingsService.getLastRevision(mockProjectSettingsFolderLocation)).thenReturn("11"); @@ -203,9 +188,9 @@ void testDelete() { ILocation mockDefaultLocation = mock(ILocation.class); ILocation mockDefaultSettingsFolderLocation = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test")).thenReturn(mockDefaultSettingsFolderLocation); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test")).thenReturn(mockDefaultSettingsFolderLocation); ILocation mockDefaultTest1Location = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test/default_delete1.settings")).thenReturn(mockDefaultTest1Location); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/default_delete1.settings")).thenReturn(mockDefaultTest1Location); mockScopeUtils.when(() -> ScopeUtils.getContextLocation("")).thenReturn(mockDefaultLocation); when(settingsService.getLastRevision(mockDefaultSettingsFolderLocation)).thenReturn("66"); @@ -235,11 +220,11 @@ void testListRevisions() { ILocation mockProjectLocation = mock(ILocation.class); ILocation mockProjectSettingsFolderLocation = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test")).thenReturn(mockProjectSettingsFolderLocation); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test")).thenReturn(mockProjectSettingsFolderLocation); ILocation mockProjectTest1Location = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test/project_list_revisions1.settings")).thenReturn(mockProjectTest1Location); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/project_list_revisions1.settings")).thenReturn(mockProjectTest1Location); ILocation mockProjectDefault1Location = mock(ILocation.class); - when(mockProjectLocation.append(".polarion/extensions/test-extension/Test/default_list_revisions1.settings")).thenReturn(mockProjectDefault1Location); + when(mockProjectLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/default_list_revisions1.settings")).thenReturn(mockProjectDefault1Location); mockScopeUtils.when(() -> ScopeUtils.getContextLocation("project/list_revisions_project/")).thenReturn(mockProjectLocation); when(settingsService.getLastRevision(mockProjectSettingsFolderLocation)).thenReturn("11"); @@ -247,9 +232,9 @@ void testListRevisions() { ILocation mockDefaultLocation = mock(ILocation.class); ILocation mockDefaultSettingsFolderLocation = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test")).thenReturn(mockDefaultSettingsFolderLocation); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test")).thenReturn(mockDefaultSettingsFolderLocation); ILocation mockDefaultTest1Location = mock(ILocation.class); - when(mockDefaultLocation.append(".polarion/extensions/test-extension/Test/default_list_revisions1.settings")).thenReturn(mockDefaultTest1Location); + when(mockDefaultLocation.append(".polarion/extensions/" + POLARION_TEXT_EXTENSION + "/Test/default_list_revisions1.settings")).thenReturn(mockDefaultTest1Location); mockScopeUtils.when(() -> ScopeUtils.getContextLocation("")).thenReturn(mockDefaultLocation); when(settingsService.getLastRevision(mockDefaultSettingsFolderLocation)).thenReturn("66");