選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UserRepositoryTest.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.batch.repository.user;
  21. import org.junit.rules.ExpectedException;
  22. import org.junit.Rule;
  23. import org.mockito.Mockito;
  24. import org.sonar.batch.bootstrap.WSLoaderResult;
  25. import com.google.common.io.ByteSource;
  26. import org.sonar.batch.bootstrap.WSLoader;
  27. import org.junit.Test;
  28. import org.sonar.batch.protocol.input.BatchInput;
  29. import java.io.ByteArrayInputStream;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.util.Arrays;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.assertj.core.api.Assertions.tuple;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.when;
  38. public class UserRepositoryTest {
  39. @Rule
  40. public final ExpectedException exception = ExpectedException.none();
  41. @Test
  42. public void testLoad() throws IOException {
  43. WSLoader wsLoader = mock(WSLoader.class);
  44. UserRepository userRepo = new UserRepository(wsLoader);
  45. ByteArrayOutputStream out = new ByteArrayOutputStream();
  46. BatchInput.User.Builder builder = BatchInput.User.newBuilder();
  47. builder.setLogin("fmallet").setName("Freddy Mallet").build().writeDelimitedTo(out);
  48. builder.setLogin("sbrandhof").setName("Simon").build().writeDelimitedTo(out);
  49. ByteSource source = mock(ByteSource.class);
  50. when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(new WSLoaderResult<>(source, true));
  51. when(source.openStream()).thenReturn(new ByteArrayInputStream(out.toByteArray()));
  52. assertThat(userRepo.loadFromWs(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon"));
  53. }
  54. @Test
  55. public void testInputStreamError() throws IOException {
  56. WSLoader wsLoader = mock(WSLoader.class);
  57. UserRepository userRepo = new UserRepository(wsLoader);
  58. ByteSource source = mock(ByteSource.class);
  59. when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(new WSLoaderResult<>(source, true));
  60. InputStream errorInputStream = mock(InputStream.class);
  61. Mockito.doThrow(IOException.class).when(errorInputStream).read();
  62. when(source.openStream()).thenReturn(errorInputStream);
  63. exception.expect(IllegalStateException.class);
  64. exception.expectMessage("Unable to get user details from server");
  65. assertThat(userRepo.loadFromWs(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon"));
  66. }
  67. }