@ -21,6 +21,7 @@ import java.net.URI;
@@ -21,6 +21,7 @@ import java.net.URI;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import org.assertj.core.api.InstanceOfAssertFactories ;
import org.junit.jupiter.api.Test ;
@ -42,6 +43,7 @@ import static org.assertj.core.api.Assertions.fail;
@@ -42,6 +43,7 @@ import static org.assertj.core.api.Assertions.fail;
/ * *
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Nicklas Wiegandt
* /
public class RestClientBuilderTests {
@ -137,6 +139,123 @@ public class RestClientBuilderTests {
@@ -137,6 +139,123 @@ public class RestClientBuilderTests {
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > builder . messageConverters ( converters ) ) ;
}
@Test
void defaultCookieAddsCookieToDefaultCookiesMap ( ) {
RestClient . Builder builder = RestClient . builder ( ) ;
builder . defaultCookie ( "myCookie" , "testValue" ) ;
assertThat ( fieldValue ( "defaultCookies" , ( DefaultRestClientBuilder ) builder ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly ( Map . entry ( "myCookie" , List . of ( "testValue" ) ) ) ;
}
@Test
void defaultCookieWithMultipleValuesAddsCookieToDefaultCookiesMapWithAllValues ( ) {
RestClient . Builder builder = RestClient . builder ( ) ;
builder . defaultCookie ( "myCookie" , "testValue1" , "testValue2" ) ;
assertThat ( fieldValue ( "defaultCookies" , ( DefaultRestClientBuilder ) builder ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly ( Map . entry ( "myCookie" , List . of ( "testValue1" , "testValue2" ) ) ) ;
}
@Test
void defaultCookiesAllowsToAddCookie ( ) {
RestClient . Builder builder = RestClient . builder ( ) ;
builder . defaultCookie ( "firstCookie" , "firstValue" ) ;
builder . defaultCookies ( cookies - > cookies . add ( "secondCookie" , "secondValue" ) ) ;
assertThat ( fieldValue ( "defaultCookies" , ( DefaultRestClientBuilder ) builder ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly (
Map . entry ( "firstCookie" , List . of ( "firstValue" ) ) ,
Map . entry ( "secondCookie" , List . of ( "secondValue" ) )
) ;
}
@Test
void defaultCookiesAllowsToRemoveCookie ( ) {
RestClient . Builder builder = RestClient . builder ( ) ;
builder . defaultCookie ( "firstCookie" , "firstValue" ) ;
builder . defaultCookie ( "secondCookie" , "secondValue" ) ;
builder . defaultCookies ( cookies - > cookies . remove ( "firstCookie" ) ) ;
assertThat ( fieldValue ( "defaultCookies" , ( DefaultRestClientBuilder ) builder ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly ( Map . entry ( "secondCookie" , List . of ( "secondValue" ) ) ) ;
}
@Test
void copyConstructorCopiesDefaultCookies ( ) {
DefaultRestClientBuilder sourceBuilder = new DefaultRestClientBuilder ( ) ;
sourceBuilder . defaultCookie ( "firstCookie" , "firstValue" ) ;
sourceBuilder . defaultCookie ( "secondCookie" , "secondValue" ) ;
DefaultRestClientBuilder copiedBuilder = new DefaultRestClientBuilder ( sourceBuilder ) ;
assertThat ( fieldValue ( "defaultCookies" , copiedBuilder ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly (
Map . entry ( "firstCookie" , List . of ( "firstValue" ) ) ,
Map . entry ( "secondCookie" , List . of ( "secondValue" ) )
) ;
}
@Test
void copyConstructorCopiesDefaultCookiesImmutable ( ) {
DefaultRestClientBuilder sourceBuilder = new DefaultRestClientBuilder ( ) ;
sourceBuilder . defaultCookie ( "firstCookie" , "firstValue" ) ;
sourceBuilder . defaultCookie ( "secondCookie" , "secondValue" ) ;
DefaultRestClientBuilder copiedBuilder = new DefaultRestClientBuilder ( sourceBuilder ) ;
sourceBuilder . defaultCookie ( "thirdCookie" , "thirdValue" ) ;
assertThat ( fieldValue ( "defaultCookies" , copiedBuilder ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly (
Map . entry ( "firstCookie" , List . of ( "firstValue" ) ) ,
Map . entry ( "secondCookie" , List . of ( "secondValue" ) )
) ;
}
@Test
void buildCopiesDefaultCookies ( ) {
RestClient . Builder builder = RestClient . builder ( ) ;
builder . defaultCookie ( "firstCookie" , "firstValue" ) ;
builder . defaultCookie ( "secondCookie" , "secondValue" ) ;
RestClient restClient = builder . build ( ) ;
assertThat ( fieldValue ( "defaultCookies" , restClient ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly (
Map . entry ( "firstCookie" , List . of ( "firstValue" ) ) ,
Map . entry ( "secondCookie" , List . of ( "secondValue" ) )
) ;
}
@Test
void buildCopiesDefaultCookiesImmutable ( ) {
RestClient . Builder builder = RestClient . builder ( ) ;
builder . defaultCookie ( "firstCookie" , "firstValue" ) ;
builder . defaultCookie ( "secondCookie" , "secondValue" ) ;
RestClient restClient = builder . build ( ) ;
builder . defaultCookie ( "thirdCookie" , "thirdValue" ) ;
builder . defaultCookie ( "firstCookie" , "fourthValue" ) ;
assertThat ( fieldValue ( "defaultCookies" , restClient ) )
. asInstanceOf ( InstanceOfAssertFactories . MAP )
. containsExactly (
Map . entry ( "firstCookie" , List . of ( "firstValue" ) ) ,
Map . entry ( "secondCookie" , List . of ( "secondValue" ) )
) ;
}
@Nullable
private static Object fieldValue ( String name , DefaultRestClientBuilder instance ) {
try {
@ -150,4 +269,18 @@ public class RestClientBuilderTests {
@@ -150,4 +269,18 @@ public class RestClientBuilderTests {
return null ;
}
}
@Nullable
private static Object fieldValue ( String name , RestClient instance ) {
try {
Field field = DefaultRestClient . class . getDeclaredField ( name ) ;
field . setAccessible ( true ) ;
return field . get ( instance ) ;
}
catch ( NoSuchFieldException | IllegalAccessException ex ) {
fail ( ex . getMessage ( ) , ex ) ;
return null ;
}
}
}