3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.authentication.event;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
26 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
27 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
28 import static org.sonar.server.authentication.event.AuthenticationEvent.Provider;
29 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
31 public class AuthenticationEventSourceTest {
33 public ExpectedException expectedException = ExpectedException.none();
36 public void local_fails_with_NPE_if_method_is_null() {
37 expectedException.expect(NullPointerException.class);
38 expectedException.expectMessage("method can't be null");
44 public void local_creates_source_instance_with_specified_method_and_hardcoded_provider_and_provider_name() {
45 Source underTest = Source.local(Method.BASIC_TOKEN);
47 assertThat(underTest.getMethod()).isEqualTo(Method.BASIC_TOKEN);
48 assertThat(underTest.getProvider()).isEqualTo(Provider.LOCAL);
49 assertThat(underTest.getProviderName()).isEqualTo("local");
53 public void oauth2_fails_with_NPE_if_providerName_is_null() {
54 expectedException.expect(NullPointerException.class);
55 expectedException.expectMessage("provider name can't be null");
61 public void oauth2_creates_source_instance_with_specified_provider_name_and_hardcoded_provider_and_method() {
62 Source underTest = Source.oauth2("some name");
64 assertThat(underTest.getMethod()).isEqualTo(Method.OAUTH2);
65 assertThat(underTest.getProvider()).isEqualTo(Provider.EXTERNAL);
66 assertThat(underTest.getProviderName()).isEqualTo("some name");
70 public void oauth2_fails_with_IAE_if_providerName_is_empty() {
71 expectedException.expect(IllegalArgumentException.class);
72 expectedException.expectMessage("provider name can't be empty");
78 public void realm_fails_with_NPE_if_method_is_null() {
79 expectedException.expect(NullPointerException.class);
80 expectedException.expectMessage("method can't be null");
82 Source.realm(null, "name");
86 public void realm_fails_with_NPE_if_providerName_is_null() {
87 expectedException.expect(NullPointerException.class);
88 expectedException.expectMessage("provider name can't be null");
90 Source.realm(Method.BASIC, null);
94 public void realm_fails_with_IAE_if_providerName_is_empty() {
95 expectedException.expect(IllegalArgumentException.class);
96 expectedException.expectMessage("provider name can't be empty");
98 Source.realm(Method.BASIC, "");
102 public void realm_creates_source_instance_with_specified_method_and_provider_name_and_hardcoded_provider() {
103 Source underTest = Source.realm(Method.BASIC, "some name");
105 assertThat(underTest.getMethod()).isEqualTo(Method.BASIC);
106 assertThat(underTest.getProvider()).isEqualTo(Provider.REALM);
107 assertThat(underTest.getProviderName()).isEqualTo("some name");
111 public void sso_returns_source_instance_with_hardcoded_method_provider_and_providerName() {
112 Source underTest = Source.sso();
114 assertThat(underTest.getMethod()).isEqualTo(Method.SSO);
115 assertThat(underTest.getProvider()).isEqualTo(Provider.SSO);
116 assertThat(underTest.getProviderName()).isEqualTo("sso");
118 assertThat(underTest).isSameAs(Source.sso());