8000 #30609. Ability to select supplier type by ikhomyn · Pull Request #31221 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions test-poc/framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
<selenium.version>4.21.0</selenium.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<kc.test.browser>${kc.test.browser}</kc.test.browser>
<kc.test.database>${kc.test.database}</kc.test.database>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Comment on lines +38 to +51
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda sucks if we need to enumerate all tests config options like this, but not sure there is any other ways if you want to pass system properties set on the command-line to Surefire.

This would need to be moved to https://github.com/keycloak/keycloak/blob/main/test-poc/base/pom.xml though, as that's the module where tests are executed.


<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
Expand All @@ -20,10 +23,17 @@ public class Registry {

private ExtensionContext currentContext;
private final List<Supplier<?, ?>> suppliers = new LinkedList<>();
private final Map<SupplierType, Map<String, Supplier>> mappedSuppliers = new HashMap<SupplierType, Map<String, Supplier>>();
enum SupplierType {
BROWSER,
DATABASE,
UNMAPPED
}
private final List<InstanceWrapper<?, ?>> deployedInstances = new LinkedList<>();
private final List<InstanceWrapper<?, ?>> requestedInstances = new LinkedList<>();

public Registry() {
setMappedSuppliers();
loadSuppliers();
}

Expand Down Expand Up @@ -197,13 +207,65 @@ private InstanceWrapper getDeployedInstance(Supplier supplier) {
}

private void loadSuppliers() {
ServiceLoader.load(Supplier.class).iterator().forEachRemaining(suppliers::add);
Map<String, Supplier> browserSuppliers = mappedSuppliers.get(SupplierType.BROWSER);
Map<String, Supplier> databaseSuppliers = mappedSuppliers.get(SupplierType.DATABASE);
String browser = System.getProperty("kc.test.browser");
String database = System.getProperty("kc.test.database");

if (browser != null){
for (String key : browserSuppliers.keySet()) {
if (key.toString().toLowerCase().contains(browser.toLowerCase())) {
suppliers.add(browserSuppliers.get(key));
break;
}
}
}else{ //Set default one
suppliers.add(browserSuppliers.get(browserSuppliers.keySet().stream().findFirst().get()));
}

if (database != null){

for (String key : databaseSuppliers.keySet()) {
if (key.toString().toLowerCase().contains(browser.toLowerCase())) {
suppliers.add(databaseSuppliers.get(key));
break;
}
}
}else{ //Set default one
if (databaseSuppliers != null) //TODO: to avoid exception as currently not available any database supplier
suppliers.add(databaseSuppliers.get(databaseSuppliers.keySet().stream().findFirst().get()));
}
Map<String, Supplier> unmappedSupplier = mappedSuppliers.get(SupplierType.UNMAPPED);
unmappedSupplier.forEach( (k, v) -> {
suppliers.add(v);
});

if (LOGGER.isTraceEnabled()) {
LOGGER.tracev("Suppliers: {0}", suppliers.stream().map(s -> s.getClass().getSimpleName()).collect(Collectors.joining(", ")));
}
}

private void setMappedSuppliers(){
ServiceLoader.load(Supplier.class).iterator().forEachRemaining(supplier -> {
SupplierType supplierType = this.getSupplierType(supplier);
Map<String, Supplier> supplierTypeMap = mappedSuppliers.get(supplierType);
if(supplierTypeMap == null){
supplierTypeMap = new HashMap<String, Supplier>();
}
supplierTypeMap.put(supplier.getClass().getSimpleName(), supplier);
mappedSuppliers.put(supplierType, supplierTypeMap);
});
}

private SupplierType getSupplierType(Supplier supplier){
if(supplier.getClass().getSimpleName().contains("database")){
return SupplierType.DATABASE;
}else if(supplier.getClass().getSimpleName().contains("WebDriver")){
return SupplierType.BROWSER;
}
return SupplierType.UNMAPPED;
}

private InstanceWrapper getDeployedInstance(Class typeClass) {
return deployedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null);
}
Expand Down
0