From ae52ba39681a811d15aaf86cbd18c4a68740d6cd Mon Sep 17 00:00:00 2001 From: sam-gardner <53271849+sam-gardner@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:02:41 +0100 Subject: [PATCH] BAEL-5771 Add code for url query manipulation in Java (#13820) --- .../core-java-networking-4/pom.xml | 24 +++++++++ .../UrlQueryManipulationUnitTest.java | 53 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 core-java-modules/core-java-networking-4/src/test/java/com/baeldung/urlquerymanipulation/UrlQueryManipulationUnitTest.java diff --git a/core-java-modules/core-java-networking-4/pom.xml b/core-java-modules/core-java-networking-4/pom.xml index 5fca4a1cab..cbe6356d0f 100644 --- a/core-java-modules/core-java-networking-4/pom.xml +++ b/core-java-modules/core-java-networking-4/pom.xml @@ -24,6 +24,30 @@ jsoup ${jsoup.version} + + + org.apache.httpcomponents + httpclient + 4.5.2 + + + + javax.ws.rs + javax.ws.rs-api + 2.1.1 + + + org.glassfish.jersey.core + jersey-common + 2.22.2 + test + + + + org.springframework + spring-web + 6.0.6 + diff --git a/core-java-modules/core-java-networking-4/src/test/java/com/baeldung/urlquerymanipulation/UrlQueryManipulationUnitTest.java b/core-java-modules/core-java-networking-4/src/test/java/com/baeldung/urlquerymanipulation/UrlQueryManipulationUnitTest.java new file mode 100644 index 0000000000..cc53a3e3a8 --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/test/java/com/baeldung/urlquerymanipulation/UrlQueryManipulationUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.urlquerymanipulation; + +import static junit.framework.TestCase.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; + +import javax.ws.rs.core.UriBuilder; + +import org.apache.http.client.utils.URIBuilder; + +import org.junit.Test; +import org.springframework.web.util.UriComponentsBuilder; + +public class UrlQueryManipulationUnitTest { + + @Test + public void whenUsingApacheUriBuilder_thenParametersAreCorrectlyAdded() throws URISyntaxException { + String url = "baeldung.com"; + String key = "article"; + String value = "alpha"; + URI uri = new URIBuilder(url).addParameter(key, value) + .build(); + + assertEquals("baeldung.com?article=alpha", uri.toString()); + } + + @Test + public void whenUsingJavaUriBuilder_thenParametersAreCorrectlyAdded() { + String url = "baeldung.com"; + String key = "article"; + String value = "beta"; + URI uri = UriBuilder.fromUri(url) + .queryParam(key, value) + .build(); + + assertEquals("baeldung.com?article=beta", uri.toString()); + } + + @Test + public void whenUsingSpringUriComponentsBuilder_thenParametersAreCorrectlyAdded() { + String url = "baeldung.com"; + String key = "article"; + String value = "charlie"; + URI uri = UriComponentsBuilder.fromUriString(url) + .queryParam(key, value) + .build() + .toUri(); + + assertEquals("baeldung.com?article=charlie", uri.toString()); + } + +}