1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.dynamo;
31
32 import com.jcabi.aspects.Tv;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.Iterator;
38 import java.util.Map;
39 import java.util.NoSuchElementException;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.junit.jupiter.api.Assertions;
43 import org.junit.jupiter.api.Test;
44 import org.mockito.Mockito;
45
46
47
48
49
50 @SuppressWarnings("unchecked")
51 final class AwsIteratorTest {
52
53 @Test
54 void iteratesValve() throws IOException {
55 final Credentials credentials = Mockito.mock(Credentials.class);
56 final String attr = "attribute-1";
57 final String value = "value-1";
58 final Dosage first = Mockito.mock(Dosage.class);
59 Mockito.doReturn(
60 Collections.singletonList(new Attributes().with(attr, value))
61 ).when(first).items();
62 Mockito.doReturn(true).when(first).hasNext();
63 final Dosage second = Mockito.mock(Dosage.class);
64 Mockito.doReturn(second).when(first).next();
65 Mockito.doReturn(
66 Collections.singletonList(new Attributes().with(attr, value))
67 ).when(second).items();
68 Mockito.doReturn(true).when(second).hasNext();
69 final Dosage last = Mockito.mock(Dosage.class);
70 Mockito.doReturn(last).when(second).next();
71 Mockito.doReturn(new ArrayList<Attributes>(0)).when(last).items();
72 final Valve valve = Mockito.mock(Valve.class);
73 Mockito.doReturn(first)
74 .doReturn(second)
75 .doReturn(last)
76 .when(valve)
77 .fetch(
78 Mockito.eq(credentials), Mockito.anyString(),
79 Mockito.any(Map.class), Mockito.any(Collection.class)
80 );
81 final String table = "table-1";
82 final Iterator<Item> iterator = new AwsIterator(
83 credentials,
84 new AwsFrame(
85 credentials,
86 new AwsTable(credentials, Mockito.mock(Region.class), table),
87 table
88 ),
89 table, new Conditions(),
90 new ArrayList<>(0), valve
91 );
92 for (int idx = 0; idx < Tv.TEN; ++idx) {
93 MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(true));
94 }
95 Mockito.verify(valve).fetch(
96 Mockito.eq(credentials), Mockito.anyString(),
97 Mockito.any(Map.class), Mockito.any(Collection.class)
98 );
99 MatcherAssert.assertThat(
100 iterator.next().get(attr).getS(),
101 Matchers.equalTo(value)
102 );
103 MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(true));
104 Mockito.verify(first).next();
105 MatcherAssert.assertThat(
106 iterator.next().get(attr).getS(),
107 Matchers.equalTo(value)
108 );
109 MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
110 Mockito.verify(second).next();
111 }
112
113 @Test
114 void throwsOnEmptyIterator() throws Exception {
115 final Credentials credentials = Mockito.mock(Credentials.class);
116 final Dosage dosage = Mockito.mock(Dosage.class);
117 Mockito.doReturn(Collections.emptyList()).when(dosage).items();
118 final Valve valve = Mockito.mock(Valve.class);
119 Mockito.doReturn(dosage)
120 .when(valve)
121 .fetch(
122 Mockito.eq(credentials), Mockito.anyString(),
123 Mockito.any(Map.class), Mockito.any(Collection.class)
124 );
125 final String table = "table-2";
126 final Iterator<Item> iterator = new AwsIterator(
127 credentials,
128 new AwsFrame(
129 credentials,
130 new AwsTable(credentials, Mockito.mock(Region.class), table),
131 table
132 ),
133 table, new Conditions(),
134 new ArrayList<>(0), valve
135 );
136 Assertions.assertThrows(
137 NoSuchElementException.class,
138 iterator::next
139 );
140 }
141
142 }