shiver.me.timbers.security.spring.AbstractNullSafeAuthenticationConverterTest.java Source code

Java tutorial

Introduction

Here is the source code for shiver.me.timbers.security.spring.AbstractNullSafeAuthenticationConverterTest.java

Source

/*
 * Copyright 2015 Karl Bennett
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package shiver.me.timbers.security.spring;

import org.junit.Test;
import org.springframework.security.core.Authentication;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

public class AbstractNullSafeAuthenticationConverterTest {

    @Test
    public void Can_safely_convert_non_null_principal() {

        // Given
        final Authentication expected = mock(Authentication.class);

        // When
        final Authentication actual = new AbstractNullSafeAuthenticationConverter<Object>() {
            @Override
            public Object convert(Authentication authentication) {
                throw new UnsupportedOperationException();
            }

            @Override
            protected Authentication nullSafeConvert(Object principal) {
                return expected;
            }
        }.convert(new Object());

        // Then
        assertThat(actual, is(expected));
    }

    @Test
    public void Cannot_convert_null_principal() {

        // When
        final Authentication actual = new AbstractNullSafeAuthenticationConverter<Object>() {
            @Override
            public Object convert(Authentication authentication) {
                throw new UnsupportedOperationException();
            }

            @Override
            protected Authentication nullSafeConvert(Object principal) {
                throw new UnsupportedOperationException();
            }
        }.convert((Object) null);

        // Then
        assertThat(actual, nullValue());
    }
}