Skip to content

Commit 32e1a0e

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

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2015 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue.crud.ldap;
20+
21+
import static com.redhat.lightblue.util.JsonUtils.json;
22+
import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadResource;
23+
import static org.junit.Assert.assertArrayEquals;
24+
import static org.junit.Assert.assertEquals;
25+
26+
import java.util.Arrays;
27+
import java.util.Collection;
28+
import java.util.Date;
29+
30+
import javax.xml.bind.DatatypeConverter;
31+
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.Parameterized;
35+
import org.junit.runners.Parameterized.Parameters;
36+
37+
import com.redhat.lightblue.common.ldap.LdapConstant;
38+
import com.redhat.lightblue.metadata.EntityMetadata;
39+
import com.redhat.lightblue.metadata.types.DateType;
40+
import com.redhat.lightblue.test.MetadataUtil;
41+
import com.redhat.lightblue.util.JsonDoc;
42+
import com.unboundid.ldap.sdk.Entry;
43+
import com.unboundid.util.StaticUtils;
44+
45+
@RunWith(value = Parameterized.class)
46+
public class EntryBuilderTest {
47+
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{
84+
String metadata = loadResource("./metadata/entryBuilderTest-metadata-template.json")
85+
.replaceFirst("#fieldname", fieldName)
86+
.replaceFirst("#type", metadataType);
87+
String crud = loadResource("./crud/insert/entryBuilderTest-insert-template.json")
88+
.replaceFirst("#fieldname", fieldName)
89+
.replaceFirst("#value", crudValue);
90+
91+
EntityMetadata md = MetadataUtil.createEntityMetadata(LdapConstant.BACKEND, json(metadata), null, null);
92+
EntryBuilder builder = new EntryBuilder(md);
93+
94+
Entry entry = builder.build("uid=someuid,dc=example,dc=com",
95+
new JsonDoc(json(crud).get("data")));
96+
97+
if(expectedValue.getClass().isArray()){
98+
assertArrayEquals((String[]) expectedValue, entry.getAttributeValues("testfield"));
99+
}
100+
else{
101+
assertEquals(expectedValue, entry.getAttributeValue("testfield"));
102+
}
103+
}
104+
105+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"entity": "entryBuilderTest",
3+
"entityVersion": "1.0.0",
4+
"data": {
5+
"#fieldname": #value
6+
}
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"entityInfo": {
3+
"name": "entryBuilderTest",
4+
"datastore": {
5+
"backend":"ldap",
6+
"database": "test",
7+
"basedn": "dc=example,dc=com",
8+
"uniqueattr": "uid"
9+
}
10+
},
11+
"schema": {
12+
"name": "entryBuilderTest",
13+
"version": {
14+
"value": "1.0.0",
15+
"changelog": "blahblah"
16+
},
17+
"status": {
18+
"value": "active"
19+
},
20+
"access" : {
21+
"insert": ["anyone"],
22+
"update": ["anyone"],
23+
"delete": ["anyone"],
24+
"find": ["anyone"]
25+
},
26+
"fields": {
27+
"#fieldname": #type
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)