treat routes as map where paths are keys, and they can be overwritten - more flexibilty in setting up tests w/ custom override paths

This commit is contained in:
2023-06-02 11:53:02 -05:00
parent 5d42f29696
commit 0eb7b9faa1
3 changed files with 20 additions and 21 deletions

View File

@ -3,10 +3,11 @@ package com.kingsrook.qqq.materialdashboard.lib.javalin;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.materialdashboard.lib.QSeleniumLib; import com.kingsrook.qqq.materialdashboard.lib.QSeleniumLib;
import io.javalin.Javalin; import io.javalin.Javalin;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
@ -25,8 +26,8 @@ public class QSeleniumJavalin
private long WAIT_SECONDS = 10; private long WAIT_SECONDS = 10;
private List<Pair<String, String>> routesToFiles = new ArrayList<>(); private Map<String, String> routesToFiles = new LinkedHashMap<>();
private List<Pair<String, String>> routesToStrings = new ArrayList<>(); private Map<String, String> routesToStrings = new LinkedHashMap<>();
private Javalin javalin; private Javalin javalin;
@ -71,9 +72,9 @@ public class QSeleniumJavalin
{ {
if(this.routesToFiles == null) if(this.routesToFiles == null)
{ {
this.routesToFiles = new ArrayList<>(); this.routesToFiles = new LinkedHashMap<>();
} }
this.routesToFiles.add(Pair.of(path, fixtureFilePath)); this.routesToFiles.put(path, fixtureFilePath);
return (this); return (this);
} }
@ -86,9 +87,9 @@ public class QSeleniumJavalin
{ {
if(this.routesToStrings == null) if(this.routesToStrings == null)
{ {
this.routesToStrings = new ArrayList<>(); this.routesToStrings = new LinkedHashMap<>();
} }
this.routesToStrings.add(Pair.of(path, responseString)); this.routesToStrings.put(path, responseString);
return (this); return (this);
} }
@ -105,11 +106,11 @@ public class QSeleniumJavalin
{ {
javalin.routes(() -> javalin.routes(() ->
{ {
for(Pair<String, String> routeToFile : routesToFiles) for(Map.Entry<String, String> routeToFile : routesToFiles.entrySet())
{ {
LOG.debug("Setting up route for [" + routeToFile.getKey() + "] => [" + routeToFile.getValue() + "]"); LOG.debug("Setting up route for [" + routeToFile.getKey() + "] => [" + routeToFile.getValue() + "]");
get(routeToFile.getKey(), new RouteFromFileHandler(this, routeToFile)); get(routeToFile.getKey(), new RouteFromFileHandler(this, routeToFile.getKey(), routeToFile.getValue()));
post(routeToFile.getKey(), new RouteFromFileHandler(this, routeToFile)); post(routeToFile.getKey(), new RouteFromFileHandler(this, routeToFile.getKey(), routeToFile.getValue()));
} }
}); });
} }
@ -118,11 +119,11 @@ public class QSeleniumJavalin
{ {
javalin.routes(() -> javalin.routes(() ->
{ {
for(Pair<String, String> routeToString : routesToStrings) for(Map.Entry<String, String> routeToString : routesToStrings.entrySet())
{ {
LOG.debug("Setting up route for [" + routeToString.getKey() + "] => [" + routeToString.getValue() + "]"); LOG.debug("Setting up route for [" + routeToString.getKey() + "] => [" + routeToString.getValue() + "]");
get(routeToString.getKey(), new RouteFromStringHandler(this, routeToString)); get(routeToString.getKey(), new RouteFromStringHandler(this, routeToString.getKey(), routeToString.getValue()));
post(routeToString.getKey(), new RouteFromStringHandler(this, routeToString)); post(routeToString.getKey(), new RouteFromStringHandler(this, routeToString.getKey(), routeToString.getValue()));
} }
}); });
} }

View File

@ -6,7 +6,6 @@ import java.util.List;
import io.javalin.http.Context; import io.javalin.http.Context;
import io.javalin.http.Handler; import io.javalin.http.Handler;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -28,11 +27,11 @@ public class RouteFromFileHandler implements Handler
** Constructor ** Constructor
** **
*******************************************************************************/ *******************************************************************************/
public RouteFromFileHandler(QSeleniumJavalin qSeleniumJavalin, Pair<String, String> routeToFilePath) public RouteFromFileHandler(QSeleniumJavalin qSeleniumJavalin, String route, String filePath)
{ {
this.qSeleniumJavalin = qSeleniumJavalin; this.qSeleniumJavalin = qSeleniumJavalin;
this.route = routeToFilePath.getKey(); this.route = route;
this.filePath = routeToFilePath.getValue(); this.filePath = filePath;
} }

View File

@ -3,7 +3,6 @@ package com.kingsrook.qqq.materialdashboard.lib.javalin;
import io.javalin.http.Context; import io.javalin.http.Context;
import io.javalin.http.Handler; import io.javalin.http.Handler;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -25,11 +24,11 @@ public class RouteFromStringHandler implements Handler
** Constructor ** Constructor
** **
*******************************************************************************/ *******************************************************************************/
public RouteFromStringHandler(QSeleniumJavalin qSeleniumJavalin, Pair<String, String> routeToStringPath) public RouteFromStringHandler(QSeleniumJavalin qSeleniumJavalin, String route, String responseString)
{ {
this.qSeleniumJavalin = qSeleniumJavalin; this.qSeleniumJavalin = qSeleniumJavalin;
this.route = routeToStringPath.getKey(); this.route = route;
this.responseString = routeToStringPath.getValue(); this.responseString = responseString;
} }