View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test case for {@link AwsIterator}.
48   * @since 0.1
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 }