22
33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .assertj .core .api .Assertions .assertThatThrownBy ;
5+ import static org .folio .ld .dictionary .PredicateDictionary .TITLE ;
56
6- import org .folio .ld .dictionary .PredicateDictionary ;
77import org .folio .ld .dictionary .ResourceTypeDictionary ;
88import org .folio .linked .data .model .entity .Resource ;
99import org .folio .linked .data .model .entity .ResourceEdge ;
1010import org .folio .spring .testing .type .UnitTest ;
1111import org .junit .jupiter .api .Test ;
12+ import org .junit .jupiter .params .ParameterizedTest ;
13+ import org .junit .jupiter .params .provider .CsvSource ;
1214
1315@ UnitTest
1416class ResourceModelMapperTest {
1517
1618 private final ResourceModelMapper mapper = new ResourceModelMapperImpl ();
1719
18- @ Test
19- void toModel_depthZero_shouldNotMapOutgoingEdges () {
20+ @ ParameterizedTest
21+ @ CsvSource ({
22+ "0, 0, 0" ,
23+ "1, 1, 0" ,
24+ "2, 1, 1"
25+ })
26+ void toModel_shouldMapEdgesUpToRequestedDepth (int depth , int expectedLevel1Size , int expectedLevel2Size ) {
2027 // given
21- var root = buildThreeNodeGraph ();
28+ var root = resource (1L );
29+ var child = resource (2L );
30+ var grandChild = resource (3L );
31+ var parent = resource (4L );
32+ var grandParent = resource (5L );
33+ var rootToChild = new ResourceEdge (root , child , TITLE );
34+ root .addOutgoingEdge (rootToChild );
35+ child .addIncomingEdge (rootToChild );
36+ var childToGrandChild = new ResourceEdge (child , grandChild , TITLE );
37+ child .addOutgoingEdge (childToGrandChild );
38+ grandChild .addIncomingEdge (childToGrandChild );
39+ var parentToRoot = new ResourceEdge (parent , root , TITLE );
40+ parent .addOutgoingEdge (parentToRoot );
41+ root .addIncomingEdge (parentToRoot );
42+ var grandParentToParent = new ResourceEdge (grandParent , parent , TITLE );
43+ grandParent .addOutgoingEdge (grandParentToParent );
44+ parent .addIncomingEdge (grandParentToParent );
2245
2346 // when
24- var model = mapper .toModel (root , 0 );
47+ var model = mapper .toModel (root , depth );
2548
2649 // then
27- assertThat (model .getOutgoingEdges ()).isEmpty ();
50+ assertThat (model .getOutgoingEdges ()).hasSize (expectedLevel1Size );
51+ assertThat (model .getIncomingEdges ()).hasSize (expectedLevel1Size );
52+ if (expectedLevel1Size > 0 ) {
53+ assertThat (model .getOutgoingEdges ().iterator ().next ().getTarget ().getOutgoingEdges ()).hasSize (expectedLevel2Size );
54+ assertThat (model .getIncomingEdges ().iterator ().next ().getSource ().getIncomingEdges ()).hasSize (expectedLevel2Size );
55+ }
2856 }
2957
3058 @ Test
31- void toModel_depthOne_shouldMapOnlyFirstLevelOutgoingEdges () {
59+ void toModel_depthOne_outgoingAndIncomingDepthsAreIndependent () {
3260 // given
33- var root = buildThreeNodeGraph ();
61+ var root = resource (1L );
62+ var child = resource (2L );
63+ var grandChild = resource (3L );
64+ var parent = resource (4L );
65+ var grandParent = resource (5L );
66+ var rootToChild = new ResourceEdge (root , child , TITLE );
67+ root .addOutgoingEdge (rootToChild );
68+ child .addIncomingEdge (rootToChild );
69+ var childToGrandChild = new ResourceEdge (child , grandChild , TITLE );
70+ child .addOutgoingEdge (childToGrandChild );
71+ grandChild .addIncomingEdge (childToGrandChild );
72+ var parentToRoot = new ResourceEdge (parent , root , TITLE );
73+ parent .addOutgoingEdge (parentToRoot );
74+ root .addIncomingEdge (parentToRoot );
75+ var grandParentToParent = new ResourceEdge (grandParent , parent , TITLE );
76+ grandParent .addOutgoingEdge (grandParentToParent );
77+ parent .addIncomingEdge (grandParentToParent );
3478
3579 // when
3680 var model = mapper .toModel (root , 1 );
3781
3882 // then
3983 assertThat (model .getOutgoingEdges ()).hasSize (1 );
40- var child = model .getOutgoingEdges ().iterator ().next ().getTarget ();
41- assertThat (child .getOutgoingEdges ()).isEmpty ();
42- }
84+ assertThat (model .getIncomingEdges ()).hasSize (1 );
4385
44- @ Test
45- void toModel_depthTwo_shouldMapTwoLevelsOfOutgoingEdges () {
46- // given
47- var root = buildThreeNodeGraph ();
48-
49- // when
50- var model = mapper .toModel (root , 2 );
86+ var mappedChild = model .getOutgoingEdges ().iterator ().next ().getTarget ();
87+ assertThat (mappedChild .getOutgoingEdges ()).isEmpty ();
88+ assertThat (mappedChild .getIncomingEdges ()).hasSize (1 );
5189
52- // then
53- assertThat (model .getOutgoingEdges ()).hasSize (1 );
54- var child = model .getOutgoingEdges ().iterator ().next ().getTarget ();
55- assertThat (child .getOutgoingEdges ()).hasSize (1 );
56- var grandChild = child .getOutgoingEdges ().iterator ().next ().getTarget ();
57- assertThat (grandChild .getOutgoingEdges ()).isEmpty ();
90+ var mappedParent = model .getIncomingEdges ().iterator ().next ().getSource ();
91+ assertThat (mappedParent .getIncomingEdges ()).isEmpty ();
92+ assertThat (mappedParent .getOutgoingEdges ()).hasSize (1 );
5893 }
5994
6095 @ Test
6196 void toModel_depthGreaterThanMax_shouldThrow () {
6297 // given
63- var root = buildThreeNodeGraph ();
98+ var root = buildThreeNodeOutgoingGraph ();
6499
65100 // when / then
66101 assertThatThrownBy (() -> mapper .toModel (root , 8 ))
67102 .isInstanceOf (IllegalArgumentException .class )
68- .hasMessage ("Requested outgoing edges depth is too high: 8. Maximum allowed is 7" );
103+ .hasMessage ("Requested edges depth is too high: 8. Maximum allowed is 7" );
69104 }
70105
71- private static Resource buildThreeNodeGraph () {
106+ private static Resource buildThreeNodeOutgoingGraph () {
72107 var root = resource (1L );
73108 var child = resource (2L );
74109 var grandChild = resource (3L );
75110
76- root .addOutgoingEdge (new ResourceEdge (root , child , PredicateDictionary . TITLE ));
77- child .addOutgoingEdge (new ResourceEdge (child , grandChild , PredicateDictionary . TITLE ));
111+ root .addOutgoingEdge (new ResourceEdge (root , child , TITLE ));
112+ child .addOutgoingEdge (new ResourceEdge (child , grandChild , TITLE ));
78113
79114 return root ;
80115 }
@@ -85,3 +120,4 @@ private static Resource resource(Long id) {
85120 .addTypes (ResourceTypeDictionary .INSTANCE );
86121 }
87122}
123+
0 commit comments