<commit_before>package org.zalando.nakadi.service;


import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.zalando.nakadi.domain.EventTypeAuthorization;
import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute;
import org.zalando.nakadi.exceptions.UnableProcessException;
import org.zalando.nakadi.exceptions.runtime.AccessDeniedException;
import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException;
import org.zalando.nakadi.plugin.api.PluginException;
import org.zalando.nakadi.plugin.api.authz.AuthorizationAttribute;
import org.zalando.nakadi.plugin.api.authz.AuthorizationService;
import org.zalando.nakadi.repository.EventTypeRepository;
import org.zalando.nakadi.utils.EventTypeTestBuilder;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AuthorizationValidatorTest {

    private final AuthorizationValidator validator;
    private final AuthorizationService authorizationService;
    private final AdminService adminService;

    private final AuthorizationAttribute attr1 = new EventTypeAuthorizationAttribute("type1", "value1");
    private final AuthorizationAttribute attr2 = new EventTypeAuthorizationAttribute("type2", "value2");
    private final AuthorizationAttribute attr3 = new EventTypeAuthorizationAttribute("type3", "value3");
    private final AuthorizationAttribute attr4 = new EventTypeAuthorizationAttribute("type4", "value4");

    public AuthorizationValidatorTest() {
        authorizationService = mock(AuthorizationService.class);
        adminService = mock(AdminService.class);

        validator = new AuthorizationValidator(authorizationService,
                mock(EventTypeRepository.class), adminService);
    }

    @Test
    public void whenInvalidAuthAttributesThenInvalidEventTypeException() throws Exception {

        final EventTypeAuthorization auth = new EventTypeAuthorization(
                ImmutableList.of(attr1), ImmutableList.of(attr2), ImmutableList.of(attr3, attr4));

        when(authorizationService.isAuthorizationAttributeValid(attr1)).thenReturn(false);
        when(authorizationService.isAuthorizationAttributeValid(attr2)).thenReturn(true);
        when(authorizationService.isAuthorizationAttributeValid(attr3)).thenReturn(true);
        when(authorizationService.isAuthorizationAttributeValid(attr4)).thenReturn(false);

        try {
            validator.validateAuthorization(auth);
            fail("Exception expected to be thrown");
        } catch (final UnableProcessException e) {
            assertThat(e.getMessage(), equalTo("authorization attribute type1:value1 is invalid, " +
                    "authorization attribute type4:value4 is invalid"));
        }
    }

    @Test
    public void whenDuplicatesThenInvalidEventTypeException() throws Exception {

        final EventTypeAuthorization auth = new EventTypeAuthorization(
                ImmutableList.of(attr1, attr3, attr2, attr1, attr1, attr3),
                ImmutableList.of(attr3, attr2, attr2),
                ImmutableList.of(attr3, attr4));

        when(authorizationService.isAuthorizationAttributeValid(any())).thenReturn(true);

        try {
            validator.validateAuthorization(auth);
            fail("Exception expected to be thrown");
        } catch (final UnableProcessException e) {
            assertThat(e.getMessage(), equalTo(
                    "authorization property 'admins' contains duplicated attribute(s): type1:value1, type3:value3; " +
                            "authorization property 'readers' contains duplicated attribute(s): type2:value2"));
        }
    }

    @Test(expected = ServiceTemporarilyUnavailableException.class)
    public void whenPluginExceptionInIsAuthorizationAttributeValidThenServiceUnavailableException() throws Exception {

        final EventTypeAuthorization auth = new EventTypeAuthorization(
                ImmutableList.of(attr1),
                ImmutableList.of(attr2),
                ImmutableList.of(attr3));

        when(authorizationService.isAuthorizationAttributeValid(any())).thenThrow(new PluginException("blah"));

        validator.validateAuthorization(auth);
    }

    @Test
    public void whenAuthorizationIsNullWhileUpdatingETThenOk() throws Exception {
        validator.authorizeEventTypeAdmin(EventTypeTestBuilder.builder().authorization(null).build());
    }

    @Test(expected = AccessDeniedException.class)
    public void whenNotAuthorizedThenForbiddenAccessException() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        validator.authorizeEventTypeAdmin(EventTypeTestBuilder.builder()
                .authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
    public void whenETAdminNotAuthorizedButAdminThenOk() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        when(adminService.isAdmin(any())).thenReturn(true);
        validator.authorizeEventTypeAdmin(EventTypeTestBuilder.builder().authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
    public void whenETWriteNotAuthorizedButAdminThenOk() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        when(adminService.isAdmin(any())).thenReturn(true);
        validator.authorizeEventTypeWrite(EventTypeTestBuilder.builder().authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
    public void whenStreamReadNotAuthorizedButAdminThenOk() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        when(adminService.isAdmin(any())).thenReturn(true);
        validator.authorizeStreamRead(EventTypeTestBuilder.builder().authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test<commit_msg>ARUHA-1079 checkstyle<commit_after>package org.zalando.nakadi.service;


import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.zalando.nakadi.domain.EventTypeAuthorization;
import org.zalando.nakadi.domain.EventTypeAuthorizationAttribute;
import org.zalando.nakadi.exceptions.UnableProcessException;
import org.zalando.nakadi.exceptions.runtime.AccessDeniedException;
import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException;
import org.zalando.nakadi.plugin.api.PluginException;
import org.zalando.nakadi.plugin.api.authz.AuthorizationAttribute;
import org.zalando.nakadi.plugin.api.authz.AuthorizationService;
import org.zalando.nakadi.repository.EventTypeRepository;
import org.zalando.nakadi.utils.EventTypeTestBuilder;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AuthorizationValidatorTest {

    private final AuthorizationValidator validator;
    private final AuthorizationService authorizationService;
    private final AdminService adminService;

    private final AuthorizationAttribute attr1 = new EventTypeAuthorizationAttribute("type1", "value1");
    private final AuthorizationAttribute attr2 = new EventTypeAuthorizationAttribute("type2", "value2");
    private final AuthorizationAttribute attr3 = new EventTypeAuthorizationAttribute("type3", "value3");
    private final AuthorizationAttribute attr4 = new EventTypeAuthorizationAttribute("type4", "value4");

    public AuthorizationValidatorTest() {
        authorizationService = mock(AuthorizationService.class);
        adminService = mock(AdminService.class);

        validator = new AuthorizationValidator(authorizationService,
                mock(EventTypeRepository.class), adminService);
    }

    @Test
    public void whenInvalidAuthAttributesThenInvalidEventTypeException() throws Exception {

        final EventTypeAuthorization auth = new EventTypeAuthorization(
                ImmutableList.of(attr1), ImmutableList.of(attr2), ImmutableList.of(attr3, attr4));

        when(authorizationService.isAuthorizationAttributeValid(attr1)).thenReturn(false);
        when(authorizationService.isAuthorizationAttributeValid(attr2)).thenReturn(true);
        when(authorizationService.isAuthorizationAttributeValid(attr3)).thenReturn(true);
        when(authorizationService.isAuthorizationAttributeValid(attr4)).thenReturn(false);

        try {
            validator.validateAuthorization(auth);
            fail("Exception expected to be thrown");
        } catch (final UnableProcessException e) {
            assertThat(e.getMessage(), equalTo("authorization attribute type1:value1 is invalid, " +
                    "authorization attribute type4:value4 is invalid"));
        }
    }

    @Test
    public void whenDuplicatesThenInvalidEventTypeException() throws Exception {

        final EventTypeAuthorization auth = new EventTypeAuthorization(
                ImmutableList.of(attr1, attr3, attr2, attr1, attr1, attr3),
                ImmutableList.of(attr3, attr2, attr2),
                ImmutableList.of(attr3, attr4));

        when(authorizationService.isAuthorizationAttributeValid(any())).thenReturn(true);

        try {
            validator.validateAuthorization(auth);
            fail("Exception expected to be thrown");
        } catch (final UnableProcessException e) {
            assertThat(e.getMessage(), equalTo(
                    "authorization property 'admins' contains duplicated attribute(s): type1:value1, type3:value3; " +
                            "authorization property 'readers' contains duplicated attribute(s): type2:value2"));
        }
    }

    @Test(expected = ServiceTemporarilyUnavailableException.class)
    public void whenPluginExceptionInIsAuthorizationAttributeValidThenServiceUnavailableException() throws Exception {

        final EventTypeAuthorization auth = new EventTypeAuthorization(
                ImmutableList.of(attr1),
                ImmutableList.of(attr2),
                ImmutableList.of(attr3));

        when(authorizationService.isAuthorizationAttributeValid(any())).thenThrow(new PluginException("blah"));

        validator.validateAuthorization(auth);
    }

    @Test
    public void whenAuthorizationIsNullWhileUpdatingETThenOk() throws Exception {
        validator.authorizeEventTypeAdmin(EventTypeTestBuilder.builder().authorization(null).build());
    }

    @Test(expected = AccessDeniedException.class)
    public void whenNotAuthorizedThenForbiddenAccessException() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        validator.authorizeEventTypeAdmin(EventTypeTestBuilder.builder()
                .authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
    public void whenETAdminNotAuthorizedButAdminThenOk() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        when(adminService.isAdmin(any())).thenReturn(true);
        validator.authorizeEventTypeAdmin(EventTypeTestBuilder.builder()
                .authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
    public void whenETWriteNotAuthorizedButAdminThenOk() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        when(adminService.isAdmin(any())).thenReturn(true);
        validator.authorizeEventTypeWrite(EventTypeTestBuilder.builder()
                .authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
    public void whenStreamReadNotAuthorizedButAdminThenOk() throws Exception {
        when(authorizationService.isAuthorized(any(), any())).thenReturn(false);
        when(adminService.isAdmin(any())).thenReturn(true);
        validator.authorizeStreamRead(EventTypeTestBuilder.builder()
                .authorization(new EventTypeAuthorization(null, null, null)).build());
    }

    @Test
{'fraction_non_alphanumeric': 0.09349825046789811, 'fraction_numerical': 0.007486369924322565, 'mean_word_length': 4.247651579846285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022253', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3132, 'n_words': 541}