3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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.organization;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.organization.OrganizationDto;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.fail;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35 import static org.sonar.server.property.InternalProperties.DEFAULT_ORGANIZATION;
37 public class DefaultOrganizationProviderImplTest {
38 private static final OrganizationDto ORGANIZATION_DTO_1 = new OrganizationDto()
40 .setName("the name of 1")
42 private static final long DATE_1 = 1_999_888L;
44 private System2 system2 = mock(System2.class);
47 public DbTester dbTester = DbTester.create(system2).setDisableDefaultOrganization(true);
49 public ExpectedException expectedException = ExpectedException.none();
51 private DbClient dbClient = dbTester.getDbClient();
52 private DbSession dbSession = dbTester.getSession();
54 private DefaultOrganizationProviderImpl underTest = new DefaultOrganizationProviderImpl(dbClient);
57 public void get_fails_with_ISE_if_default_organization_internal_property_does_not_exist() {
58 expectISENoDefaultOrganizationUuid();
64 public void get_fails_with_ISE_if_default_organization_internal_property_is_empty() {
65 dbClient.internalPropertiesDao().saveAsEmpty(dbSession, DEFAULT_ORGANIZATION);
68 expectISENoDefaultOrganizationUuid();
73 private void expectISENoDefaultOrganizationUuid() {
74 expectedException.expect(IllegalStateException.class);
75 expectedException.expectMessage("No Default organization uuid configured");
79 public void get_fails_with_ISE_if_default_organization_does_not_exist() {
80 dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, "bla");
83 expectedException.expect(IllegalStateException.class);
84 expectedException.expectMessage("Default organization with uuid 'bla' does not exist");
90 public void get_returns_DefaultOrganization_populated_from_DB() {
91 insertOrganization(ORGANIZATION_DTO_1, DATE_1);
92 dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid());
95 DefaultOrganization defaultOrganization = underTest.get();
96 assertThat(defaultOrganization.getUuid()).isEqualTo(ORGANIZATION_DTO_1.getUuid());
97 assertThat(defaultOrganization.getKey()).isEqualTo(ORGANIZATION_DTO_1.getKey());
98 assertThat(defaultOrganization.getName()).isEqualTo(ORGANIZATION_DTO_1.getName());
99 assertThat(defaultOrganization.getCreatedAt()).isEqualTo(DATE_1);
100 assertThat(defaultOrganization.getUpdatedAt()).isEqualTo(DATE_1);
104 public void get_returns_new_DefaultOrganization_with_each_call_when_cache_is_not_loaded() {
105 insertOrganization(ORGANIZATION_DTO_1, DATE_1);
106 dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid());
109 assertThat(underTest.get()).isNotSameAs(underTest.get());
113 public void unload_does_not_fail_if_load_has_not_been_called() {
118 public void load_fails_with_ISE_when_called_twice_without_unload_in_between() {
123 fail("A IllegalStateException should have been raised");
124 } catch (IllegalStateException e) {
125 assertThat(e).hasMessage("load called twice for thread '" + Thread.currentThread().getName() + "' or state wasn't cleared last time it was used");
132 public void load_and_unload_cache_DefaultOrganization_object_by_thread() throws InterruptedException {
133 insertOrganization(ORGANIZATION_DTO_1, DATE_1);
134 dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid());
140 DefaultOrganization cachedForThread1 = underTest.get();
141 assertThat(cachedForThread1).isSameAs(underTest.get());
143 Thread otherThread = new Thread(() -> {
147 assertThat(underTest.get())
148 .isNotSameAs(cachedForThread1)
149 .isSameAs(underTest.get());
162 public void get_returns_new_instance_for_each_call_once_unload_has_been_called() {
163 insertOrganization(ORGANIZATION_DTO_1, DATE_1);
164 dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, ORGANIZATION_DTO_1.getUuid());
169 DefaultOrganization cached = underTest.get();
170 assertThat(cached).isSameAs(underTest.get());
173 assertThat(underTest.get()).isNotSameAs(underTest.get()).isNotSameAs(cached);
180 private void insertOrganization(OrganizationDto dto, long createdAt) {
181 when(system2.now()).thenReturn(createdAt);
182 dbClient.organizationDao().insert(dbSession, dto, false);