forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GemfireItemWriterTests.java
145 lines (116 loc) · 3.51 KB
/
GemfireItemWriterTests.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.data;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.SpELItemKeyMapper;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.core.convert.converter.Converter;
@ExtendWith(MockitoExtension.class)
class GemfireItemWriterTests {
private GemfireItemWriter<String, Foo> writer;
@Mock
private GemfireTemplate template;
@BeforeEach
void setUp() throws Exception {
writer = new GemfireItemWriter<>();
writer.setTemplate(template);
writer.setItemKeyMapper(new SpELItemKeyMapper<>("bar.val"));
writer.afterPropertiesSet();
}
@Test
void testAfterPropertiesSet() throws Exception {
writer = new GemfireItemWriter<>();
assertThrows(IllegalArgumentException.class, writer::afterPropertiesSet);
writer.setTemplate(template);
assertThrows(IllegalArgumentException.class, writer::afterPropertiesSet);
writer.setItemKeyMapper(new SpELItemKeyMapper<>("foo"));
writer.afterPropertiesSet();
}
@Test
void testBasicWrite() throws Exception {
List<Foo> items = new ArrayList<Foo>() {
{
add(new Foo(new Bar("val1")));
add(new Foo(new Bar("val2")));
}
};
writer.write(items);
verify(template).put("val1", items.get(0));
verify(template).put("val2", items.get(1));
}
@Test
void testBasicDelete() throws Exception {
List<Foo> items = new ArrayList<Foo>() {
{
add(new Foo(new Bar("val1")));
add(new Foo(new Bar("val2")));
}
};
writer.setDelete(true);
writer.write(items);
verify(template).remove("val1");
verify(template).remove("val2");
}
@Test
void testWriteWithCustomItemKeyMapper() throws Exception {
List<Foo> items = new ArrayList<Foo>() {
{
add(new Foo(new Bar("val1")));
add(new Foo(new Bar("val2")));
}
};
writer = new GemfireItemWriter<>();
writer.setTemplate(template);
writer.setItemKeyMapper(new Converter<Foo, String>() {
@Override
public String convert(Foo item) {
String index = item.bar.val.replaceAll("val", "");
return "item" + index;
}
});
writer.afterPropertiesSet();
writer.write(items);
verify(template).put("item1", items.get(0));
verify(template).put("item2", items.get(1));
}
@Test
void testWriteNoTransactionNoItems() throws Exception {
writer.write(new Chunk<>());
verifyNoInteractions(template);
}
static class Foo {
public Bar bar;
public Foo(Bar bar) {
this.bar = bar;
}
}
static class Bar {
public String val;
public Bar(String b1) {
this.val = b1;
}
}
}