Skip to content

Commit e1efccf

Browse files
committed
add unit tests for EntryBuilder
1 parent 32e1a0e commit e1efccf

File tree

1 file changed

+98
-42
lines changed

1 file changed

+98
-42
lines changed

lightblue-ldap-crud/src/test/java/com/redhat/lightblue/crud/ldap/EntryBuilderTest.java

Lines changed: 98 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadResource;
2323
import static org.junit.Assert.assertArrayEquals;
2424
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertNotNull;
26+
import static org.junit.Assert.assertNull;
2527

2628
import java.util.Arrays;
2729
import java.util.Collection;
@@ -35,52 +37,17 @@
3537
import org.junit.runners.Parameterized.Parameters;
3638

3739
import com.redhat.lightblue.common.ldap.LdapConstant;
40+
import com.redhat.lightblue.common.ldap.LightblueUtil;
3841
import com.redhat.lightblue.metadata.EntityMetadata;
3942
import com.redhat.lightblue.metadata.types.DateType;
4043
import com.redhat.lightblue.test.MetadataUtil;
4144
import com.redhat.lightblue.util.JsonDoc;
4245
import com.unboundid.ldap.sdk.Entry;
4346
import com.unboundid.util.StaticUtils;
4447

45-
@RunWith(value = Parameterized.class)
4648
public class EntryBuilderTest {
4749

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{
8451
String metadata = loadResource("./metadata/entryBuilderTest-metadata-template.json")
8552
.replaceFirst("#fieldname", fieldName)
8653
.replaceFirst("#type", metadataType);
@@ -91,15 +58,104 @@ public void test() throws Exception{
9158
EntityMetadata md = MetadataUtil.createEntityMetadata(LdapConstant.BACKEND, json(metadata), null, null);
9259
EntryBuilder builder = new EntryBuilder(md);
9360

94-
Entry entry = builder.build("uid=someuid,dc=example,dc=com",
61+
return builder.build("uid=someuid,dc=example,dc=com",
9562
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+
}
9678

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));
9994
}
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+
});
102132
}
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+
103159
}
104160

105161
}

0 commit comments

Comments
 (0)