Skip to content

Commit 8c7927b

Browse files
committed
Removed FieldAttributeMapping in favour of a HashMap
1 parent 04f462f commit 8c7927b

File tree

6 files changed

+95
-131
lines changed

6 files changed

+95
-131
lines changed

lightblue-ldap-metadata/src/main/java/com/redhat/lightblue/metadata/ldap/model/FieldAttributeMapping.java

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

lightblue-ldap-metadata/src/main/java/com/redhat/lightblue/metadata/ldap/model/LdapProperty.java

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

21-
import java.util.HashSet;
22-
import java.util.Set;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.Map.Entry;
2324

2425
import com.redhat.lightblue.common.ldap.LdapFieldNameTranslator;
2526

@@ -32,31 +33,31 @@
3233
*/
3334
public class LdapProperty implements LdapFieldNameTranslator{
3435

35-
private final Set<FieldAttributeMapping> fieldsToAttributes = new HashSet<FieldAttributeMapping>();
36+
private final Map<String, String> fieldsToAttributes = new HashMap<String, String>();
3637

3738
/**
3839
* Returns an immutable copy of the internal collection of {@link FieldAttributeMapping}s.
3940
* @return a collection of {@link FieldAttributeMapping}s.
4041
*/
41-
public Set<FieldAttributeMapping> getFieldsToAttributes(){
42-
return new HashSet<FieldAttributeMapping>(fieldsToAttributes);
42+
public Map<String, String> getFieldsToAttributes(){
43+
return new HashMap<String, String>(fieldsToAttributes);
4344
}
4445

4546
@Override
4647
public String translateFieldName(String fieldName){
47-
for(FieldAttributeMapping f2a : fieldsToAttributes){
48-
if(f2a.getFieldName().equalsIgnoreCase(fieldName)){
49-
return f2a.getAttributeName();
50-
}
48+
String attributeName = fieldsToAttributes.get(fieldName);
49+
if(attributeName == null){
50+
return fieldName;
5151
}
52-
return fieldName;
52+
53+
return attributeName;
5354
}
5455

5556
@Override
5657
public String translateAttributeName(String attributeName){
57-
for(FieldAttributeMapping f2a : fieldsToAttributes){
58-
if(f2a.getAttributeName().equalsIgnoreCase(attributeName)){
59-
return f2a.getFieldName();
58+
for(Entry<String, String> f2a : fieldsToAttributes.entrySet()){
59+
if(f2a.getValue().equalsIgnoreCase(attributeName)){
60+
return f2a.getKey();
6061
}
6162
}
6263
return attributeName;
@@ -66,8 +67,8 @@ public String translateAttributeName(String attributeName){
6667
* Adds a {@link FieldAttributeMapping} to this {@link LdapProperty}.
6768
* @param fieldAttributeMapping - {@link FieldAttributeMapping}
6869
*/
69-
public void addFieldToAttribute(FieldAttributeMapping fieldAttributeMapping){
70-
fieldsToAttributes.add(fieldAttributeMapping);
70+
public void addFieldToAttribute(String fieldName, String attributeName){
71+
fieldsToAttributes.put(fieldName, attributeName);
7172
}
7273

7374
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
package com.redhat.lightblue.metadata.ldap.parser;
2020

2121
import java.util.List;
22+
import java.util.Map.Entry;
2223

2324
import com.redhat.lightblue.common.ldap.LdapConstant;
2425
import com.redhat.lightblue.metadata.MetadataConstants;
25-
import com.redhat.lightblue.metadata.ldap.model.FieldAttributeMapping;
2626
import com.redhat.lightblue.metadata.ldap.model.LdapProperty;
2727
import com.redhat.lightblue.metadata.parser.MetadataParser;
2828
import com.redhat.lightblue.metadata.parser.PropertyParser;
@@ -50,9 +50,9 @@ public com.redhat.lightblue.metadata.ldap.model.LdapProperty parse(String name,
5050
List<T> fieldsToAttributesNode = p.getObjectList(node, FIELDS_TO_ATTRIBUTES);
5151
if(fieldsToAttributesNode != null){
5252
for(T fieldToAttributeNode : fieldsToAttributesNode){
53-
ldapProperty.addFieldToAttribute(new FieldAttributeMapping(
53+
ldapProperty.addFieldToAttribute(
5454
p.getRequiredStringProperty(fieldToAttributeNode, FIELD),
55-
p.getRequiredStringProperty(fieldToAttributeNode, ATTRIBUTE)));
55+
p.getRequiredStringProperty(fieldToAttributeNode, ATTRIBUTE));
5656
}
5757
}
5858

@@ -70,10 +70,10 @@ public void convert(MetadataParser<T> p, T emptyNode, Object object) {
7070
if(!ldapProperty.getFieldsToAttributes().isEmpty()){
7171
Object fieldsToAttributesNode = p.newArrayField(emptyNode, FIELDS_TO_ATTRIBUTES);
7272

73-
for(FieldAttributeMapping fieldAttributeMapping : ldapProperty.getFieldsToAttributes()){
73+
for(Entry<String, String> entry : ldapProperty.getFieldsToAttributes().entrySet()){
7474
T fieldToAttributeNode = p.newNode();
75-
p.putString(fieldToAttributeNode, FIELD, fieldAttributeMapping.getFieldName());
76-
p.putString(fieldToAttributeNode, ATTRIBUTE, fieldAttributeMapping.getAttributeName());
75+
p.putString(fieldToAttributeNode, FIELD, entry.getKey());
76+
p.putString(fieldToAttributeNode, ATTRIBUTE, entry.getValue());
7777

7878
p.addObjectToArray(fieldsToAttributesNode, fieldToAttributeNode);
7979
}

lightblue-ldap-metadata/src/test/java/com/redhat/lightblue/metadata/ldap/model/LdapPropertyTest.java

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

21+
import static com.redhat.lightblue.ldap.test.Assert.assertMapEquivalent;
2122
import static org.junit.Assert.assertEquals;
2223
import static org.junit.Assert.assertNotNull;
2324
import static org.junit.Assert.assertNotSame;
2425

25-
import java.util.Set;
26+
import java.util.Map;
2627

2728
import org.junit.Test;
2829

@@ -34,8 +35,8 @@ public void testTranslateFieldName(){
3435
String attributeName = "fakeAttributeName";
3536

3637
LdapProperty property = new LdapProperty();
37-
property.addFieldToAttribute(new FieldAttributeMapping(fieldName, attributeName));
38-
property.addFieldToAttribute(new FieldAttributeMapping("anotherField", "anotherAttribute"));
38+
property.addFieldToAttribute(fieldName, attributeName);
39+
property.addFieldToAttribute("anotherField", "anotherAttribute");
3940

4041
assertEquals(attributeName, property.translateFieldName(fieldName));
4142
}
@@ -53,8 +54,8 @@ public void testTranslateAttributeName(){
5354
String attributeName = "fakeAttributeName";
5455

5556
LdapProperty property = new LdapProperty();
56-
property.addFieldToAttribute(new FieldAttributeMapping(fieldName, attributeName));
57-
property.addFieldToAttribute(new FieldAttributeMapping("anotherField", "anotherAttribute"));
57+
property.addFieldToAttribute(fieldName, attributeName);
58+
property.addFieldToAttribute("anotherField", "anotherAttribute");
5859

5960
assertEquals(fieldName, property.translateAttributeName(attributeName));
6061
}
@@ -69,10 +70,11 @@ public void testTranslateAttributeName_ValueNotPresent(){
6970
@Test
7071
public void testGetFieldsToAttributes_AssertImmutable(){
7172
LdapProperty property = new LdapProperty();
72-
property.addFieldToAttribute(new FieldAttributeMapping("anotherField", "anotherAttribute"));
73+
property.addFieldToAttribute("anotherField", "anotherAttribute");
7374

74-
Set<FieldAttributeMapping> fieldsToAttributes = property.getFieldsToAttributes();
75+
Map<String, String> fieldsToAttributes = property.getFieldsToAttributes();
7576
assertNotNull(fieldsToAttributes);
77+
assertMapEquivalent(fieldsToAttributes, property.getFieldsToAttributes());
7678
assertNotSame(fieldsToAttributes, property.getFieldsToAttributes());
7779
}
7880

lightblue-ldap-metadata/src/test/java/com/redhat/lightblue/metadata/ldap/parser/LdapPropertyParserTest.java

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

21-
import static com.redhat.lightblue.ldap.test.Assert.assertCollectionEquivalent;
21+
import static com.redhat.lightblue.ldap.test.Assert.assertMapEquivalent;
2222
import static com.redhat.lightblue.util.JsonUtils.json;
2323
import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadJsonNode;
2424
import static org.junit.Assert.assertEquals;
2525
import static org.junit.Assert.assertNotNull;
2626
import static org.junit.Assert.assertTrue;
2727

2828
import java.io.IOException;
29-
import java.util.Arrays;
30-
import java.util.Set;
29+
import java.util.HashMap;
30+
import java.util.Map;
3131

3232
import org.json.JSONException;
3333
import org.junit.Rule;
@@ -37,7 +37,6 @@
3737

3838
import com.fasterxml.jackson.databind.JsonNode;
3939
import com.redhat.lightblue.common.ldap.LdapConstant;
40-
import com.redhat.lightblue.metadata.ldap.model.FieldAttributeMapping;
4140
import com.redhat.lightblue.metadata.ldap.model.LdapProperty;
4241
import com.redhat.lightblue.test.MetadataUtil;
4342

@@ -55,13 +54,14 @@ public void testParse() throws IOException{
5554

5655
assertNotNull(ldapProperty);
5756

58-
Set<FieldAttributeMapping> fieldsToAttributes = ldapProperty.getFieldsToAttributes();
57+
Map<String, String> fieldsToAttributes = ldapProperty.getFieldsToAttributes();
5958
assertNotNull(fieldsToAttributes);
6059
assertEquals(2, fieldsToAttributes.size());
6160

62-
assertCollectionEquivalent(
63-
Arrays.asList(new FieldAttributeMapping("firstName", "givenName"), new FieldAttributeMapping("lastName", "sn")),
64-
fieldsToAttributes);
61+
Map<String, String> expected = new HashMap<String, String>();
62+
expected.put("firstName", "givenName");
63+
expected.put("lastName", "sn");
64+
assertMapEquivalent(expected, fieldsToAttributes);
6565
}
6666

6767
@Test
@@ -87,8 +87,8 @@ public void testParse_IncorrectBackend(){
8787
@Test
8888
public void testConvert() throws IOException, JSONException{
8989
LdapProperty ldapProperty = new LdapProperty();
90-
ldapProperty.addFieldToAttribute(new FieldAttributeMapping("firstName", "givenName"));
91-
ldapProperty.addFieldToAttribute(new FieldAttributeMapping("lastName", "sn"));
90+
ldapProperty.addFieldToAttribute("firstName", "givenName");
91+
ldapProperty.addFieldToAttribute("lastName", "sn");
9292

9393
JsonNode node = json("{}");
9494

lightblue-ldap-test/src/main/java/com/redhat/lightblue/ldap/test/Assert.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Arrays;
2727
import java.util.Collection;
2828
import java.util.List;
29+
import java.util.Map;
2930

3031
import org.apache.commons.lang.StringUtils;
3132

@@ -38,6 +39,52 @@ public final class Assert {
3839

3940
private Assert(){}
4041

42+
/**
43+
* <p>Asserts that the keys and values of the two maps are equal.</p>
44+
* @param expectedMap
45+
* @param actualMap
46+
*/
47+
public static <K,V> void assertMapEquivalent(Map<K,V> expectedMap, Map<K,V> actualMap){
48+
assertMapEquivalent(expectedMap, actualMap, new DefaultEquivalencyEvaluator());
49+
}
50+
51+
/**
52+
* <p>Asserts that the keys and values of the two maps are equal.</p>
53+
* @param expectedMap
54+
* @param actualMap
55+
* @param evaluator
56+
*/
57+
public static <K,V> void assertMapEquivalent(Map<K,V> expectedMap, Map<K,V> actualMap, EquivalencyEvaluator evaluator){
58+
assertBothNullsOrNotNulls(expectedMap, actualMap);
59+
60+
List<String> collector = new ArrayList<String>();
61+
62+
try{
63+
assertEquals("Map size does not match", expectedMap.size(), actualMap.size());
64+
}
65+
catch(AssertionError e){
66+
collector.add(e.getMessage());
67+
}
68+
69+
try{
70+
assertCollectionEquivalent(expectedMap.keySet(), actualMap.keySet(), evaluator);
71+
}
72+
catch(AssertionError e){
73+
collector.add(e.getMessage());
74+
}
75+
76+
try{
77+
assertCollectionEquivalent(expectedMap.values(), actualMap.values(), evaluator);
78+
}
79+
catch(AssertionError e){
80+
collector.add(e.getMessage());
81+
}
82+
83+
if(!collector.isEmpty()){
84+
throw new AssertionError(StringUtils.join(collector, "\n"));
85+
}
86+
}
87+
4188
/**
4289
* <p>Asserts that the two arrays are equal without regard for Object position.</p>
4390
* <p>For Example: [5,3,2] is equivalent to [2,3,5].</p>
@@ -58,7 +105,13 @@ public static void assertCollectionEquivalent(Collection<?> expecteds, Collectio
58105
assertCollectionEquivalent(expecteds, actuals, new DefaultEquivalencyEvaluator());
59106
}
60107

61-
108+
/**
109+
* <p>Asserts that the two Lists are equal without regard for Object position.</p>
110+
* <p>For Example: [5,3,2] is equivalent to [2,3,5].</p>
111+
* @param expecteds
112+
* @param actuals
113+
* @param evaluator
114+
*/
62115
public static void assertCollectionEquivalent(Collection<?> expecteds, Collection<?> actuals, EquivalencyEvaluator evaluator){
63116
assertBothNullsOrNotNulls(expecteds, actuals);
64117

0 commit comments

Comments
 (0)