Skip to content

Commit bde9a42

Browse files
committed
continue working on ITCaseLdapCRUDControllerTest
1 parent 7326c4a commit bde9a42

File tree

15 files changed

+187
-115
lines changed

15 files changed

+187
-115
lines changed

lightblue-ldap-config/src/main/java/com/redhat/lightblue/config/ldap/LdapControllerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package com.redhat.lightblue.config.ldap;
2020

21+
import java.util.HashSet;
22+
2123
import com.redhat.lightblue.config.ControllerConfiguration;
2224
import com.redhat.lightblue.config.ControllerFactory;
2325
import com.redhat.lightblue.config.DataSourcesConfiguration;
@@ -27,8 +29,8 @@
2729
public class LdapControllerFactory implements ControllerFactory{
2830

2931
public CRUDController createController(ControllerConfiguration cfg, DataSourcesConfiguration ds) {
30-
return new LdapCRUDController(
31-
new LdapDBResolver(ds.getDataSourcesByType(LdapDataSourceConfiguration.class)));
32+
return new LdapCRUDController(new LdapDBResolver(
33+
new HashSet<LdapDataSourceConfiguration>(ds.getDataSourcesByType(LdapDataSourceConfiguration.class).values())));
3234
}
3335

3436
}

lightblue-ldap-config/src/main/java/com/redhat/lightblue/config/ldap/LdapDBResolver.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.redhat.lightblue.config.ldap;
2020

21-
import java.util.Map;
21+
import java.util.Set;
2222

2323
import com.redhat.lightblue.common.ldap.DBResolver;
2424
import com.redhat.lightblue.common.ldap.LdapDataStore;
@@ -28,9 +28,9 @@
2828

2929
public class LdapDBResolver implements DBResolver{
3030

31-
private final Map<String, LdapDataSourceConfiguration> ldapDataSources;
31+
private final Set<LdapDataSourceConfiguration> ldapDataSources;
3232

33-
public LdapDBResolver(Map<String, LdapDataSourceConfiguration> ldapDataSources){
33+
public LdapDBResolver(Set<LdapDataSourceConfiguration> ldapDataSources){
3434
this.ldapDataSources = ldapDataSources;
3535
}
3636

@@ -43,11 +43,24 @@ public LDAPConnection get(DataStore store) throws LDAPException {
4343
}
4444

4545
public LDAPConnection get(String database) throws LDAPException{
46-
LdapDataSourceConfiguration cnf = ldapDataSources.get(database);
46+
LdapDataSourceConfiguration cnf = findByDatabase(database);
4747
if(cnf == null){
4848
throw new IllegalArgumentException("No database for " + database);
4949
}
5050
return cnf.getLdapConnection();
5151
}
5252

53+
private LdapDataSourceConfiguration findByDatabase(String database){
54+
if(database == null){
55+
return null;
56+
}
57+
58+
for(LdapDataSourceConfiguration cnf : ldapDataSources){
59+
if(database.equals(cnf.getDatabaseName())){
60+
return cnf;
61+
}
62+
}
63+
return null;
64+
}
65+
5366
}

lightblue-ldap-config/src/test/java/com/redhat/lightblue/config/ldap/LdapDBResolverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.redhat.lightblue.config.ldap;
2020

21-
import java.util.HashMap;
21+
import java.util.HashSet;
2222

2323
import org.junit.Test;
2424

@@ -28,7 +28,7 @@ public class LdapDBResolverTest {
2828

2929
@Test(expected = IllegalArgumentException.class)
3030
public void testGet_UnknownDatabase() throws LDAPException{
31-
LdapDBResolver resolver = new LdapDBResolver(new HashMap<String, LdapDataSourceConfiguration>());
31+
LdapDBResolver resolver = new LdapDBResolver(new HashSet<LdapDataSourceConfiguration>());
3232
resolver.get("Does Not Exist");
3333
}
3434

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/LdapCRUDController.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
*/
1919
package com.redhat.lightblue.crud.ldap;
2020

21+
import java.util.ArrayList;
22+
import java.util.Iterator;
2123
import java.util.List;
24+
import java.util.Map;
2225

2326
import org.slf4j.Logger;
2427
import org.slf4j.LoggerFactory;
2528

26-
import com.fasterxml.jackson.databind.node.ObjectNode;
29+
import com.fasterxml.jackson.databind.JsonNode;
2730
import com.redhat.lightblue.common.ldap.DBResolver;
2831
import com.redhat.lightblue.crud.CRUDController;
2932
import com.redhat.lightblue.crud.CRUDDeleteResponse;
@@ -34,7 +37,6 @@
3437
import com.redhat.lightblue.crud.CRUDUpdateResponse;
3538
import com.redhat.lightblue.crud.DocCtx;
3639
import com.redhat.lightblue.eval.FieldAccessRoleEvaluator;
37-
import com.redhat.lightblue.eval.Projector;
3840
import com.redhat.lightblue.hystrix.ldap.InsertCommand;
3941
import com.redhat.lightblue.metadata.EntityMetadata;
4042
import com.redhat.lightblue.metadata.MetadataListener;
@@ -43,6 +45,7 @@
4345
import com.redhat.lightblue.query.Sort;
4446
import com.redhat.lightblue.query.UpdateExpression;
4547
import com.redhat.lightblue.util.JsonDoc;
48+
import com.unboundid.ldap.sdk.Attribute;
4649
import com.unboundid.ldap.sdk.Entry;
4750
import com.unboundid.ldap.sdk.LDAPConnection;
4851
import com.unboundid.ldap.sdk.LDAPException;
@@ -53,6 +56,8 @@ public class LdapCRUDController implements CRUDController{
5356

5457
private static final Logger LOGGER = LoggerFactory.getLogger(LdapCRUDController.class);
5558

59+
private static final String DN = "dn";
60+
5661
private final DBResolver dbResolver;
5762

5863
public LdapCRUDController(DBResolver dbResolver){
@@ -76,16 +81,44 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
7681
projection,
7782
roleEval.getExcludedFields(FieldAccessRoleEvaluator.Operation.insert));
7883

79-
Projector projector = null;
84+
//TODO Revisit Projection
85+
/* Projector projector = null;
8086
if(combinedProjection != null){
8187
projector = Projector.getInstance(combinedProjection, md);
82-
}
88+
}*/
8389

8490
try {
8591
LDAPConnection connection = dbResolver.get(md.getDataStore());
8692

8793
for(DocCtx document : documents){
88-
Entry entry = new Entry(""); //TODO populate Entry
94+
//document.setOriginalDocument(document);
95+
JsonNode rootNode = document.getRoot();
96+
JsonNode dnNode = rootNode.get(DN);
97+
if(dnNode == null){
98+
throw new IllegalArgumentException("dn is a required field");
99+
}
100+
101+
Entry entry = new Entry(dnNode.asText());
102+
103+
Iterator<Map.Entry<String, JsonNode>> nodeIterator = rootNode.fields();
104+
while(nodeIterator.hasNext()){
105+
Map.Entry<String, JsonNode> node = nodeIterator.next();
106+
if(DN.equalsIgnoreCase(node.getKey())){
107+
continue;
108+
}
109+
110+
JsonNode valueNode = node.getValue();
111+
if(valueNode.isArray()){
112+
List<String> values = new ArrayList<String>();
113+
for(JsonNode string : valueNode){
114+
values.add(string.asText());
115+
}
116+
entry.addAttribute(new Attribute(node.getKey(), values));
117+
}
118+
else{
119+
entry.addAttribute(new Attribute(node.getKey(), node.getValue().asText()));
120+
}
121+
}
89122

90123
InsertCommand command = new InsertCommand(connection, entry);
91124

@@ -95,13 +128,13 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
95128
continue;
96129
}
97130

98-
if(projector != null){
131+
/*if(projector != null){
99132
JsonDoc jsonDoc = null; //TODO: actually populate field.
100133
document.setOutputDocument(projector.project(jsonDoc, ctx.getFactory().getNodeFactory()));
101134
}
102-
else{
103-
document.setOutputDocument(new JsonDoc(new ObjectNode(ctx.getFactory().getNodeFactory())));
104-
}
135+
else{*/
136+
//document.setOutputDocument(new JsonDoc(new ObjectNode(ctx.getFactory().getNodeFactory())));
137+
//}
105138

106139
response.setNumInserted(response.getNumInserted() + 1);
107140
}

lightblue-ldap-crud/src/test/resources/crud/insert/insert-many.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

lightblue-ldap-integration-test/src/test/java/com/redhat/lightblue/crud/ldap/ITCaseLdapCRUDControllerTest.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
*/
1919
package com.redhat.lightblue.crud.ldap;
2020

21+
import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadJsonNode;
22+
import static org.junit.Assert.assertEquals;
2123
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.fail;
2324

2425
import java.io.IOException;
2526

2627
import org.junit.AfterClass;
2728
import org.junit.BeforeClass;
2829
import org.junit.ClassRule;
30+
import org.junit.Rule;
2931
import org.junit.Test;
32+
import org.junit.rules.ErrorCollector;
3033

3134
import com.redhat.lightblue.DataError;
3235
import com.redhat.lightblue.Response;
@@ -36,11 +39,12 @@
3639
import com.redhat.lightblue.crud.InsertionRequest;
3740
import com.redhat.lightblue.ldap.test.LdapServerExternalResource;
3841
import com.redhat.lightblue.ldap.test.LdapServerExternalResource.InMemoryLdapServer;
42+
import com.redhat.lightblue.mediator.Mediator;
43+
import com.redhat.lightblue.metadata.EntityMetadata;
44+
import com.redhat.lightblue.metadata.Metadata;
3945
import com.redhat.lightblue.mongo.test.MongoServerExternalResource;
4046
import com.redhat.lightblue.mongo.test.MongoServerExternalResource.InMemoryMongoServer;
4147
import com.redhat.lightblue.util.Error;
42-
import com.redhat.lightblue.util.JsonUtils;
43-
import com.redhat.lightblue.util.test.AbstractJsonNodeTest;
4448

4549
@InMemoryLdapServer
4650
@InMemoryMongoServer
@@ -52,18 +56,23 @@ public class ITCaseLdapCRUDControllerTest{
5256
@ClassRule
5357
public static MongoServerExternalResource mongoServer = new MongoServerExternalResource();
5458

59+
@Rule
60+
public ErrorCollector collector = new ErrorCollector();
61+
5562
public static LightblueFactory lightblueFactory;
5663

5764
@BeforeClass
5865
public static void beforeClass() throws IOException {
5966
System.setProperty("ldap.host", "localhost");
6067
System.setProperty("ldap.port", String.valueOf(LdapServerExternalResource.DEFAULT_PORT));
68+
System.setProperty("ldap.database", "test");
6169

6270
System.setProperty("mongo.host", "localhost");
6371
System.setProperty("mongo.port", String.valueOf(MongoServerExternalResource.DEFAULT_PORT));
64-
72+
System.setProperty("mongo.database", "lightblue");
73+
6574
lightblueFactory = new LightblueFactory(
66-
new DataSourcesConfiguration(AbstractJsonNodeTest.loadJsonNode("./datasources.json")));
75+
new DataSourcesConfiguration(loadJsonNode("./datasources.json")));
6776
}
6877

6978
@AfterClass
@@ -74,35 +83,32 @@ public static void after(){
7483
@Test
7584
public void testInsert() throws Exception{
7685
JsonTranslator tx = lightblueFactory.getJsonTranslator();
77-
InsertionRequest insertIequest = tx.parse(InsertionRequest.class, JsonUtils.json("{}"));
78-
Response response = lightblueFactory.getMediator().insert(insertIequest);
86+
87+
Metadata metadata = lightblueFactory.getMetadata();
88+
metadata.createNewMetadata(tx.parse(EntityMetadata.class, loadJsonNode("./metadata/ldap-person-metadata.json")));
89+
90+
InsertionRequest insertIequest = tx.parse(InsertionRequest.class, loadJsonNode("./crud/insert/insert-single.json"));
91+
92+
Mediator mediator = lightblueFactory.getMediator();
93+
Response response = mediator.insert(insertIequest);
7994

8095
assertNotNull(response);
81-
//assertNoErrors(response);
96+
assertNoErrors(response);
97+
assertEquals(1, response.getModifiedCount());
8298
}
8399

84100
private void assertNoErrors(Response response){
85-
if(response.getErrors().isEmpty() && response.getDataErrors().isEmpty()){
86-
return;
101+
for(Error error : response.getErrors()){
102+
Exception e = new Exception(error.getMessage(), error);
103+
e.printStackTrace();
104+
collector.addError(e);
87105
}
88106

89-
StringBuilder builder = new StringBuilder();
90-
91-
if(!response.getErrors().isEmpty()){
92-
builder.append("Response has Errors: \n");
93-
for(Error error : response.getErrors()){
94-
builder.append(error.toJson()+ "\n");
95-
}
96-
}
97-
98-
if(!response.getDataErrors().isEmpty()){
99-
builder.append("Response has Data Errors: \n");
100-
for(DataError error : response.getDataErrors()){
101-
builder.append(error.toJson() + "\n");
102-
}
107+
for(DataError error : response.getDataErrors()){
108+
Exception e = new Exception("DataError: " + error.toJson().asText());
109+
e.printStackTrace();
110+
collector.addError(e);
103111
}
104-
105-
fail(builder.toString());
106112
}
107113

108114
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# set default isolation strategy for tests to SEMAPHORE else breakpoints in IDE
2+
# when debugging unit tests will timeout with default strategy (THREAD).
3+
hystrix.command.default.execution.isolation.strategy=SEMAPHORE
4+

lightblue-ldap-integration-test/src/test/resources/datasources.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ldap": {
33
"type" : "com.redhat.lightblue.config.ldap.LdapDataSourceConfiguration",
4-
"database" : "name of ldap server",
4+
"database" : "${ldap.database}",
55

66
"bindableDn" : "uid=admin,dc=example,dc=com",
77
"password" : "password",
@@ -18,11 +18,11 @@
1818
"type" : "com.redhat.lightblue.mongo.config.MongoConfiguration",
1919
"metadataBackendParser" : "com.redhat.lightblue.metadata.mongo.MongoBackendParser",
2020
"ssl" : false,
21-
"database" : "mongo",
21+
"database" : "${mongo.database}",
2222
"servers" : [
2323
{
24-
"host" : "${mongodb.host}",
25-
"port" : "${mongodb.port}"
24+
"host" : "${mongo.host}",
25+
"port" : "${mongo.port}"
2626
}
2727
]
2828
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
{
22
"type" : "com.redhat.lightblue.mongo.config.MongoMetadataConfiguration",
33
"dataSource" : "mongo",
4-
"collection": "metadata"
4+
"collection": "metadata",
5+
"backendParsers": [
6+
{
7+
"name": "ldap",
8+
"clazz": "com.redhat.lightblue.metadata.ldap.parser.LdapDataStoreParser"
9+
}
10+
],
11+
"propertyParsers": [
12+
{
13+
"name": "ldap",
14+
"clazz": "com.redhat.lightblue.metadata.ldap.parser.LdapPropertyParser"
15+
}
16+
]
517
}

lightblue-ldap-metadata/src/main/java/com/redhat/lightblue/metadata/ldap/parser/LdapDataStoreParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public DataStore parse(String name, MetadataParser<T> p, T node) {
3333
}
3434

3535
LdapDataStore dataStore = new LdapDataStore();
36+
dataStore.setDatabase(p.getRequiredStringProperty(node, "database"));
3637

3738
return dataStore;
3839
}

0 commit comments

Comments
 (0)