-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getting configured but obsolete/non-valid options for extension (…
- Loading branch information
Showing
9 changed files
with
293 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,8 @@ public int size() { | |
return properties.size(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return properties.isEmpty(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/src/main/java/ch/sbb/polarion/extension/generic/properties/SystemProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ch.sbb.polarion.extension.generic.properties; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
import java.util.Properties; | ||
|
||
@UtilityClass | ||
public class SystemProperties { | ||
public static Properties getProperties() { | ||
return System.getProperties(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package ch.sbb.polarion.extension.generic.context; | ||
|
||
import ch.sbb.polarion.extension.generic.properties.ExtensionConfiguration; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static ch.sbb.polarion.extension.generic.context.CurrentContextExtension.TEST_EXTENSION; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CurrentContextConfig { | ||
String value() default "text-extension"; | ||
} | ||
String value() default TEST_EXTENSION; | ||
Class<?> extensionConfiguration() default ExtensionConfiguration.class; | ||
} |
39 changes: 30 additions & 9 deletions
39
app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
package ch.sbb.polarion.extension.generic.context; | ||
|
||
import ch.sbb.polarion.extension.generic.rest.model.Context; | ||
import ch.sbb.polarion.extension.generic.properties.ExtensionConfiguration; | ||
import ch.sbb.polarion.extension.generic.util.ContextUtils; | ||
import ch.sbb.polarion.extension.generic.util.ManifestUtils; | ||
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; | ||
|
||
import java.util.Set; | ||
import java.util.jar.Attributes; | ||
|
||
import static org.mockito.Mockito.mockStatic; | ||
|
||
public class CurrentContextExtension implements BeforeEachCallback, AfterEachCallback { | ||
|
||
public static final String TEST_EXTENSION = "test-extension"; | ||
|
||
private MockedStatic<ContextUtils> contextUtilsMockedStatic; | ||
private MockedStatic<ManifestUtils> manifestUtilsMockedStatic; | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext extensionContext) throws Exception { | ||
contextUtilsMockedStatic = Mockito.mockStatic(ContextUtils.class); | ||
contextUtilsMockedStatic = mockStatic(ContextUtils.class); | ||
|
||
String contextName = "test-extension"; | ||
CurrentContextConfig currentContextConfig = extensionContext.getRequiredTestClass().getAnnotation(CurrentContextConfig.class); | ||
if (currentContextConfig != null) { | ||
contextName = currentContextConfig.value(); | ||
String contextName = TEST_EXTENSION; | ||
CurrentContextConfig annotation = extensionContext.getRequiredTestClass().getAnnotation(CurrentContextConfig.class); | ||
if (annotation != null) { | ||
contextName = annotation.value(); | ||
Class<?> testExtensionConfigurationClass = annotation.extensionConfiguration(); | ||
contextUtilsMockedStatic.when(() -> ContextUtils.findSubTypes(ExtensionConfiguration.class)).thenReturn(Set.of(testExtensionConfigurationClass)); | ||
} | ||
|
||
Context context = new Context(contextName); | ||
contextUtilsMockedStatic.when(ContextUtils::getContext).thenReturn(context); | ||
manifestUtilsMockedStatic = mockStatic(ManifestUtils.class); | ||
Attributes attributes = new Attributes(); | ||
attributes.put(new Attributes.Name(ContextUtils.EXTENSION_CONTEXT), contextName); | ||
attributes.put(new Attributes.Name(ContextUtils.DISCOVER_BASE_PACKAGE), ContextUtils.CH_SBB_POLARION_EXTENSION + contextName.replace("-", "_")); | ||
attributes.put(new Attributes.Name(ContextUtils.CONFIGURATION_PROPERTIES_PREFIX), ContextUtils.CH_SBB_POLARION_EXTENSION + contextName.replace("-", "_") + "."); | ||
manifestUtilsMockedStatic.when(ManifestUtils::getManifestAttributes).thenReturn(attributes); | ||
|
||
contextUtilsMockedStatic.when(ContextUtils::getContext).thenCallRealMethod(); | ||
contextUtilsMockedStatic.when(ContextUtils::getConfigurationPropertiesPrefix).thenCallRealMethod(); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext extensionContext) throws Exception { | ||
if (manifestUtilsMockedStatic != null) { | ||
manifestUtilsMockedStatic.close(); | ||
} | ||
if (contextUtilsMockedStatic != null) { | ||
contextUtilsMockedStatic.close(); | ||
} | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
app/src/test/java/ch/sbb/polarion/extension/generic/context/SystemPropertiesExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package ch.sbb.polarion.extension.generic.context; | ||
|
||
import ch.sbb.polarion.extension.generic.properties.SystemProperties; | ||
import com.polarion.core.config.impl.SystemValueReader; | ||
import org.jetbrains.annotations.NotNull; | ||
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 java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.lang.reflect.Method; | ||
import java.util.Properties; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class SystemPropertiesExtension implements BeforeEachCallback, AfterEachCallback { | ||
|
||
private MockedStatic<SystemProperties> systemPropertiesMockedStatic; | ||
private MockedStatic<SystemValueReader> systemValueReaderMockedStatic; | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
systemPropertiesMockedStatic = mockStatic(SystemProperties.class); | ||
systemValueReaderMockedStatic = mockStatic(SystemValueReader.class); | ||
|
||
Properties properties = getAnnotatedRuntimeProperties(context); | ||
systemPropertiesMockedStatic.when(SystemProperties::getProperties).thenReturn(properties); | ||
|
||
SystemValueReader systemValueReader = mock(SystemValueReader.class); | ||
lenient().when(systemValueReader.readString(anyString(), anyString())).thenAnswer(invocation -> { | ||
String key = invocation.getArgument(0); | ||
return properties.getProperty(key); | ||
}); | ||
systemValueReaderMockedStatic.when(SystemValueReader::getInstance).thenReturn(systemValueReader); | ||
} | ||
|
||
private @NotNull Properties getAnnotatedRuntimeProperties(ExtensionContext context) { | ||
Properties properties = new Properties(); | ||
|
||
Method testMethod = context.getRequiredTestMethod(); | ||
|
||
if (testMethod.isAnnotationPresent(RuntimeProperties.class)) { | ||
RuntimeProperties annotation = testMethod.getAnnotation(RuntimeProperties.class); | ||
if (annotation != null) { | ||
for (String property : annotation.value()) { | ||
String[] keyValue = property.split("="); | ||
if (keyValue.length == 2) { | ||
properties.setProperty(keyValue[0], keyValue[1]); | ||
} | ||
} | ||
} | ||
} | ||
return properties; | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
if (systemPropertiesMockedStatic != null) { | ||
systemPropertiesMockedStatic.close(); | ||
} | ||
if (systemValueReaderMockedStatic != null) { | ||
systemValueReaderMockedStatic.close(); | ||
} | ||
} | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RuntimeProperties { | ||
String[] value() default {}; | ||
} | ||
} |
Oops, something went wrong.