Skip to content

Commit

Permalink
feat: current context for test classes (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev authored Oct 4, 2024
1 parent b9f76d1 commit 244de95
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
@@ -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<ContextUtils> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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> 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() {
Expand Down Expand Up @@ -80,23 +65,23 @@ 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");
when(settingsService.getPersistedSettingFileNames(mockProjectSettingsFolderLocation)).thenReturn(List.of("project_test1", "project_test2"));

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");
Expand Down Expand Up @@ -192,20 +177,20 @@ 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");
when(settingsService.getPersistedSettingFileNames(mockProjectSettingsFolderLocation)).thenReturn(List.of("project_delete1"));

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");
Expand Down Expand Up @@ -235,21 +220,21 @@ 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");
when(settingsService.getPersistedSettingFileNames(mockProjectSettingsFolderLocation)).thenReturn(List.of("project_list_revisions1"));

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");
Expand Down

0 comments on commit 244de95

Please sign in to comment.