From 4c75c141c09e7983ecdbec338ab51fe19a3e95ec Mon Sep 17 00:00:00 2001 From: Gintare Vilkelyte Date: Wed, 15 Jan 2020 11:04:41 +0100 Subject: [PATCH] feat: adds addOrUpdate to queryParamBuilder (#6) --- .../hisp/dhis/request/QueryParamsBuilder.java | 50 ++++++++++++++++- .../dhis/request/QueryParamsBuilderTest.java | 56 +++++++++++++++++-- 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/hisp/dhis/request/QueryParamsBuilder.java b/src/main/java/org/hisp/dhis/request/QueryParamsBuilder.java index b1f80a6..864c2d7 100644 --- a/src/main/java/org/hisp/dhis/request/QueryParamsBuilder.java +++ b/src/main/java/org/hisp/dhis/request/QueryParamsBuilder.java @@ -24,11 +24,11 @@ public QueryParamsBuilder() * @param param * @return */ - public QueryParamsBuilder add( String param ) + public QueryParamsBuilder addOrUpdate( String param ) { String[] split= param.split( "=" ); - return this.add( split[0], split[1] ); + return this.addOrUpdate( split[0], split[1] ); } /** @@ -39,7 +39,7 @@ public QueryParamsBuilder add( String param ) * @return */ - public QueryParamsBuilder add( String key, String value ) + public QueryParamsBuilder addOrUpdate( String key, String value ) { MutablePair pair = getByKey( key ); @@ -54,11 +54,55 @@ public QueryParamsBuilder add( String key, String value ) return this; } + /** + * Adds query param even when param with the same key exists. + * Format: key=value + * + * @param param + * @return + */ + public QueryParamsBuilder add( String param ) + { + String[] split= param.split( "=" ); + + return this.add( split[0], split[1] ); + } + + /** + * Adds the query param even when param with the same key exists. + * + * @param key + * @param value + * @return + */ + + public QueryParamsBuilder add( String key, String value ) + { + queryParams.add( MutablePair.of( key, value ) ); + + return this; + } + /** * Adds or updates the query params. * @param params * @return */ + public QueryParamsBuilder addOrUpdateAll( String... params ) + { + for ( String param : params ) + { + this.addOrUpdate( param ); + } + + return this; + } + + /** + * Adds the query params. + * @param params + * @return + */ public QueryParamsBuilder addAll( String... params ) { for ( String param : params ) diff --git a/src/test/java/org/hisp/dhis/request/QueryParamsBuilderTest.java b/src/test/java/org/hisp/dhis/request/QueryParamsBuilderTest.java index 0e9407a..3017cb0 100644 --- a/src/test/java/org/hisp/dhis/request/QueryParamsBuilderTest.java +++ b/src/test/java/org/hisp/dhis/request/QueryParamsBuilderTest.java @@ -11,7 +11,7 @@ public class QueryParamsBuilderTest { @Test - public void shouldAddKeyAndValue() + public void addShouldAddKeyAndValue() { String queryParams = new QueryParamsBuilder().add( "key", "value" ) .add( "anotherKey", "anotherValue" ) @@ -27,7 +27,22 @@ public void shouldAddKeyAndValue() } @Test - public void shouldAddAll() + public void addOrUpdateShouldAddKeyAndValue() { + String queryParams = new QueryParamsBuilder().addOrUpdate( "key", "value" ) + .addOrUpdate( "anotherKey", "anotherValue" ) + .build(); + + assertThat( queryParams, equalTo( "?key=value&anotherKey=anotherValue" ) ); + + queryParams = new QueryParamsBuilder().addOrUpdate( "key=value" ) + .addOrUpdate( "anotherKey=anotherValue" ) + .build(); + + assertThat( queryParams, equalTo( "?key=value&anotherKey=anotherValue" ) ); + } + + @Test + public void addShouldAddAll() { String queryParams = new QueryParamsBuilder().addAll( "key=value", "anotherKey=anotherValue" ) .build(); @@ -36,12 +51,45 @@ public void shouldAddAll() } @Test - public void shouldReplaceParam() + public void addOrUpdateShouldAddAll() { + String queryParams = new QueryParamsBuilder().addOrUpdateAll( "key=value", "anotherKey=anotherValue" ) + .build(); + + assertThat( queryParams, equalTo( "?key=value&anotherKey=anotherValue" ) ); + } + + @Test + public void addAllShouldNotUpdateParams() { + String queryParams = new QueryParamsBuilder().addAll( "key=value", "anotherKey=anotherValue", "anotherKey=yetAnotherValue" ) + .build(); + + assertThat( queryParams, equalTo( "?key=value&anotherKey=anotherValue&anotherKey=yetAnotherValue" ) ); + } + + @Test + public void addOrUpdateAllShouldUpdateParams() { + String queryParams = new QueryParamsBuilder().addOrUpdateAll( "key=value", "anotherKey=anotherValue", "anotherKey=yetAnotherValue" ) + .build(); + + assertThat( queryParams, equalTo( "?key=value&anotherKey=yetAnotherValue" ) ); + } + + @Test + public void addOrUpdateShouldUpdateParam() { String queryParams = new QueryParamsBuilder().add( "key", "value" ) - .add( "key", "anotherValue" ) + .addOrUpdate( "key", "anotherValue" ) .build(); assertThat( queryParams, equalTo( "?key=anotherValue" ) ); } + + @Test + public void addShouldNotUpdateParam() { + String queryParams = new QueryParamsBuilder().add( "key", "value" ) + .add( "key", "anotherValue" ) + .build(); + + assertThat( queryParams, equalTo( "?key=value&key=anotherValue" ) ); + } }