]> source.dussan.org Git - sonarqube.git/blob
09a22653625c6082cf0f292b88f9fa8fb8fe7110
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.organization;
21
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;
30
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;
36
37 public class DefaultOrganizationProviderImplTest {
38   private static final OrganizationDto ORGANIZATION_DTO_1 = new OrganizationDto()
39     .setUuid("uuid1")
40     .setName("the name of 1")
41     .setKey("the key 1");
42   private static final long DATE_1 = 1_999_888L;
43
44   private System2 system2 = mock(System2.class);
45
46   @Rule
47   public DbTester dbTester = DbTester.create(system2).setDisableDefaultOrganization(true);
48   @Rule
49   public ExpectedException expectedException = ExpectedException.none();
50
51   private DbClient dbClient = dbTester.getDbClient();
52   private DbSession dbSession = dbTester.getSession();
53
54   private DefaultOrganizationProviderImpl underTest = new DefaultOrganizationProviderImpl(dbClient);
55
56   @Test
57   public void get_fails_with_ISE_if_default_organization_internal_property_does_not_exist() {
58     expectISENoDefaultOrganizationUuid();
59
60     underTest.get();
61   }
62
63   @Test
64   public void get_fails_with_ISE_if_default_organization_internal_property_is_empty() {
65     dbClient.internalPropertiesDao().saveAsEmpty(dbSession, DEFAULT_ORGANIZATION);
66     dbSession.commit();
67
68     expectISENoDefaultOrganizationUuid();
69
70     underTest.get();
71   }
72
73   private void expectISENoDefaultOrganizationUuid() {
74     expectedException.expect(IllegalStateException.class);
75     expectedException.expectMessage("No Default organization uuid configured");
76   }
77
78   @Test
79   public void get_fails_with_ISE_if_default_organization_does_not_exist() {
80     dbClient.internalPropertiesDao().save(dbSession, DEFAULT_ORGANIZATION, "bla");
81     dbSession.commit();
82
83     expectedException.expect(IllegalStateException.class);
84     expectedException.expectMessage("Default organization with uuid 'bla' does not exist");
85
86     underTest.get();
87   }
88
89   @Test
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());
93     dbSession.commit();
94
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);
101   }
102
103   @Test
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());
107     dbSession.commit();
108
109     assertThat(underTest.get()).isNotSameAs(underTest.get());
110   }
111
112   @Test
113   public void unload_does_not_fail_if_load_has_not_been_called() {
114     underTest.unload();
115   }
116
117   @Test
118   public void load_fails_with_ISE_when_called_twice_without_unload_in_between() {
119     underTest.load();
120
121     try {
122       underTest.load();
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");
126     } finally {
127       underTest.unload();
128     }
129   }
130
131   @Test
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());
135     dbSession.commit();
136
137     try {
138       underTest.load();
139
140       DefaultOrganization cachedForThread1 = underTest.get();
141       assertThat(cachedForThread1).isSameAs(underTest.get());
142
143       Thread otherThread = new Thread(() -> {
144         try {
145           underTest.load();
146
147           assertThat(underTest.get())
148               .isNotSameAs(cachedForThread1)
149               .isSameAs(underTest.get());
150         } finally {
151           underTest.unload();
152         }
153       });
154       otherThread.start();
155       otherThread.join();
156     } finally {
157       underTest.unload();
158     }
159   }
160
161   @Test
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());
165     dbSession.commit();
166
167     try {
168       underTest.load();
169       DefaultOrganization cached = underTest.get();
170       assertThat(cached).isSameAs(underTest.get());
171
172       underTest.unload();
173       assertThat(underTest.get()).isNotSameAs(underTest.get()).isNotSameAs(cached);
174     } finally {
175       // fail safe
176       underTest.unload();
177     }
178   }
179
180   private void insertOrganization(OrganizationDto dto, long createdAt) {
181     when(system2.now()).thenReturn(createdAt);
182     dbClient.organizationDao().insert(dbSession, dto, false);
183     dbSession.commit();
184   }
185 }