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.batch.repository;
22 import org.apache.commons.lang.mutable.MutableBoolean;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.batch.cache.WSLoader;
26 import org.sonar.batch.cache.WSLoaderResult;
27 import org.sonar.scanner.protocol.input.GlobalRepositories;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoMoreInteractions;
33 import static org.mockito.Mockito.when;
35 public class DefaultGlobalRepositoriesLoaderTest {
36 private static final String BATCH_GLOBAL_URL = "/batch/global";
37 private WSLoader wsLoader;
38 private WSLoaderResult<String> result;
39 private DefaultGlobalRepositoriesLoader globalRepositoryLoader;
43 wsLoader = mock(WSLoader.class);
44 result = new WSLoaderResult<>(new GlobalRepositories().toJson(), true);
45 when(wsLoader.loadString(BATCH_GLOBAL_URL)).thenReturn(result);
47 globalRepositoryLoader = new DefaultGlobalRepositoriesLoader(wsLoader);
52 MutableBoolean fromCache = new MutableBoolean();
53 globalRepositoryLoader.load(fromCache);
55 assertThat(fromCache.booleanValue()).isTrue();
56 verify(wsLoader).loadString(BATCH_GLOBAL_URL);
57 verifyNoMoreInteractions(wsLoader);
61 public void testFromServer() {
62 result = new WSLoaderResult<>(new GlobalRepositories().toJson(), false);
63 when(wsLoader.loadString(BATCH_GLOBAL_URL)).thenReturn(result);
64 MutableBoolean fromCache = new MutableBoolean();
65 globalRepositoryLoader.load(fromCache);
67 assertThat(fromCache.booleanValue()).isFalse();
68 verify(wsLoader).loadString(BATCH_GLOBAL_URL);
69 verifyNoMoreInteractions(wsLoader);
72 public void testWithoutArg() {
73 globalRepositoryLoader.load(null);
75 verify(wsLoader).loadString(BATCH_GLOBAL_URL);
76 verifyNoMoreInteractions(wsLoader);