-
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.
Refs: #220
- Loading branch information
Showing
8 changed files
with
347 additions
and
205 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
11 changes: 11 additions & 0 deletions
11
app/src/test/java/ch/sbb/polarion/extension/generic/polarion/CustomExtensionMock.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,11 @@ | ||
package ch.sbb.polarion.extension.generic.polarion; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CustomExtensionMock { | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/test/java/ch/sbb/polarion/extension/generic/polarion/CustomExtensionMockInjector.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,21 @@ | ||
package ch.sbb.polarion.extension.generic.polarion; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class CustomExtensionMockInjector { | ||
|
||
public static <T> void inject(ExtensionContext context, T what) throws IllegalAccessException { | ||
Object testInstance = context.getTestInstance().orElse(null); | ||
if (testInstance != null) { | ||
for (Field field : testInstance.getClass().getDeclaredFields()) { | ||
if (field.isAnnotationPresent(CustomExtensionMock.class) && field.getType().isAssignableFrom(what.getClass())) { | ||
field.setAccessible(true); | ||
field.set(testInstance, what); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...rc/test/java/ch/sbb/polarion/extension/generic/polarion/PlatformContextMockExtension.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.polarion; | ||
|
||
import com.polarion.alm.projects.IProjectService; | ||
import com.polarion.alm.tracker.ITestManagementService; | ||
import com.polarion.alm.tracker.ITrackerService; | ||
import com.polarion.platform.IPlatformService; | ||
import com.polarion.platform.core.IPlatform; | ||
import com.polarion.platform.core.PlatformContext; | ||
import com.polarion.platform.persistence.IDataService; | ||
import com.polarion.platform.security.ILoginPolicy; | ||
import com.polarion.platform.security.ISecurityService; | ||
import com.polarion.platform.service.repository.IRepositoryService; | ||
import com.polarion.portal.internal.server.navigation.TestManagementServiceAccessor; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.mockito.MockedConstruction; | ||
import org.mockito.MockedStatic; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class PlatformContextMockExtension implements BeforeEachCallback, AfterEachCallback { | ||
|
||
private MockedConstruction<TestManagementServiceAccessor> testManagementServiceAccessorMockedConstruction; | ||
private MockedStatic<PlatformContext> platformContextMockedStatic; | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
IPlatform platformMock = mock(IPlatform.class); | ||
|
||
ITrackerService trackerService = mock(ITrackerService.class); | ||
IProjectService projectService = mock(IProjectService.class); | ||
ISecurityService securityService = mock(ISecurityService.class); | ||
IPlatformService platformService = mock(IPlatformService.class); | ||
IRepositoryService repositoryService = mock(IRepositoryService.class); | ||
IDataService dataService = mock(IDataService.class); | ||
ILoginPolicy loginPolicy = mock(ILoginPolicy.class); | ||
ITestManagementService testManagementService = mock(ITestManagementService.class); | ||
|
||
lenient().when(platformMock.lookupService(ITrackerService.class)).thenReturn(trackerService); | ||
lenient().when(platformMock.lookupService(IProjectService.class)).thenReturn(projectService); | ||
lenient().when(platformMock.lookupService(ISecurityService.class)).thenReturn(securityService); | ||
lenient().when(platformMock.lookupService(IPlatformService.class)).thenReturn(platformService); | ||
lenient().when(platformMock.lookupService(IRepositoryService.class)).thenReturn(repositoryService); | ||
lenient().when(platformMock.lookupService(IDataService.class)).thenReturn(dataService); | ||
lenient().when(platformMock.lookupService(ILoginPolicy.class)).thenReturn(loginPolicy); | ||
|
||
lenient().when(trackerService.getDataService()).thenReturn(dataService); | ||
|
||
platformContextMockedStatic = mockStatic(PlatformContext.class); | ||
platformContextMockedStatic.when(PlatformContext::getPlatform).thenReturn(platformMock); | ||
|
||
CustomExtensionMockInjector.inject(context, trackerService); | ||
CustomExtensionMockInjector.inject(context, projectService); | ||
CustomExtensionMockInjector.inject(context, securityService); | ||
CustomExtensionMockInjector.inject(context, platformService); | ||
CustomExtensionMockInjector.inject(context, repositoryService); | ||
CustomExtensionMockInjector.inject(context, dataService); | ||
CustomExtensionMockInjector.inject(context, loginPolicy); | ||
|
||
testManagementServiceAccessorMockedConstruction = mockConstruction(TestManagementServiceAccessor.class, (testManagementServiceAccessor, mockedContructionContext) -> { | ||
lenient().when(testManagementServiceAccessor.getTestingService()).thenReturn(testManagementService); | ||
}); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
if (testManagementServiceAccessorMockedConstruction != null) { | ||
testManagementServiceAccessorMockedConstruction.close(); | ||
} | ||
if (platformContextMockedStatic != null) { | ||
platformContextMockedStatic.close(); | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
.../test/java/ch/sbb/polarion/extension/generic/polarion/TransactionalExecutorExtension.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,62 @@ | ||
package ch.sbb.polarion.extension.generic.polarion; | ||
|
||
import com.polarion.alm.shared.api.transaction.RunnableInReadOnlyTransaction; | ||
import com.polarion.alm.shared.api.transaction.TransactionalExecutor; | ||
import com.polarion.alm.shared.api.transaction.internal.InternalReadOnlyTransaction; | ||
import com.polarion.alm.shared.api.utils.RunnableWithResult; | ||
import com.polarion.alm.shared.api.utils.internal.InternalPolarionUtils; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
import org.mockito.MockedStatic; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class TransactionalExecutorExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver { | ||
|
||
private MockedStatic<TransactionalExecutor> transactionalExecutorMockedStatic; | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
transactionalExecutorMockedStatic = mockStatic(TransactionalExecutor.class); | ||
|
||
InternalReadOnlyTransaction internalReadOnlyTransactionMock = mock(InternalReadOnlyTransaction.class); | ||
transactionalExecutorMockedStatic.when(() -> TransactionalExecutor.executeSafelyInReadOnlyTransaction(any())).thenAnswer(invocation -> { | ||
RunnableInReadOnlyTransaction<?> runnable = invocation.getArgument(0); | ||
return runnable.run(internalReadOnlyTransactionMock); | ||
}); | ||
transactionalExecutorMockedStatic.when(TransactionalExecutor::currentTransaction).thenReturn(internalReadOnlyTransactionMock); | ||
|
||
InternalPolarionUtils internalPolarionUtils = mock(InternalPolarionUtils.class); | ||
lenient().when(internalPolarionUtils.executeInBaseline(any(), any())).thenAnswer(invocation -> { | ||
RunnableWithResult<?> runnableWithResult = invocation.getArgument(1); | ||
return runnableWithResult.run(); | ||
}); | ||
lenient().when(internalReadOnlyTransactionMock.utils()).thenReturn(internalPolarionUtils); | ||
|
||
CustomExtensionMockInjector.inject(context, internalPolarionUtils); | ||
CustomExtensionMockInjector.inject(context, internalReadOnlyTransactionMock); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
if (transactionalExecutorMockedStatic != null) { | ||
transactionalExecutorMockedStatic.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return parameterContext.getParameter().getType().equals(ExtensionContext.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return extensionContext; | ||
} | ||
|
||
} |
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
Oops, something went wrong.