@Test public void testDefault() { WSLoader loader = underTest.provide( mode, cache, wsClient, new AnalysisProperties(Maps.<String, String>newHashMap())); assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY); }
@Test public void no_cache_by_default_in_issues_mode() { when(mode.isIssues()).thenReturn(true); WSLoader loader = underTest.provide( mode, cache, wsClient, new AnalysisProperties(Maps.<String, String>newHashMap())); assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY); }
@Test public void enable_cache_in_issues_mode() { when(mode.isIssues()).thenReturn(true); WSLoader loader = underTest.provide( mode, cache, wsClient, new AnalysisProperties( ImmutableMap.of(AnalysisWSLoaderProvider.SONAR_USE_WS_CACHE, "true"))); assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.CACHE_ONLY); }
@Test public void testLoadSingleUser() throws IOException { WSLoaderResult<ByteSource> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true); when(wsLoader.loadSource("/batch/users?logins=fmallet")).thenReturn(res); assertThat(userRepo.load("fmallet").getName()).isEqualTo("Freddy Mallet"); }
@Test public void testLoad() throws IOException { Map<String, String> userMap = ImmutableMap.of("fmallet", "Freddy Mallet", "sbrandhof", "Simon"); WSLoaderResult<ByteSource> res = new WSLoaderResult<>(createUsersMock(userMap), true); when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(res); assertThat(userRepo.load(Arrays.asList("fmallet", "sbrandhof"))) .extracting("login", "name") .containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon")); }
@Test public void testFromCache() throws IOException { WSLoaderResult<ByteSource> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true); when(wsLoader.loadSource(anyString())).thenReturn(res); MutableBoolean fromCache = new MutableBoolean(); userRepo.load("", fromCache); assertThat(fromCache.booleanValue()).isTrue(); fromCache.setValue(false); userRepo.load(ImmutableList.of("user"), fromCache); assertThat(fromCache.booleanValue()).isTrue(); }
@Test public void testInputStreamError() throws IOException { ByteSource source = mock(ByteSource.class); WSLoaderResult<ByteSource> res = new WSLoaderResult<>(source, true); when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(res); InputStream errorInputStream = mock(InputStream.class); Mockito.doThrow(IOException.class).when(errorInputStream).read(); when(source.openStream()).thenReturn(errorInputStream); exception.expect(IllegalStateException.class); exception.expectMessage("Unable to get user details from server"); userRepo.load(Arrays.asList("fmallet", "sbrandhof")); }