|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import static java.util.Comparator.comparing; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | +import static org.mockito.MockitoAnnotations.initMocks; |
| 9 | + |
| 10 | +import static java.util.stream.Collectors.toList; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import javax.ws.rs.core.MultivaluedMap; |
| 16 | + |
| 17 | +import org.gitlab4j.api.models.User; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | +import org.mockito.ArgumentCaptor; |
| 22 | +import org.mockito.Captor; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.Mockito; |
| 25 | +import org.mockito.Spy; |
| 26 | + |
| 27 | +public class TestStreams implements Constants { |
| 28 | + |
| 29 | + @Mock private GitLabApi gitLabApi; |
| 30 | + @Mock private GitLabApiClient gitLabApiClient; |
| 31 | + @Spy private FakeResponse response; |
| 32 | + @Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor; |
| 33 | + |
| 34 | + static private List<User> sortedUsers; |
| 35 | + |
| 36 | + @BeforeClass |
| 37 | + public static void setupClass() throws Exception { |
| 38 | + // Get a list of users sorted by username |
| 39 | + sortedUsers = JsonUtils.unmarshalResourceList(User.class, "user-list.json"); |
| 40 | + sortedUsers.sort(comparing(User::getUsername)); |
| 41 | + } |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setup() throws Exception { |
| 45 | + initMocks(this); |
| 46 | + response.init(User.class, null, "user-list.json"); |
| 47 | + when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient); |
| 48 | + when(gitLabApiClient.validateSecretToken(any())).thenReturn(true); |
| 49 | + when(gitLabApiClient.get(attributeCaptor.capture(), Mockito.<Object>any())).thenReturn(response); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testStream() throws Exception { |
| 54 | + |
| 55 | + Stream<User> stream = new UserApi(gitLabApi).getUsersStream(); |
| 56 | + assertNotNull(stream); |
| 57 | + |
| 58 | + List<User> users = stream.sorted(comparing(User::getUsername)).collect(toList()); |
| 59 | + assertNotNull(users); |
| 60 | + |
| 61 | + assertEquals(users.size(), sortedUsers.size()); |
| 62 | + |
| 63 | + for (int i = 0; i < users.size(); i++) { |
| 64 | + assertEquals(users.get(i).getId(), sortedUsers.get(i).getId()); |
| 65 | + assertEquals(users.get(i).getUsername(), sortedUsers.get(i).getUsername()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testParallelStream() throws Exception { |
| 71 | + |
| 72 | + Stream<User> stream = new UserApi(gitLabApi).getUsersStream(); |
| 73 | + assertNotNull(stream); |
| 74 | + |
| 75 | + List<User> users = stream.parallel().sorted(comparing(User::getUsername)).collect(toList()); |
| 76 | + assertNotNull(users); |
| 77 | + |
| 78 | + assertEquals(users.size(), sortedUsers.size()); |
| 79 | + |
| 80 | + for (int i = 0; i < users.size(); i++) { |
| 81 | + assertEquals(users.get(i).getId(), sortedUsers.get(i).getId()); |
| 82 | + assertEquals(users.get(i).getUsername(), sortedUsers.get(i).getUsername()); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments