AuthTokens { public static AuthToken custom( String principal, String credentials, String realm, String scheme) { return custom( principal, credentials, realm, scheme, null ); }  static AuthToken basic( String username, String password ); static AuthToken basic( String username, String password, String realm ); static AuthToken kerberos( String base64EncodedTicket ); static AuthToken custom( String principal, String credentials, String realm, String scheme); static AuthToken custom( String principal, String credentials, String realm, String scheme, Map<String, Object> parameters); static AuthToken none();  }
@Test void shouldNotAllowCustomAuthTokenWithNullCredentials() { NullPointerException e = assertThrows( NullPointerException.class, () -> AuthTokens.custom( "principal", null, "realm", "scheme" ) ); assertEquals( "Credentials can't be null", e.getMessage() ); }
@Test void shouldAllowCustomAuthTokenWithNullRealm() { AuthToken token = AuthTokens.custom( "principal", "credentials", null, "scheme" ); Map<String,Value> map = ((InternalAuthToken) token).toMap(); assertEquals( 3, map.size() ); assertEquals( "scheme", map.get( "scheme" ).asString() ); assertEquals( "principal", map.get( "principal" ).asString() ); assertEquals( "credentials", map.get( "credentials" ).asString() ); }
@Test void shouldNotAllowCustomAuthTokenWithNullScheme() { NullPointerException e = assertThrows( NullPointerException.class, () -> AuthTokens.custom( "principal", "credentials", "realm", null ) ); assertEquals( "Scheme can't be null", e.getMessage() ); }
@Test void customAuthWithoutParameters() { InternalAuthToken basic = (InternalAuthToken) custom( "foo", "bar", "baz", "my_scheme" ); Map<String,Value> map = basic.toMap(); assertThat( map.size(), equalTo( 4 ) ); assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "my_scheme" ) ) ); assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) ); assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) ); assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) ); }
@Test void customAuthParameters() { HashMap<String,Object> parameters = new HashMap<>(); parameters.put( "list", asList( 1, 2, 3 ) ); InternalAuthToken basic = (InternalAuthToken) custom( "foo", "bar", "baz", "my_scheme", parameters ); Map<String,Value> expectedParameters = new HashMap<>(); expectedParameters.put( "list", new ListValue( values( 1, 2, 3 ) ) ); Map<String,Value> map = basic.toMap(); assertThat( map.size(), equalTo( 5 ) ); assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "my_scheme" ) ) ); assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) ); assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) ); assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) ); assertThat( map.get( "parameters" ), equalTo( (Value) new MapValue( expectedParameters ) ) ); }
@Test void shouldNotAllowCustomAuthTokenWithNullPrincipal() { NullPointerException e = assertThrows( NullPointerException.class, () -> AuthTokens.custom( null, "credentials", "realm", "scheme" ) ); assertEquals( "Principal can't be null", e.getMessage() ); }