File tree Expand file tree Collapse file tree 2 files changed +34
-3
lines changed
main/java/com/dasburo/spring/cache/dynamo/rootattribute
test/java/com/dasburo/spring/cache/dynamo/rootattribute Expand file tree Collapse file tree 2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,9 @@ public class RootAttributeReader {
1919 public RootAttribute readRootAttribute (@ NonNull RootAttributeConfig rootAttributeConfig , @ NonNull Object object ) {
2020 try {
2121 Object value = PropertyUtils .getProperty (object , rootAttributeConfig .getName ());
22+ if (value == null ) {
23+ return null ;
24+ }
2225 AttributeValue attributeValue = mapValueToAttributeValue (value , rootAttributeConfig .getType ());
2326 return new RootAttribute (rootAttributeConfig .getName (), attributeValue );
2427 }
@@ -31,13 +34,13 @@ public RootAttribute readRootAttribute(@NonNull RootAttributeConfig rootAttribut
3134 @ Nullable
3235 private AttributeValue mapValueToAttributeValue (@ NonNull Object value , @ NonNull ScalarAttributeType type ) {
3336 switch (type ) {
34- case S :
35- return new AttributeValue ().withS (String .valueOf (value ));
3637 case N :
3738 return new AttributeValue ().withN (String .valueOf (value ));
3839 case B :
3940 return new AttributeValue ().withB ((ByteBuffer ) value );
41+ case S :
42+ default :
43+ return new AttributeValue ().withS (String .valueOf (value ));
4044 }
41- return null ;
4245 }
4346}
Original file line number Diff line number Diff line change 99import java .util .LinkedHashMap ;
1010
1111import static org .junit .Assert .assertEquals ;
12+ import static org .junit .Assert .assertNull ;
1213
1314public class RootAttributeReaderTest {
1415
@@ -138,4 +139,31 @@ public void testRootAttributeReader_LinkedHashMapCanBeHandled() {
138139 assertEquals ("stringField" , rootAttribute .getName ());
139140 assertEquals ("dummy-text" , rootAttribute .getAttributeValue ().getS ());
140141 }
142+
143+ @ Test
144+ public void testRootAttributeReader_UnknownFieldIsGetsIgnored () {
145+ //given
146+ RootAttributeConfig rootAttributeConfig = new RootAttributeConfig ("unknownField" , ScalarAttributeType .S );
147+ SampleTestClass sampleInstance = new SampleTestClass ();
148+
149+ //when
150+ RootAttribute rootAttribute = rootAttributeReader .readRootAttribute (rootAttributeConfig , sampleInstance );
151+
152+ //then
153+ assertNull (rootAttribute );
154+ }
155+
156+ @ Test
157+ public void testRootAttributeReader_NullValueOfKnownFieldsGetsIgnored () {
158+ //given
159+ RootAttributeConfig rootAttributeConfig = new RootAttributeConfig ("stringField" , ScalarAttributeType .S );
160+ SampleTestClass sampleInstance = new SampleTestClass ();
161+ sampleInstance .setStringField (null );
162+
163+ //when
164+ RootAttribute rootAttribute = rootAttributeReader .readRootAttribute (rootAttributeConfig , sampleInstance );
165+
166+ //then
167+ assertNull (rootAttribute );
168+ }
141169}
You can’t perform that action at this time.
0 commit comments