JUnit testing (positive and negative testing) - java

This is the code I have written for JUnit Testing for positive and negative testing.
#Test
public void getMaintenenceIntervalsByMetadataOKTest() throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.set("vinModelYear", "2016");
params.set("vinModelCode", "1633F6");
params.set("vinEngineCode", "CZTA");
params.set("interval", "100000");
params.set("vinTransmissionCode", "");
params.set("importerNumber", "");
params.set("makeCode", "V");
params.set("descriptionText", "");
params.set("languageCode", "en-US");
params.set("dealerCode", "408083");
mvc.perform(get("/upg-admin-controller/maintenence-intervals-by-metadata")
.contentType(MediaType.APPLICATION_JSON)
.params(params))
.andExpect(status().isAccepted());
}
#Test
public void getMaintenenceIntervalsByMetadata400Test()
throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.set("vinModelYear", "2000");
params.set("vinModelCode", "8727R9");
params.set("vinEngineCode", "GTAV");
params.set("interval", "100000");
params.set("vinTransmissionCode", "");
params.set("importerNumber", "");
params.set("makeCode", "T");
params.set("descriptionText", "");
params.set("languageCode", "sp-MX");
params.set("dealerCode", "120021");
mvc.perform(get("/upg-admin-controller/maintenence-intervals-by-metadata")
.contentType(MediaType.APPLICATION_JSON)
.params(params))
.andExpect(status().isBadRequest());
}
Error:
Error: java.lang.AssertionError: Status expected:<202> but was:<400>.
I have been trying to fix it but cannot find a solution. Using EclEmma extension on Eclipse. (sorry if the code is out of line. The text box is small it splits one line of code into two lines.)
Also this is the Controller code that I am working with that has the QueryParams.
#RequestMapping(value = "/maintenence-intervals-by-metadata", method = RequestMethod.GET)
public ResponseEntity<List<AdminMaintenanceIntervalReponse>> findMaintenenceIntervalsByMetadata( #QueryParam("modelYear") String modelYear,
#QueryParam("modelCode") String modelCode, #QueryParam("engineCode") String engineCode, #QueryParam("interval") String interval ,
#QueryParam("transmissionCode") String transmissionCode , #QueryParam("importer") String importer, #QueryParam("make") String make,
#QueryParam("descriptionText") String descriptionText, #QueryParam("languageCode") String languageCode, #QueryParam("dealerCode") String dealerCode, #QueryParam("brand") String Brand) throws MPMSException {
LOGGER.log(Level.INFO, "Entered UPGAdminServiceController, getAllMaintenenceIntervalsByMetadata");
LOGGER.log(Level.INFO, "modelYear =" + modelYear +" modelCode = " + modelCode +" engineCode = " + engineCode +" interval = " + interval + "transmissionCode = " + transmissionCode + "importer = " + importer + "make = " + make + "descriptionText = " + descriptionText);
List<AdminMaintenanceIntervalReponse> allMaintenanceIntervalsList = new ArrayList<AdminMaintenanceIntervalReponse>();
try{
Integer modelYearParam = null;
if (modelYear!=null){
modelYearParam = Integer.parseInt(modelYear);
}
Integer intervalParam = null;
if (interval!=null){
intervalParam = Integer.parseInt(interval);
}
String modelCodeParam = null;
if (modelCode!=null){
modelCodeParam = String.valueOf(modelCode);
}
String engineCodeParam = null;
if (engineCode!=null){
engineCodeParam = String.valueOf(engineCode);
}
String transmissionCodeParam = null;
if (transmissionCode!=null){
transmissionCodeParam = String.valueOf(transmissionCode);
}
Integer importerParam = null;
if (importer!=null){
importerParam = Integer.parseInt(importer);
}
String makeParam = null;
if (make!=null){
makeParam = String.valueOf(make);
}
if (descriptionText!=null){
String.valueOf(descriptionText);
}
allMaintenanceIntervalsList = upgAdminMaintenanceCalcService.findMaintenanceIntervalsByMetadata(modelYearParam, modelCodeParam, engineCodeParam, intervalParam, transmissionCodeParam, importerParam, makeParam, descriptionText, languageCode, dealerCode);
} catch(MPMSException e){
throw e;
} catch (Exception e) {
throw new MPMSException(ErrorConstants.UNKNOWN.getErrorCode(), "No Data Available", ErrorConstants.UNKNOWN.toString(), e);
}
return new ResponseEntity<List<AdminMaintenanceIntervalReponse>>(allMaintenanceIntervalsList, HttpStatus.OK);
}
Can someone please help me correct this issue.

Your /maintenence-intervals-by-metadata endpoint has the following query parameters:
#QueryParam("modelYear")
#QueryParam("modelCode")
#QueryParam("engineCode")
#QueryParam("interval")
#QueryParam("transmissionCode")
#QueryParam("importer")
#QueryParam("make")
#QueryParam("descriptionText")
#QueryParam("languageCode")
#QueryParam("dealerCode")
#QueryParam("brand")
But your test is submitting a [GET] request to /maintenence-intervals-by-metadata with the following named parameters:
params.set("vinModelYear", "2016");
params.set("vinModelCode", "1633F6");
params.set("vinEngineCode", "CZTA");
params.set("interval", "100000");
params.set("vinTransmissionCode", "");
params.set("importerNumber", "");
params.set("makeCode", "V");
params.set("descriptionText", "");
params.set("languageCode", "en-US");
params.set("dealerCode", "408083");
So, the query params you supply do not match the query params expected by the /maintenence-intervals-by-metadata endpoint. There are name mismatches:
modelYear vs. vinModelYear
modelCode vs. vinModelCode
... etc
And at least one query parameter is not supplied: the endpoint declares #QueryParam("brand") but you are not supplying a parameter named "brand".
I suspect the message associated with the 400 error might include something like: Required String parameter '...' is not present.
If you change your invocation such that every one of the query parameters defined by the /maintenence-intervals-by-metadata endpoint has a supplied parameter value of the correct type (a String) then I think the 400 will no longer occur.

Related

google dialogflow how to query subset of knowledgebases in a mega bot

I have a mega bot eg: projectID_stage with id=stage-aahedc and there are two sub agents: projectID_automated id=stageautomated-spiccu and projectC.
the projectID_automated has knowledge base enabled. and it's included in the projectID_stage.
The problem:
When I trying to do a detectIntent request with some part of knowledgebase set. it failed because of permission denied.
the code is similar with - https://github.com/googleapis/java-dialogflow/blob/master/samples/snippets/src/main/java/com/example/dialogflow/DetectIntentKnowledge.java (except I added two knowledge names)
import com.google.cloud.dialogflow.v2beta1.*;
import com.google.common.collect.*;
import java.io.*;
import java.util.*;
public class DetectIntentTexts {
// DialogFlow API Detect Intent sample with text inputs.
public static Map<String, QueryResult> detectIntentTexts(
String projectId, List<String> texts, String sessionId, String languageCode, String... kbNames)
throws IOException {
Map<String, QueryResult> queryResults = Maps.newHashMap();
// Instantiates a client
try (SessionsClient sessionsClient = SessionsClient.create()) {
// Set the session name using the sessionId (UUID) and projectID (my-project-id)
SessionName session = SessionName.of(projectId, sessionId);
System.out.println("Session Path: " + session.toString());
// Detect intents for each text input
for (String text : texts) {
// Set the text (hello) and language code (en-US) for the query
TextInput.Builder textInput =
TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
// Build the query with the TextInput
QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
QueryParameters queryParameters =
QueryParameters.newBuilder().addKnowledgeBaseNames(kbNames[0])
.addKnowledgeBaseNames(kbNames[1]).build();
DetectIntentRequest detectIntentRequest =
DetectIntentRequest.newBuilder()
.setSession(session.toString())
.setQueryInput(queryInput)
.setQueryParams(queryParameters)
.build();
// Performs the detect intent request
DetectIntentResponse response = sessionsClient.detectIntent(detectIntentRequest);
// Display the query result
QueryResult queryResult = response.getQueryResult();
System.out.println("====================");
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
System.out.format(
"Detected Intent: %s (confidence: %f)\n",
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
System.out.format(
"Fulfillment Text: '%s'\n",
queryResult.getFulfillmentMessagesCount() > 0
? queryResult.getFulfillmentMessages(0).getText()
: "Triggered Default Fallback Intent");
queryResults.put(text, queryResult);
}
}
return queryResults;
}
}
// [END dialogflow_detect_intent_text]
and the main function is like below:
String projectID_automated = "stageautomated-spiccu";
String projectID_stage = "stage-aahedc";
String finalProjectID = projectID_stage;
String sessionID = "123456789";
String language = "en-US";
List<String> text = Arrays.asList("is it safe for my family and pets");
// when you trying to list out the kbs....
// listKbs(projectID);
// projects/<Project ID>/knowledgeBases/<Knowledge Base ID>`
String kb1 = "pest control-1", kb2 = "pest control-2";
String full1 = "projects/" + projectID_automated + "/knowledgeBases/MTE0NTc1ODUxNjIwNTM3NDY2ODg";
String full2 = "projects/" + projectID_automated + "/knowledgeBases/Njg0NTg5OTE0MzYyNjM1ODc4NA";
Map<String, QueryResult> r = DetectIntentTexts.detectIntentTexts(finalProjectID, text, sessionID, language, full1, full2);
System.out.println(r);
When using the projectID_automated for full1 and full2, it failed with below error:
Caused by: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Project 'stageautomated-spiccu' in knowledge_base_names does not match project 'stage-aahedc' in session.
When using the projectID_stage for full1 and full2,
String full1 = "projects/" + projectID_stage + "/knowledgeBases/MTE0NTc1ODUxNjIwNTM3NDY2ODg";
String full2 = "projects/" + projectID_stage + "/knowledgeBases/Njg0NTg5OTE0MzYyNjM1ODc4NA";
it failed with below error in the query result
diagnostic_info {
fields {
key: "knowledge_connector_error"
value {
string_value: "INVALID_ARGUMENT: UNAUTHORIZED: You can\'t query KnowledgeBase under project: stage-aahedc, which is different than your agent project: stageautomated-spiccu"
}
}
Environment details
dialogflow#detectIntent
OS type and version: macos 10
Java version: jdk11
dialogflow version(s): dialogflow es + 'com.google.api.grpc:proto-google-cloud-dialogflow-v2beta1:0.99.5'
and 'com.google.cloud:google-cloud-dialogflow:4.1.5'

How to get data from query string in Java Spring boot REST

We have a requirement to read data after '?' in service-url in Spring boot REST API.
For example, We exposed a service called sampleService and GET URL for this is
http://www.myservices.com/api/sampleServce
And clients will pass the data as http://www.myservices.com/api/sampleServce?dynamicdata
So we have to read that "dynamicdata" in my sample service and process.
Please let me know the possibilities.
GET: http://localhost:8080/api/foos?id=abc here the query string is id=abc . Now to extract the value of id, you can use the code something like this.
#GetMapping("/api/foos")
#ResponseBody
public String getFoos(#RequestParam String id) {
return "ID: " + id;
}
GET: http://www.myservices.com/api/sampleServce?dynamicdata is incorrect. Either it should be http://www.myservices.com/api/sampleServce/dynamicdata (PathVariable) or http://www.myservices.com/api/sampleServce?title=dynamicdata (RequestParam)
GET: http://www.myservices.com/api/sampleServce/dynamicdata to extract dynamicdata, you can use code something like
#GetMapping("/api/sampleServce/{id}")
#ResponseBody
public String getFooById(#PathVariable String id) {
return "ID: " + id; // here id = "dynamicdata"
}
GET: http://www.myservices.com/api/sampleServce?title=dynamicdata to extract title, you can use code something like
#GetMapping("/api/sampleServce")
#ResponseBody
public String getFoos(#RequestParam String title) {
return "title: " + title; // title="dynamicdata"
}
dynamicdata is path param, it cannot be placed after ?. It should be something like this:
http://www.myservices.com/api/dynamicdata/sampleServce
Check when and how to use query or path parameters
Accept dynamic data through request param:
#GetMapping("/api/sampleServce")
public void test(#RequestParam Map<String, String> dynamicdata) {
System.out.println(dynamicdata.keySet()); //http://localhost:9001/dag-backend/api/sampleServce?test&test11
Optional<String> data = dynamicdata.keySet().stream().findFirst();
String value = data.isPresent() ? data.get() : null;
System.out.println(value); //http://localhost:9001/dag-backend/api/sampleServce?test
}
URLs:
http://www.myservices.com/api/sampleServce?dynamicdata
http://www.myservices.com/api/sampleServce?dynamicdata&12344&1212
http://www.myservices.com/api/sampleServce?$999900&124434&234
You can add an HttpServletRequest object to your mapped method signature:
#RequestMapping("/api/sampleServce")
public Object sampleServce (HttpServletRequest request) {
//print everything after the question mark:
System.out.println("queryString: " + request.getQueryString());
//print key value pairs:
Enumeration<String> params = request.getParameterNames();
while(params.hasMoreElements()){
String paramName = params.nextElement();
String paramValue = request.getParameter(paramName);
System.out.println("name: " + paramName);
System.out.println("value: " + paramValue);
}
return request.getQueryString();
}

Domain Restriction in BabelNet

I am trying to restrict the results of my BabelNet query to a specific (Babel)domain. To do that, I'm trying to find out a way to compare the synsets' domains with the domain I need (Geographical). However, I'm having trouble getting the right output, since although the 2 strings match, it still gives me the wrong output. I'm surely doing something wrong here, but I'm out of ideas.
After many trials, the following code was the one that gave me the nearest result to the desired output:
public class GeoRestrict {
public static void main(String[] args) throws IOException {
String file = "/path/to/file/testdata.txt";
BabelNet bn = BabelNet.getInstance();
BufferedReader br = new BufferedReader(new FileReader(file));
String word = null;
while ((word = br.readLine()) != null) {
BabelNetQuery query = new BabelNetQuery.Builder(word)
.build();
List<BabelSynset> wordSynset = bn.getSynsets(query);
for (BabelSynset synset : wordSynset) {
BabelSynsetID id = synset.getID();
System.out.println("\n" + "Synset ID for " + word.toUpperCase() + " is: " + id);
HashMap<Domain, Double> domains = synset.getDomains();
Set<Domain> keys = domains.keySet();
String keyString = domains.keySet().toString();
List<String> categories = synset.getDomains().keySet().stream()
.map(domain -> ((BabelDomain) domain).getDomainString())
.collect(Collectors.toList());
for (String category : categories) {
if(keyString.equals(category)) {
System.out.println("The word " + word + " has the domain " + category);
} else {
System.out.println("Nada! " + category);
}
}
}
}
br.close();
}
}
The output looks like this:
Synset ID for TURIN is: bn:00077665n
Nada! Geography and places
Any ideas on how to solve this issue?
I found my own error. For the sake of completeness I'm posting it.
The BabelDomain needs to be declared and specified (before the while-loop), like this:
BabelDomain domain = BabelDomain.GEOGRAPHY_AND_PLACES;

Getting URL parameter in java and extract a specific text from that URL

I have a URL and I need to get the value of v from this URL.
Here is my URL: http://www.youtube.com/watch?v=_RCIP6OrQrE
How can I do that?
I think the one of the easiest ways out would be to parse the string returned by URL.getQuery() as
public static Map<String, String> getQueryMap(String query) {
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params) {
String name = param.split("=")[0];
String value = param.split("=")[1];
map.put(name, value);
}
return map;
}
You can use the map returned by this function to retrieve the value keying in the parameter name.
If you're on Android, you can do this:
Uri uri = Uri.parse(url);
String v = uri.getQueryParameter("v");
I have something like this:
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
private String getParamValue(String link, String paramName) throws URISyntaxException {
List<NameValuePair> queryParams = new URIBuilder(link).getQueryParams();
return queryParams.stream()
.filter(param -> param.getName().equalsIgnoreCase(paramName))
.map(NameValuePair::getValue)
.findFirst()
.orElse("");
}
I wrote this last month for Joomla Module when implementing youtube videos (with the Gdata API). I've since converted it to java.
Import These Libraries
import java.net.URL;
import java.util.regex.*;
Copy/Paste this function
public String getVideoId( String videoId ) throws Exception {
String pattern = "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(videoId);
int youtu = videoId.indexOf("youtu");
if(m.matches() && youtu != -1){
int ytu = videoId.indexOf("http://youtu.be/");
if(ytu != -1) {
String[] split = videoId.split(".be/");
return split[1];
}
URL youtube = new URL(videoId);
String[] split = youtube.getQuery().split("=");
int query = split[1].indexOf("&");
if(query != -1){
String[] nSplit = split[1].split("&");
return nSplit[0];
} else return split[1];
}
return null; //throw something or return what you want
}
URL's it will work with
http://www.youtube.com/watch?v=k0BWlvnBmIE (General URL)
http://youtu.be/k0BWlvnBmIE (Share URL)
http://www.youtube.com/watch?v=UWb5Qc-fBvk&list=FLzH5IF4Lwgv-DM3CupM3Zog&index=2 (Playlist URL)
Import these libraries
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
Similar to the verisimilitude, but with the capabilities of handling multivalue parameters. Note: I've seen HTTP GET requests without a value, in this case the value will be null.
public static List<NameValuePair> getQueryMap(String query)
{
List<NameValuePair> queryMap = new ArrayList<NameValuePair>();
String[] params = query.split(Pattern.quote("&"));
for (String param : params)
{
String[] chunks = param.split(Pattern.quote("="));
String name = chunks[0], value = null;
if(chunks.length > 1) {
value = chunks[1];
}
queryMap.add(new BasicNameValuePair(name, value));
}
return queryMap;
}
Example:
GET /bottom.gif?e235c08=1509896923&%49%6E%...
Using pure Java 8
Assumming you want to extract param "v" from url:
String paramV = Stream.of(url.split("?")[1].split("&"))
.map(kv -> kv.split("="))
.filter(kv -> "v".equalsIgnoreCase(kv[0]))
.map(kv -> kv[1])
.findFirst()
.orElse("");
Assuming the URL syntax will always be http://www.youtube.com/watch?v= ...
String v = "http://www.youtube.com/watch?v=_RCIP6OrQrE".substring(31);
or disregarding the prefix syntax:
String url = "http://www.youtube.com/watch?v=_RCIP6OrQrE";
String v = url.substring(url.indexOf("v=") + 2);
I believe we have a better approach to answer this question.
1: Define a function that returns Map values.
Here we go.
public Map<String, String> getUrlValues(String url) throws UnsupportedEncodingException {
int i = url.indexOf("?");
Map<String, String> paramsMap = new HashMap<>();
if (i > -1) {
String searchURL = url.substring(url.indexOf("?") + 1);
String params[] = searchURL.split("&");
for (String param : params) {
String temp[] = param.split("=");
paramsMap.put(temp[0], java.net.URLDecoder.decode(temp[1], "UTF-8"));
}
}
return paramsMap;
}
2: Call your function surrounding with a try catch block
Here we go
try {
Map<String, String> values = getUrlValues("https://example.com/index.php?form_id=9&page=1&view_id=78");
String formId = values.get("form_id");
String page = values.get("page");
String viewId = values.get("view_id");
Log.d("FormID", formId);
Log.d("Page", page);
Log.d("ViewID", viewId);
} catch (UnsupportedEncodingException e) {
Log.e("Error", e.getMessage());
}
If you are using Jersey (which I was, my server component needs to make outbound HTTP requests) it contains the following public method:
var multiValueMap = UriComponent.decodeQuery(uri, true);
It is part of org.glassfish.jersey.uri.UriComponent, and the javadoc is here. Whilst you may not want all of Jersey, it is part of the Jersey common package which isn't too bad on dependencies...
I solved the problem like this
public static String getUrlParameterValue(String url, String paramName) {
String value = "";
List<NameValuePair> result = null;
try {
result = URLEncodedUtils.parse(new URI(url), UTF_8);
value = result.stream().filter(pair -> pair.getName().equals(paramName)).findFirst().get().getValue();
System.out.println("--------------> \n" + paramName + " : " + value + "\n");
} catch (URISyntaxException e) {
e.printStackTrace();
}
return value;
}
this will work for all sort of youtube url :
if url could be
youtube.com/?v=_RCIP6OrQrE
youtube.com/v/_RCIP6OrQrE
youtube.com/watch?v=_RCIP6OrQrE
youtube.com/watch?v=_RCIP6OrQrE&feature=whatever&this=that
Pattern p = Pattern.compile("http.*\\?v=([a-zA-Z0-9_\\-]+)(?:&.)*");
String url = "http://www.youtube.com/watch?v=_RCIP6OrQrE";
Matcher m = p.matcher(url.trim()); //trim to remove leading and trailing space if any
if (m.matches()) {
url = m.group(1);
}
System.out.println(url);
this will extract video id from your url
further reference
My solution mayble not good
String url = "https://www.youtube.com/watch?param=test&v=XcHJMiSy_1c&lis=test";
int start = url.indexOf("v=")+2;
// int start = url.indexOf("list=")+5; **5 is length of ("list=")**
int end = url.indexOf("&", start);
end = (end == -1 ? url.length() : end);
System.out.println(url.substring(start, end));
// result: XcHJMiSy_1c
work fine with:
https://www.youtube.com/watch?param=test&v=XcHJMiSy_1c&lis=test
https://www.youtube.com/watch?v=XcHJMiSy_1c
public static String getQueryMap(String query) {
String[] params = query.split("&");
for (String param : params) {
String name = param.split("=")[0];
if ("YourParam".equals(name)) {
return param.split("=")[1];
}
}
return null;
}

Java regex pattern to remove a parameter from query string

I am looking for removing foo parameter and its value from all the possible following query strings in Java.
Is there a regex pattern to do this?
http://localhost/test?foo=abc&foobar=def
http://localhost/test?foobar=def&foo=abc
http://localhost/test?foo=abc
http://localhost/test?foobar=def&foo=abc&foobar2=def
The resulting strings would be
http://localhost/test?foobar=def
http://localhost/test?foobar=def
http://localhost/test
http://localhost/test?foobar=def&foobar2=def
This regex should match the GET param and its value...
(?<=[?&;])foo=.*?($|[&;])
RegExr.
Just replace it with an empty string.
For reference, there is a better (Perl) regex available in this other question: Regular expression to remove one parameter from query string
In Java, this can be implemented as follows:
public static String removeParams(String queryString, String... params) {
for (String param : params) {
String keyValue = param + "=[^&]*?";
queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
}
return queryString;
}
This is an extension of the answer provided by mchr.
It allows params to be removed from an url and from a query string, and shows how to execute the javascript test cases he mentions. Since it uses a regex, it will return the url with all the other parameters exactly where they were before. This is useful if you want to remove some parameters from an url when "signing" an url ;-) Note that this does not remove any completely empty parameters.
e.g. it will not remove foo from /test?foo&me=52
The test cases listed below remove the parameters, "foo" and "test" when found in the query string.
You can test it out line here at Repl.it
class Main {
public static void main(String[] args) {
runTests();
}
public static void runTests() {
test("foo=%2F{}/me/you&me=52", "me=52");
test("?foo=%2F{}/me/you&me=52", "?me=52");
test("?foo=52&me=able was i ere&test=2", "?me=able was i ere");
test("foo=", "");
test("?", "");
test("?foo=52", "");
test("test?", "test");
test("test?foo=23", "test");
test("foo=&bar=456", "bar=456");
test("bar=456&foo=", "bar=456");
test("abc=789&foo=&bar=456", "abc=789&bar=456");
test("foo=123", "");
test("foo=123&bar=456", "bar=456");
test("bar=456&foo=123", "bar=456");
test("abc=789&foo=123&bar=456", "abc=789&bar=456");
test("xfoo", "xfoo");
test("xfoo&bar=456", "xfoo&bar=456");
test("bar=456&xfoo", "bar=456&xfoo");
test("abc=789&xfoo&bar=456", "abc=789&xfoo&bar=456");
test("xfoo=", "xfoo=");
test("xfoo=&bar=456", "xfoo=&bar=456");
test("bar=456&xfoo=", "bar=456&xfoo=");
test("abc=789&xfoo=&bar=456", "abc=789&xfoo=&bar=456");
test("xfoo=123", "xfoo=123");
test("xfoo=123&bar=456", "xfoo=123&bar=456");
test("bar=456&xfoo=123", "bar=456&xfoo=123");
test("abc=789&xfoo=123&bar=456", "abc=789&xfoo=123&bar=456");
test("foox", "foox");
test("foox&bar=456", "foox&bar=456");
test("bar=456&foox", "bar=456&foox");
test("abc=789&foox&bar=456", "abc=789&foox&bar=456");
test("foox=", "foox=");
test("foox=&bar=456", "foox=&bar=456");
test("bar=456&foox=", "bar=456&foox=");
test("abc=789&foox=&bar=456", "abc=789&foox=&bar=456");
test("foox=123", "foox=123");
test("foox=123&bar=456", "foox=123&bar=456");
test("bar=456&foox=123", "bar=456&foox=123");
test("abc=789&foox=123&bar=456", "abc=789&foox=123&bar=456");
}
public static void test (String input, String expected) {
String result = removeParamsFromUrl(input, "foo", "test");
if (! result.equals(expected))
throw new RuntimeException("Failed:" + input);
System.out.println("Passed:" + input + ", output:" + result);
}
public static String removeParamsFromQueryString(String queryString, String... params) {
for (String param : params) {
String keyValue = param + "=[^&]*?";
queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
}
return queryString;
}
public static String removeParamsFromUrl(String url, String... params) {
String queryString;
String baseUrl;
int index = url.indexOf("?");
boolean wasFullUrl = (index != -1);
if (wasFullUrl)
{
baseUrl = url.substring(0, index);
queryString = url.substring(index+1);
}
else
{
baseUrl = "";
queryString = url;
}
String newQueryString = removeParamsFromQueryString(queryString, params);
String result;
if (wasFullUrl)
{
boolean isEmpty = newQueryString == null || newQueryString.equals("");
result = isEmpty ? baseUrl : baseUrl + "?" + newQueryString;
}
else
{
result = newQueryString;
}
return result;
}
}
url=url.replaceAll("(&"+param+"=[^&]*\$)|(\\?"+param+"=[^&]*\$)|("+param+"=[^&]*&)","")

Categories