View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.dynamo.retry;
6   
7   import com.jcabi.dynamo.Attributes;
8   import com.jcabi.dynamo.Conditions;
9   import com.jcabi.dynamo.Credentials;
10  import com.jcabi.dynamo.Dosage;
11  import com.jcabi.dynamo.Frame;
12  import com.jcabi.dynamo.Valve;
13  import com.jcabi.dynamo.mock.H2Data;
14  import com.jcabi.dynamo.mock.MkRegion;
15  import java.io.IOException;
16  import java.util.Collection;
17  import java.util.Map;
18  import org.hamcrest.MatcherAssert;
19  import org.hamcrest.Matchers;
20  import org.junit.jupiter.api.Test;
21  import software.amazon.awssdk.services.dynamodb.model.Condition;
22  
23  /**
24   * Test case for {@link ReFrame}.
25   * @since 0.9
26   */
27  final class ReFrameTest {
28  
29      @Test
30      void delegatesWhereWithNameAndValue() throws Exception {
31          MatcherAssert.assertThat(
32              "does not wrap where result in ReFrame",
33              new ReFrame(ReFrameTest.origin()).where("k\u00e9y", "1"),
34              Matchers.instanceOf(ReFrame.class)
35          );
36      }
37  
38      @Test
39      void delegatesWhereWithCondition() throws Exception {
40          MatcherAssert.assertThat(
41              "does not wrap where-with-condition result in ReFrame",
42              new ReFrame(ReFrameTest.origin()).where(
43                  "k\u00e9y", Conditions.equalTo("1")
44              ),
45              Matchers.instanceOf(ReFrame.class)
46          );
47      }
48  
49      @Test
50      void delegatesTableAsReTable() throws Exception {
51          MatcherAssert.assertThat(
52              "does not wrap table in ReTable",
53              new ReFrame(ReFrameTest.origin()).table(),
54              Matchers.instanceOf(ReTable.class)
55          );
56      }
57  
58      @Test
59      void delegatesSizeToOrigin() throws Exception {
60          MatcherAssert.assertThat(
61              "does not delegate size to origin frame",
62              new ReFrame(ReFrameTest.origin()).size(),
63              Matchers.equalTo(1)
64          );
65      }
66  
67      @Test
68      void delegatesIteratorAsReIterator() throws Exception {
69          MatcherAssert.assertThat(
70              "does not wrap iterator in ReIterator",
71              new ReFrame(ReFrameTest.origin()).iterator(),
72              Matchers.instanceOf(ReIterator.class)
73          );
74      }
75  
76      @Test
77      void delegatesThroughReturningReFrame() throws Exception {
78          MatcherAssert.assertThat(
79              "does not return ReFrame from through",
80              new ReFrame(ReFrameTest.origin()).through(
81                  new Valve() {
82                      @Override
83                      public Dosage fetch(
84                          final Credentials credentials,
85                          final String table,
86                          final Map<String, Condition> conditions,
87                          final Collection<String> keys) {
88                          return new Dosage.Empty();
89                      }
90  
91                      @Override
92                      public int count(
93                          final Credentials credentials,
94                          final String table,
95                          final Map<String, Condition> conditions) {
96                          return 0;
97                      }
98                  }
99              ),
100             Matchers.instanceOf(ReFrame.class)
101         );
102     }
103 
104     @Test
105     void delegatesIsEmptyToOrigin() throws Exception {
106         MatcherAssert.assertThat(
107             "does not delegate isEmpty to origin",
108             new ReFrame(ReFrameTest.origin()).isEmpty(),
109             Matchers.is(false)
110         );
111     }
112 
113     /**
114      * Creates a frame backed by H2Data with one item.
115      * @return Frame with one item
116      * @throws IOException If fails
117      */
118     private static Frame origin() throws IOException {
119         final String table = "t\u00e9sts";
120         final String key = "k\u00e9y";
121         final String attr = "\u00e4ttr";
122         final H2Data data = new H2Data().with(
123             table, new String[]{key}, attr
124         );
125         data.put(
126             table,
127             new Attributes().with(key, "1").with(attr, "v\u00e4l")
128         );
129         return new MkRegion(data).table(table).frame();
130     }
131 }