22
22
import static com .redhat .lightblue .util .test .AbstractJsonNodeTest .loadResource ;
23
23
import static org .junit .Assert .assertArrayEquals ;
24
24
import static org .junit .Assert .assertEquals ;
25
+ import static org .junit .Assert .assertNotNull ;
26
+ import static org .junit .Assert .assertNull ;
25
27
26
28
import java .util .Arrays ;
27
29
import java .util .Collection ;
35
37
import org .junit .runners .Parameterized .Parameters ;
36
38
37
39
import com .redhat .lightblue .common .ldap .LdapConstant ;
40
+ import com .redhat .lightblue .common .ldap .LightblueUtil ;
38
41
import com .redhat .lightblue .metadata .EntityMetadata ;
39
42
import com .redhat .lightblue .metadata .types .DateType ;
40
43
import com .redhat .lightblue .test .MetadataUtil ;
41
44
import com .redhat .lightblue .util .JsonDoc ;
42
45
import com .unboundid .ldap .sdk .Entry ;
43
46
import com .unboundid .util .StaticUtils ;
44
47
45
- @ RunWith (value = Parameterized .class )
46
48
public class EntryBuilderTest {
47
49
48
- private final static Date now = new Date ();
49
-
50
- @ Parameters (name = "{index}: {0}" )
51
- public static Collection <Object []> data () {
52
- return Arrays .asList (new Object [][] {
53
- {"{\" type\" : \" string\" }" , quote ("teststring" ), "teststring" },
54
- {"{\" type\" : \" integer\" }" , "4" , null },
55
- {"{\" type\" : \" boolean\" }" , "true" , null },
56
- {"{\" type\" : \" date\" }" , quote (DateType .getDateFormat ().format (now )), StaticUtils .encodeGeneralizedTime (now )},
57
- {"{\" type\" : \" binary\" }" , quote (DatatypeConverter .printBase64Binary ("test binary data" .getBytes ())), "test binary data" },
58
- {"{\" type\" : \" array\" , \" items\" : {\" type\" : \" string\" }}" , "[\" hello\" ,\" world\" ]" , new String []{"hello" , "world" }},
59
- {"{\" type\" : \" array\" , \" items\" : {\" type\" : \" binary\" }}" ,
60
- "[" + quote (DatatypeConverter .printBase64Binary ("hello" .getBytes ())) + ","
61
- + quote (DatatypeConverter .printBase64Binary ("world" .getBytes ())) + "]" ,
62
- new String []{"hello" , "world" }
63
- },
64
- });
65
- }
66
-
67
- private static String quote (String text ){
68
- return '"' + text + '"' ;
69
- }
70
-
71
- private final String fieldName = "testfield" ;
72
- private final String metadataType ;
73
- private final String crudValue ;
74
- private final Object expectedValue ;
75
-
76
- public EntryBuilderTest (String metadataType , String crudValue , Object expectedValue ){
77
- this .metadataType = metadataType ;
78
- this .crudValue = crudValue ;
79
- this .expectedValue = (expectedValue == null ) ? crudValue : expectedValue ;
80
- }
81
-
82
- @ Test
83
- public void test () throws Exception {
50
+ protected static Entry buildEntry (String fieldName , String metadataType , String crudValue ) throws Exception {
84
51
String metadata = loadResource ("./metadata/entryBuilderTest-metadata-template.json" )
85
52
.replaceFirst ("#fieldname" , fieldName )
86
53
.replaceFirst ("#type" , metadataType );
@@ -91,15 +58,104 @@ public void test() throws Exception{
91
58
EntityMetadata md = MetadataUtil .createEntityMetadata (LdapConstant .BACKEND , json (metadata ), null , null );
92
59
EntryBuilder builder = new EntryBuilder (md );
93
60
94
- Entry entry = builder .build ("uid=someuid,dc=example,dc=com" ,
61
+ return builder .build ("uid=someuid,dc=example,dc=com" ,
95
62
new JsonDoc (json (crud ).get ("data" )));
63
+ }
64
+
65
+ protected static String quote (String text ){
66
+ return '"' + text + '"' ;
67
+ }
68
+
69
+ public static class SpecializedTests {
70
+
71
+ @ Test
72
+ public void testFieldIsObjectType () throws Exception {
73
+ Entry entry = buildEntry (LightblueUtil .FIELD_OBJECT_TYPE , "{\" type\" : \" string\" }" , quote ("someEntity" ));
74
+
75
+ assertNotNull (entry );
76
+ assertNull (entry .getAttribute (LightblueUtil .FIELD_OBJECT_TYPE ));
77
+ }
96
78
97
- if (expectedValue .getClass ().isArray ()){
98
- assertArrayEquals ((String []) expectedValue , entry .getAttributeValues ("testfield" ));
79
+ /**
80
+ * This test is kind of hacky as it requires json injection in order to make it work because
81
+ * it requires two fields.
82
+ */
83
+ @ Test
84
+ public void testFieldIsArrayField () throws Exception {
85
+ String arrayFieldName = "someArray" ;
86
+ String arrayCountFieldName = LightblueUtil .createArrayCountFieldName (arrayFieldName );
87
+ Entry entry = buildEntry (
88
+ arrayCountFieldName ,
89
+ "{\" type\" : \" integer\" }, " + quote (arrayFieldName ) + ": {\" type\" : \" array\" , \" items\" : {\" type\" : \" string\" }}" ,
90
+ "2" );
91
+
92
+ assertNotNull (entry );
93
+ assertNull (entry .getAttribute (arrayFieldName ));
99
94
}
100
- else {
101
- assertEquals (expectedValue , entry .getAttributeValue ("testfield" ));
95
+
96
+ @ Test
97
+ public void testFieldIsArrayFieldWithoutMatchArray () throws Exception {
98
+ String arrayCountFieldName = LightblueUtil .createArrayCountFieldName ("someArray" );
99
+ Entry entry = buildEntry (arrayCountFieldName , "{\" type\" : \" integer\" }" , "2" );
100
+
101
+ assertNotNull (entry );
102
+ assertNotNull (entry .getAttribute (arrayCountFieldName ));
103
+ }
104
+
105
+ @ Test (expected = IllegalArgumentException .class )
106
+ public void testFieldIsDN () throws Exception {
107
+ buildEntry (LdapConstant .FIELD_DN , "{\" type\" : \" string\" }" , quote ("uid=someuid,dc=example,dc=com" ));
108
+ }
109
+
110
+ }
111
+
112
+ @ RunWith (value = Parameterized .class )
113
+ public static class ParameterizedTests {
114
+
115
+ private final static Date now = new Date ();
116
+
117
+ @ Parameters (name = "{index}: {0}" )
118
+ public static Collection <Object []> data () {
119
+ return Arrays .asList (new Object [][] {
120
+ {"{\" type\" : \" string\" }" , quote ("teststring" ), "teststring" },
121
+ {"{\" type\" : \" integer\" }" , "4" , null },
122
+ {"{\" type\" : \" boolean\" }" , "true" , null },
123
+ {"{\" type\" : \" date\" }" , quote (DateType .getDateFormat ().format (now )), StaticUtils .encodeGeneralizedTime (now )},
124
+ {"{\" type\" : \" binary\" }" , quote (DatatypeConverter .printBase64Binary ("test binary data" .getBytes ())), "test binary data" },
125
+ {"{\" type\" : \" array\" , \" items\" : {\" type\" : \" string\" }}" , "[\" hello\" ,\" world\" ]" , new String []{"hello" , "world" }},
126
+ {"{\" type\" : \" array\" , \" items\" : {\" type\" : \" binary\" }}" ,
127
+ "[" + quote (DatatypeConverter .printBase64Binary ("hello" .getBytes ())) + ","
128
+ + quote (DatatypeConverter .printBase64Binary ("world" .getBytes ())) + "]" ,
129
+ new String []{"hello" , "world" }
130
+ },
131
+ });
102
132
}
133
+
134
+ private final String fieldName = "testfield" ;
135
+ private final String metadataType ;
136
+ private final String crudValue ;
137
+ private final Object expectedValue ;
138
+
139
+ public ParameterizedTests (String metadataType , String crudValue , Object expectedValue ){
140
+ this .metadataType = metadataType ;
141
+ this .crudValue = crudValue ;
142
+ this .expectedValue = (expectedValue == null ) ? crudValue : expectedValue ;
143
+ }
144
+
145
+ @ Test
146
+ public void test () throws Exception {
147
+ Entry entry = buildEntry (fieldName , metadataType , crudValue );
148
+
149
+ assertNotNull (entry );
150
+
151
+ if (expectedValue .getClass ().isArray ()){
152
+ assertArrayEquals ((String []) expectedValue , entry .getAttributeValues ("testfield" ));
153
+ }
154
+ else {
155
+ assertEquals (expectedValue , entry .getAttributeValue ("testfield" ));
156
+ }
157
+ }
158
+
103
159
}
104
160
105
161
}
0 commit comments