Skip to content

Commit

Permalink
feat: adds addOrUpdate to queryParamBuilder (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilkg authored Jan 15, 2020
1 parent 80b6964 commit 4c75c14
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
50 changes: 47 additions & 3 deletions src/main/java/org/hisp/dhis/request/QueryParamsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}

/**
Expand All @@ -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<String, String> pair = getByKey( key );

Expand All @@ -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 )
Expand Down
56 changes: 52 additions & 4 deletions src/test/java/org/hisp/dhis/request/QueryParamsBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class QueryParamsBuilderTest
{
@Test
public void shouldAddKeyAndValue()
public void addShouldAddKeyAndValue()
{
String queryParams = new QueryParamsBuilder().add( "key", "value" )
.add( "anotherKey", "anotherValue" )
Expand All @@ -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();
Expand All @@ -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" ) );
}
}

0 comments on commit 4c75c14

Please sign in to comment.