Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.1 kB
3
Indexable
Never
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

public class YourClassTest {

    @InjectMocks
    private YourClass yourClass;

    @Mock
    private List<AbrangenciaProvider> providers;

    @Mock
    private CacheUsuarioAbrangencia cacheUsuarioAbrangencia;

    @Mock
    private AuthorizationAdapter authorizationAdapter;

    @Mock
    private TenantSupplier tenantSupplier;

    @Mock
    private UserSupplier userSupplier;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    @Transactional
    public void testLoadAbrangencia() {
        when(yourClass.initCache(anyString(), anyString())).thenReturn(true);

        yourClass.loadAbrangencia("testDomain", "testService");

        assertTrue(yourClass.securedResource.get());
    }

    @Test
    @Transactional
    public void testInitCacheWithFilter() {
        when(providers.iterator()).thenReturn(Collections.singleton(mock(AbrangenciaProvider.class)).iterator());

        when(tenantSupplier.get()).thenReturn("testTenant");
        when(userSupplier.get()).thenReturn("testUser");

        AbrangenciaKey key = new AbrangenciaKey();
        key.setTenant("testTenant");
        key.setUserName("testUser");
        key.setProviderName("testProvider");

        when(cacheUsuarioAbrangencia.containsKey(key)).thenReturn(false);

        when(authorizationAdapter.getFilters(anyString(), anyString(), anyString())).thenReturn(someMockFilter());

        yourClass.initCache("testDomain", "testService");

        verify(cacheUsuarioAbrangencia, times(1)).put(eq(key), any());
        assertTrue(yourClass.securedResource);
    }

    @Test
    @Transactional
    public void testInitCacheWithoutFilter() {
        when(providers.iterator()).thenReturn(Collections.singleton(mock(AbrangenciaProvider.class)).iterator());

        when(tenantSupplier.get()).thenReturn("testTenant");
        when(userSupplier.get()).thenReturn("testUser");

        AbrangenciaKey key = new AbrangenciaKey();
        key.setTenant("testTenant");
        key.setUserName("testUser");
        key.setProviderName("testProvider");

        when(cacheUsuarioAbrangencia.containsKey(key)).thenReturn(false);

        when(authorizationAdapter.getFilters(anyString(), anyString(), anyString())).thenReturn(null);

        yourClass.initCache("testDomain", "testService");

        verify(cacheUsuarioAbrangencia, never()).put(eq(key), any());
        assertFalse(yourClass.securedResource);
    }

    private Filter someMockFilter() {
        // Implement or mock your Filter object as needed for testing
        return mock(Filter.class);
    }
}
Leave a Comment