Skip to content

Commit 8fd9be7

Browse files
committed
fixes #31 - Add ldap field to attribute mapping
1 parent afea8a2 commit 8fd9be7

File tree

9 files changed

+527
-29
lines changed

9 files changed

+527
-29
lines changed

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

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

21+
import java.util.List;
22+
23+
import com.redhat.lightblue.common.ldap.LdapConstant;
24+
import com.redhat.lightblue.metadata.MetadataConstants;
25+
import com.redhat.lightblue.metadata.ldap.parser.property.FieldToAttribute;
26+
import com.redhat.lightblue.metadata.ldap.parser.property.LdapProperty;
2127
import com.redhat.lightblue.metadata.parser.MetadataParser;
2228
import com.redhat.lightblue.metadata.parser.PropertyParser;
29+
import com.redhat.lightblue.util.Error;
2330

2431
/**
2532
* {@link PropertyParser} implementation for LDAP.
@@ -28,14 +35,49 @@
2835
*/
2936
public class LdapPropertyParser <T> extends PropertyParser<T> {
3037

31-
public Object parse(String name, MetadataParser<T> p, T node) {
32-
// TODO Auto-generated method stub
33-
return null;
38+
private static final String FIELDS_TO_ATTRIBUTES = "fieldsToAttributes";
39+
private static final String FIELD = "field";
40+
private static final String ATTRIBUTE = "attribute";
41+
42+
@Override
43+
public LdapProperty parse(String name, MetadataParser<T> p, T node) {
44+
if (!LdapConstant.BACKEND.equals(name)) {
45+
throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, name);
46+
}
47+
48+
LdapProperty ldapProperty = new LdapProperty();
49+
50+
List<T> fieldsToAttributesNode = p.getObjectList(node, FIELDS_TO_ATTRIBUTES);
51+
if(fieldsToAttributesNode != null){
52+
for(T fieldToAttributeNode : fieldsToAttributesNode){
53+
ldapProperty.addFieldToAttribute(new FieldToAttribute(
54+
p.getRequiredStringProperty(fieldToAttributeNode, FIELD),
55+
p.getRequiredStringProperty(fieldToAttributeNode, ATTRIBUTE)));
56+
}
57+
}
58+
59+
return ldapProperty;
3460
}
3561

62+
@Override
3663
public void convert(MetadataParser<T> p, T emptyNode, Object object) {
37-
// TODO Auto-generated method stub
64+
if(!(object instanceof LdapProperty)){
65+
throw new IllegalArgumentException("Source type " + object.getClass() + " is not supported.");
66+
}
67+
68+
LdapProperty ldapProperty = (LdapProperty) object;
69+
70+
if(!ldapProperty.getFieldsToAttributes().isEmpty()){
71+
Object fieldsToAttributesNode = p.newArrayField(emptyNode, FIELDS_TO_ATTRIBUTES);
72+
73+
for(FieldToAttribute fieldToAttribute : ldapProperty.getFieldsToAttributes()){
74+
T fieldToAttributeNode = p.newNode();
75+
p.putString(fieldToAttributeNode, FIELD, fieldToAttribute.getFieldName());
76+
p.putString(fieldToAttributeNode, ATTRIBUTE, fieldToAttribute.getAttributeName());
3877

78+
p.addObjectToArray(fieldsToAttributesNode, fieldToAttributeNode);
79+
}
80+
}
3981
}
4082

4183
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.metadata.ldap.parser.property;
20+
21+
/**
22+
* Maps the metadata field name to the Ldap attribute name.
23+
*
24+
* @author dcrissman
25+
*/
26+
public class FieldToAttribute {
27+
28+
private final String fieldName;
29+
private final String attributeName;
30+
31+
public String getFieldName() {
32+
return fieldName;
33+
}
34+
35+
public String getAttributeName() {
36+
return attributeName;
37+
}
38+
39+
public FieldToAttribute(String fieldName, String attributeName){
40+
this.fieldName = fieldName;
41+
this.attributeName = attributeName;
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
final int prime = 31;
47+
int result = 1;
48+
result = prime * result
49+
+ ((attributeName == null) ? 0 : attributeName.hashCode());
50+
result = prime * result
51+
+ ((fieldName == null) ? 0 : fieldName.hashCode());
52+
return result;
53+
}
54+
55+
@Override
56+
public boolean equals(Object obj) {
57+
if (this == obj) {
58+
return true;
59+
}
60+
if (obj == null) {
61+
return false;
62+
}
63+
if (getClass() != obj.getClass()) {
64+
return false;
65+
}
66+
FieldToAttribute other = (FieldToAttribute) obj;
67+
if (attributeName == null) {
68+
if (other.attributeName != null) {
69+
return false;
70+
}
71+
}
72+
else if (!attributeName.equals(other.attributeName)) {
73+
return false;
74+
}
75+
if (fieldName == null) {
76+
if (other.fieldName != null) {
77+
return false;
78+
}
79+
}
80+
else if (!fieldName.equals(other.fieldName)) {
81+
return false;
82+
}
83+
return true;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "FieldToAttribute [fieldName=" + fieldName + ", attributeName="
89+
+ attributeName + "]";
90+
}
91+
92+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.metadata.ldap.parser.property;
20+
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
24+
/**
25+
* Container for special ldap properties parsed from the metadata.json file.
26+
*
27+
* @author dcrissman
28+
*
29+
* @see com.redhat.lightblue.metadata.ldap.parser.LdapPropertyParser
30+
*/
31+
public class LdapProperty {
32+
33+
private final Set<FieldToAttribute> fieldsToAttributes = new HashSet<FieldToAttribute>();
34+
35+
public Set<FieldToAttribute> getFieldsToAttributes(){
36+
return fieldsToAttributes;
37+
}
38+
39+
public void addFieldToAttribute(FieldToAttribute fieldToAttribute){
40+
fieldsToAttributes.add(fieldToAttribute);
41+
}
42+
43+
}

lightblue-ldap-metadata/src/main/resources/json-schema/metadata/ldap/model/searchscope.json

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.metadata.ldap.parser;
20+
21+
import static com.redhat.lightblue.ldap.test.Assert.assertCollectionEquivalent;
22+
import static com.redhat.lightblue.util.JsonUtils.json;
23+
import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadJsonNode;
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertNotNull;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import java.io.IOException;
29+
import java.util.Arrays;
30+
import java.util.Set;
31+
32+
import org.json.JSONException;
33+
import org.junit.Rule;
34+
import org.junit.Test;
35+
import org.junit.rules.ExpectedException;
36+
import org.skyscreamer.jsonassert.JSONAssert;
37+
38+
import com.fasterxml.jackson.databind.JsonNode;
39+
import com.redhat.lightblue.common.ldap.LdapConstant;
40+
import com.redhat.lightblue.metadata.ldap.parser.property.FieldToAttribute;
41+
import com.redhat.lightblue.metadata.ldap.parser.property.LdapProperty;
42+
import com.redhat.lightblue.test.MetadataUtil;
43+
44+
public class LdapPropertyParserTest {
45+
46+
@Rule
47+
public ExpectedException expectedEx = ExpectedException.none();
48+
49+
@Test
50+
public void testParse() throws IOException{
51+
LdapProperty ldapProperty = new LdapPropertyParser<JsonNode>().parse(
52+
LdapConstant.BACKEND,
53+
MetadataUtil.createJSONMetadataParser(LdapConstant.BACKEND, null),
54+
loadJsonNode("./ldap-segment-metadata.json").get("ldap"));
55+
56+
assertNotNull(ldapProperty);
57+
58+
Set<FieldToAttribute> fieldsToAttributes = ldapProperty.getFieldsToAttributes();
59+
assertNotNull(fieldsToAttributes);
60+
assertEquals(2, fieldsToAttributes.size());
61+
62+
assertCollectionEquivalent(
63+
Arrays.asList(new FieldToAttribute("firstName", "givenName"), new FieldToAttribute("lastName", "sn")),
64+
fieldsToAttributes);
65+
}
66+
67+
@Test
68+
public void testParse_NoProperties() throws IOException{
69+
LdapProperty ldapProperty = new LdapPropertyParser<JsonNode>().parse(
70+
LdapConstant.BACKEND,
71+
MetadataUtil.createJSONMetadataParser(LdapConstant.BACKEND, null),
72+
json("{\"ldap\": {}}").get("ldap"));
73+
74+
assertNotNull(ldapProperty);
75+
76+
assertTrue(ldapProperty.getFieldsToAttributes().isEmpty());
77+
}
78+
79+
@Test
80+
public void testParse_IncorrectBackend(){
81+
expectedEx.expect(com.redhat.lightblue.util.Error.class);
82+
expectedEx.expectMessage("{\"objectType\":\"error\",\"errorCode\":\"metadata:IllFormedMetadata\",\"msg\":\"fakebackend\"}");
83+
84+
new LdapPropertyParser<JsonNode>().parse("fakebackend", null, null);
85+
}
86+
87+
@Test
88+
public void testConvert() throws IOException, JSONException{
89+
LdapProperty ldapProperty = new LdapProperty();
90+
ldapProperty.addFieldToAttribute(new FieldToAttribute("firstName", "givenName"));
91+
ldapProperty.addFieldToAttribute(new FieldToAttribute("lastName", "sn"));
92+
93+
JsonNode node = json("{}");
94+
95+
new LdapPropertyParser<JsonNode>().convert(
96+
MetadataUtil.createJSONMetadataParser(LdapConstant.BACKEND, null),
97+
node,
98+
ldapProperty);
99+
100+
JSONAssert.assertEquals("{\"fieldsToAttributes\":[{\"field\":\"lastName\",\"attribute\":\"sn\"},{\"field\":\"firstName\",\"attribute\":\"givenName\"}]}",
101+
node.toString(), true);
102+
}
103+
104+
@Test
105+
public void testConvert_NoMappings() throws IOException, JSONException{
106+
LdapProperty ldapProperty = new LdapProperty();
107+
108+
JsonNode node = json("{}");
109+
110+
new LdapPropertyParser<JsonNode>().convert(
111+
MetadataUtil.createJSONMetadataParser(LdapConstant.BACKEND, null),
112+
node,
113+
ldapProperty);
114+
115+
JSONAssert.assertEquals("{}",
116+
node.toString(), true);
117+
}
118+
119+
@Test(expected = IllegalArgumentException.class)
120+
public void testConvert_invalidObject(){
121+
new LdapPropertyParser<JsonNode>().convert(null, null, new Object());
122+
}
123+
124+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"ldap": {
3+
"fieldsToAttributes": [
4+
{
5+
"field": "firstName",
6+
"attribute": "givenName"
7+
},
8+
{
9+
"field": "lastName",
10+
"attribute": "sn"
11+
}
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)