22
33import org .dataloader .fixtures .TestKit ;
44import org .dataloader .fixtures .parameterized .DelegatingDataLoaderFactory ;
5- import org .jspecify .annotations .NonNull ;
5+ import org .jspecify .annotations .NullMarked ;
66import org .jspecify .annotations .Nullable ;
7- import org .junit .jupiter .api .Assertions ;
87import org .junit .jupiter .api .Test ;
98
109import java .util .List ;
10+ import java .util .Map ;
1111import java .util .concurrent .CompletableFuture ;
12+ import java .util .concurrent .atomic .AtomicInteger ;
13+ import java .util .stream .Collectors ;
1214
1315import static org .awaitility .Awaitility .await ;
1416import static org .hamcrest .CoreMatchers .equalTo ;
1517import static org .hamcrest .CoreMatchers .is ;
1618import static org .hamcrest .MatcherAssert .assertThat ;
17- import static org .junit .jupiter .api .Assertions .* ;
19+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
1820
1921/**
2022 * There are WAY more tests via the {@link DelegatingDataLoaderFactory}
@@ -32,14 +34,37 @@ void canUnwrapDataLoaders() {
3234 }
3335
3436 @ Test
37+ @ NullMarked
3538 void canCreateAClassOk () {
3639 DataLoader <String , String > rawLoader = TestKit .idLoader ();
3740 DelegatingDataLoader <String , String > delegatingDataLoader = new DelegatingDataLoader <>(rawLoader ) {
38- @ Override
39- public CompletableFuture <String > load (@ NonNull String key , @ Nullable Object keyContext ) {
40- CompletableFuture <String > cf = super .load (key , keyContext );
41+ private CompletableFuture <String > enhance (CompletableFuture <String > cf ) {
4142 return cf .thenApply (v -> "|" + v + "|" );
4243 }
44+
45+ private CompletableFuture <List <String >> enhanceList (CompletableFuture <List <String >> cf ) {
46+ return cf .thenApply (v -> v .stream ().map (s -> "|" + s + "|" ).collect (Collectors .toList ()));
47+ }
48+
49+ @ Override
50+ public CompletableFuture <String > load (String key , @ Nullable Object keyContext ) {
51+ return enhance (super .load (key , keyContext ));
52+ }
53+
54+ @ Override
55+ public CompletableFuture <String > load (String key ) {
56+ return enhance (super .load (key ));
57+ }
58+
59+ @ Override
60+ public CompletableFuture <List <String >> loadMany (List <String > keys ) {
61+ return enhanceList (super .loadMany (keys ));
62+ }
63+
64+ @ Override
65+ public CompletableFuture <List <String >> loadMany (List <String > keys , List <Object > keyContexts ) {
66+ return enhanceList (super .loadMany (keys , keyContexts ));
67+ }
4368 };
4469
4570 assertThat (delegatingDataLoader .getDelegate (), is (rawLoader ));
@@ -73,8 +98,69 @@ void can_delegate_simple_properties() {
7398 DelegatingDataLoader <String , String > delegate = new DelegatingDataLoader <>(rawLoader );
7499
75100 assertNotNull (delegate .getName ());
76- assertThat (delegate .getName (),equalTo ("name" ));
77- assertThat (delegate .getOptions (),equalTo (options ));
78- assertThat (delegate .getBatchLoadFunction (),equalTo (loadFunction ));
101+ assertThat (delegate .getName (), equalTo ("name" ));
102+ assertThat (delegate .getOptions (), equalTo (options ));
103+ assertThat (delegate .getBatchLoadFunction (), equalTo (loadFunction ));
104+ }
105+
106+ @ NullMarked
107+ @ Test
108+ void can_create_a_delegate_class_that_has_post_side_effects () {
109+ DataLoaderOptions options = DataLoaderOptions .newOptions ().build ();
110+ BatchLoader <String , String > loadFunction = CompletableFuture ::completedFuture ;
111+ DataLoader <String , String > rawLoader = DataLoaderFactory .newDataLoader ("name" , loadFunction , options );
112+
113+ AtomicInteger loadCalled = new AtomicInteger (0 );
114+ AtomicInteger loadManyCalled = new AtomicInteger (0 );
115+ AtomicInteger loadManyMapCalled = new AtomicInteger (0 );
116+ DelegatingDataLoader <String , String > delegate = new DelegatingDataLoader <>(rawLoader ) {
117+
118+ @ Override
119+ public CompletableFuture <String > load (String key ) {
120+ CompletableFuture <String > cf = super .load (key );
121+ loadCalled .incrementAndGet ();
122+ return cf ;
123+ }
124+
125+ @ Override
126+ public CompletableFuture <String > load (String key , @ Nullable Object keyContext ) {
127+ CompletableFuture <String > cf = super .load (key , keyContext );
128+ loadCalled .incrementAndGet ();
129+ return cf ;
130+ }
131+
132+ @ Override
133+ public CompletableFuture <List <String >> loadMany (List <String > keys , List <Object > keyContexts ) {
134+ CompletableFuture <List <String >> cf = super .loadMany (keys , keyContexts );
135+ loadManyCalled .incrementAndGet ();
136+ return cf ;
137+ }
138+
139+ @ Override
140+ public CompletableFuture <List <String >> loadMany (List <String > keys ) {
141+ CompletableFuture <List <String >> cf = super .loadMany (keys );
142+ loadManyCalled .incrementAndGet ();
143+ return cf ;
144+ }
145+
146+ @ Override
147+ public CompletableFuture <Map <String , String >> loadMany (Map <String , ?> keysAndContexts ) {
148+ CompletableFuture <Map <String , String >> cf = super .loadMany (keysAndContexts );
149+ loadManyMapCalled .incrementAndGet ();
150+ return cf ;
151+ }
152+ };
153+
154+
155+ delegate .load ("L1" );
156+ delegate .load ("L2" , null );
157+ delegate .loadMany (List .of ("LM1" , "LM2" ), List .of ());
158+ delegate .loadMany (List .of ("LM3" ));
159+ delegate .loadMany (Map .of ("LMM1" , "kc1" , "LMM2" , "kc2" ));
160+
161+ assertNotNull (delegate .getDelegate ());
162+ assertThat (loadCalled .get (), equalTo (2 ));
163+ assertThat (loadManyCalled .get (), equalTo (2 ));
164+ assertThat (loadManyMapCalled .get (), equalTo (1 ));
79165 }
80166}
0 commit comments