Skip to content

Commit 8b8bb1a

Browse files
committed
Merge pull request #28 from dcrissman/better-array-checking
fixes #16 - adds check to ensure that an array count field actually has an array defined in the entity metadata
2 parents 0b36189 + e106fb4 commit 8b8bb1a

File tree

4 files changed

+127
-28
lines changed

4 files changed

+127
-28
lines changed

lightblue-ldap-common/src/main/java/com/redhat/lightblue/common/ldap/LightblueUtil.java

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

21+
import com.redhat.lightblue.metadata.ArrayField;
22+
import com.redhat.lightblue.metadata.Field;
23+
import com.redhat.lightblue.metadata.Fields;
24+
2125
/**
2226
* Utility methods to assist with interacting with the Lightblue framework.
2327
*
@@ -26,25 +30,78 @@
2630
public final class LightblueUtil {
2731

2832
public static final String FIELD_OBJECT_TYPE = "objectType";
29-
public static final String FIELD_MOD_ARRAY_COUNT = "#";
33+
public static final String FIELD_ARRAY_COUNT_POSTFIX = "#";
3034

35+
/**
36+
* Returns <code>true</code> if the passed in field name is the object type, otherwise <code>false</code>.
37+
* @param fieldName - field name to test.
38+
* @return <code>true</code> if the passed in field name is the object type, otherwise <code>false</code>.
39+
*/
3140
public static boolean isFieldObjectType(String fieldName){
41+
if(fieldName == null){
42+
return false;
43+
}
44+
3245
return FIELD_OBJECT_TYPE.equalsIgnoreCase(fieldName);
3346
}
3447

35-
public static boolean isFieldAnArrayCount(String fieldName){
48+
/**
49+
* Returns <code>true</code> if the passed in field name matches the array count field name pattern,
50+
* otherwise <code>false</code>.
51+
* @param fieldName - field name to test.
52+
* @return <code>true</code> if the passed in field name matches the array count field name pattern,
53+
* otherwise <code>false</code>.
54+
*/
55+
protected static boolean doesFieldNameMatchArrayCountPattern(String fieldName){
3656
if(fieldName == null){
3757
return false;
3858
}
39-
return fieldName.endsWith(FIELD_MOD_ARRAY_COUNT);
59+
return fieldName.endsWith(FIELD_ARRAY_COUNT_POSTFIX);
4060
}
4161

42-
public static boolean isFieldPredefined(String fieldName){
43-
return isFieldObjectType(fieldName) || isFieldAnArrayCount(fieldName);
62+
/**
63+
* Returns <code>true</code> if the passed in field name is the count field for an array field,
64+
* otherwise <code>false</code>.
65+
* @param fieldName - field name to test.
66+
* @param metadataFields - {@link Fields} from entity metadata.
67+
* @return <code>true</code> if the passed in field name is the count field for an array field,
68+
* otherwise <code>false</code>.
69+
*/
70+
public static boolean isFieldAnArrayCount(String fieldName, Fields metadataFields){
71+
if(!doesFieldNameMatchArrayCountPattern(fieldName)){
72+
return false;
73+
}
74+
75+
Field field = metadataFields.getField(createArrayFieldNameFromCountField(fieldName));
76+
if((field != null) && (field instanceof ArrayField)){
77+
return true;
78+
}
79+
80+
return false;
4481
}
4582

83+
/**
84+
* Created and returns the name of the array count field for the passed in array field name.
85+
* @param arrayFieldName - array field name.
86+
* @return name of the array count field for the passed in array field name.
87+
*/
4688
public static String createArrayCountFieldName(String arrayFieldName){
47-
return arrayFieldName + FIELD_MOD_ARRAY_COUNT;
89+
return arrayFieldName + FIELD_ARRAY_COUNT_POSTFIX;
90+
}
91+
92+
/**
93+
* Creates and returns the array field name for the passed in array count field name.
94+
* @param countField - array count field.
95+
* @return the array field name for the passed in array count field name.
96+
*/
97+
public static String createArrayFieldNameFromCountField(String countField){
98+
if(countField == null){
99+
return null;
100+
}
101+
if(!doesFieldNameMatchArrayCountPattern(countField)){
102+
return countField;
103+
}
104+
return countField.substring(0, countField.length() - 1);
48105
}
49106

50107
private LightblueUtil(){}

lightblue-ldap-common/src/test/java/com/redhat/lightblue/common/ldap/LightblueUtilTest.java

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020

2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
2425

2526
import org.junit.Test;
2627

28+
import com.redhat.lightblue.metadata.ArrayField;
29+
import com.redhat.lightblue.metadata.Fields;
30+
import com.redhat.lightblue.metadata.SimpleField;
31+
2732
public class LightblueUtilTest {
2833

2934
@Test
@@ -37,43 +42,76 @@ public void testIsFieldObjectType_False(){
3742
}
3843

3944
@Test
40-
public void testIsFieldAnArrayCount_True(){
41-
assertTrue(LightblueUtil.isFieldAnArrayCount("somearray" + LightblueUtil.FIELD_MOD_ARRAY_COUNT));
45+
public void testIsFieldObjectType_NullValue(){
46+
assertFalse(LightblueUtil.isFieldObjectType(null));
4247
}
4348

4449
@Test
45-
public void testIsFieldAnArrayCount_NullValue(){
46-
assertFalse(LightblueUtil.isFieldAnArrayCount(null));
50+
public void testDoesFieldNameMatchArrayCountPattern_True(){
51+
assertTrue(LightblueUtil.doesFieldNameMatchArrayCountPattern("somearray" + LightblueUtil.FIELD_ARRAY_COUNT_POSTFIX));
4752
}
4853

4954
@Test
50-
public void testIsFieldAnArrayCount_False(){
51-
assertFalse(LightblueUtil.isFieldAnArrayCount("somearray"));
55+
public void testDoesFieldNameMatchArrayCountPattern_NullValue(){
56+
assertFalse(LightblueUtil.doesFieldNameMatchArrayCountPattern(null));
5257
}
5358

5459
@Test
55-
public void testIsFieldPredefined_ObjectType_True(){
56-
assertTrue(LightblueUtil.isFieldPredefined(LightblueUtil.FIELD_OBJECT_TYPE));
60+
public void testDoesFieldNameMatchArrayCountPattern_False(){
61+
assertFalse(LightblueUtil.doesFieldNameMatchArrayCountPattern("somearray"));
5762
}
5863

5964
@Test
60-
public void testIsFieldPredefined_ObjectType_False(){
61-
assertFalse(LightblueUtil.isFieldPredefined("NOT " + LightblueUtil.FIELD_OBJECT_TYPE));
65+
public void testCreateArrayCountFieldName(){
66+
assertEquals("somearray#", LightblueUtil.createArrayCountFieldName("somearray"));
6267
}
6368

6469
@Test
65-
public void testIsFieldPredefined_Array_True(){
66-
assertTrue(LightblueUtil.isFieldPredefined("somearray" + LightblueUtil.FIELD_MOD_ARRAY_COUNT));
70+
public void testCreateArrayFieldNameFromCountField_NullValue(){
71+
assertNull(LightblueUtil.createArrayFieldNameFromCountField(null));
6772
}
6873

6974
@Test
70-
public void testIsFieldPredefined_Array_False(){
71-
assertFalse(LightblueUtil.isFieldPredefined("somearray"));
75+
public void testCreateArrayFieldNameFromCountField_NotArrayCountField(){
76+
String nonArrayCountFieldName = "notarraycount";
77+
assertEquals(nonArrayCountFieldName, LightblueUtil.createArrayFieldNameFromCountField(nonArrayCountFieldName));
7278
}
7379

7480
@Test
75-
public void testCreateArrayCountFieldName(){
76-
assertEquals("somearray#", LightblueUtil.createArrayCountFieldName("somearray"));
81+
public void testCreateArrayFieldNameFromCountField_ArrayCountField(){
82+
String arrayFieldName = "arrayFieldName";
83+
String arrayCountFieldName = arrayFieldName + LightblueUtil.FIELD_ARRAY_COUNT_POSTFIX;
84+
assertEquals(arrayFieldName, LightblueUtil.createArrayFieldNameFromCountField(arrayCountFieldName));
85+
}
86+
87+
@Test
88+
public void testIsFieldAnArrayCount_DoesNotMatchArrayCountPattern(){
89+
assertFalse(LightblueUtil.isFieldAnArrayCount("someField", null));
90+
}
91+
92+
@Test
93+
public void testIsFieldAnArrayCount_DoesNotHaveMatchingArrayField(){
94+
assertFalse(LightblueUtil.isFieldAnArrayCount("someField" + LightblueUtil.FIELD_ARRAY_COUNT_POSTFIX, new Fields(null)));
95+
}
96+
97+
@Test
98+
public void testIsFieldAnArrayCount_True(){
99+
String arrayFieldName = "arrayField";
100+
101+
Fields fields = new Fields(null);
102+
fields.addNew(new ArrayField(arrayFieldName));
103+
104+
assertTrue(LightblueUtil.isFieldAnArrayCount(arrayFieldName + LightblueUtil.FIELD_ARRAY_COUNT_POSTFIX, fields));
105+
}
106+
107+
@Test
108+
public void testIsFieldAnArrayCount_Exists_ButNotArrayField(){
109+
String arrayFieldName = "arrayField";
110+
111+
Fields fields = new Fields(null);
112+
fields.addNew(new SimpleField(arrayFieldName));
113+
114+
assertFalse(LightblueUtil.isFieldAnArrayCount(arrayFieldName + LightblueUtil.FIELD_ARRAY_COUNT_POSTFIX, fields));
77115
}
78116

79117
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
145145
entry.addAttribute(new Attribute(node.getKey(), values));
146146
}
147147
else{
148-
if(LightblueUtil.isFieldPredefined(node.getKey())){
148+
String fieldName = node.getKey();
149+
if(LightblueUtil.isFieldObjectType(fieldName)
150+
|| LightblueUtil.isFieldAnArrayCount(fieldName, md.getFields())){
149151
/*
150152
* Indicates the field is auto-generated for lightblue purposes. These fields
151153
* should not be inserted into LDAP.
@@ -344,12 +346,12 @@ private Set<String> collectRequiredFields(EntityMetadata md,
344346
if(((projection != null) && projection.isFieldRequiredToEvaluateProjection(node))
345347
|| ((query != null) && query.isRequired(node))
346348
|| ((sort != null) && sort.isRequired(node))) {
347-
if(LightblueUtil.isFieldAnArrayCount(fieldName)){
349+
if(LightblueUtil.isFieldAnArrayCount(fieldName, md.getFields())){
348350
/*
349351
* Handles the case of an array count field, which will not actually exist in
350352
* the ldap entity.
351353
*/
352-
fields.add(fieldName.substring(0, fieldName.length() - 1));
354+
fields.add(LightblueUtil.createArrayFieldNameFromCountField(fieldName));
353355
}
354356
else{
355357
fields.add(fieldName);

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/translator/ResultTranslator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.redhat.lightblue.metadata.EntityMetadata;
3333
import com.redhat.lightblue.metadata.FieldCursor;
3434
import com.redhat.lightblue.metadata.FieldTreeNode;
35+
import com.redhat.lightblue.metadata.Fields;
3536
import com.redhat.lightblue.metadata.ObjectField;
3637
import com.redhat.lightblue.metadata.ReferenceField;
3738
import com.redhat.lightblue.metadata.SimpleArrayElement;
@@ -70,21 +71,22 @@ public List<DocCtx> translate(SearchResult result, EntityMetadata md){
7071
public DocCtx translate(SearchResultEntry entry, EntityMetadata md){
7172
FieldCursor cursor = md.getFieldCursor();
7273
String entityName = md.getEntityInfo().getName();
74+
Fields fields = md.getFields();
7375
if (cursor.firstChild()) {
74-
return new DocCtx(new JsonDoc(toJson(entry, cursor, entityName)));
76+
return new DocCtx(new JsonDoc(toJson(entry, cursor, entityName, fields)));
7577
}
7678

7779
//TODO: What to do in case of a null value here?
7880
return null;
7981
}
8082

81-
private JsonNode toJson(SearchResultEntry entry, FieldCursor fieldCursor, String entityName){
83+
private JsonNode toJson(SearchResultEntry entry, FieldCursor fieldCursor, String entityName, Fields fields){
8284
ObjectNode node = factory.objectNode();
8385

8486
do {
8587
FieldTreeNode field = fieldCursor.getCurrentNode();
8688
String fieldName = field.getName();
87-
if(LightblueUtil.isFieldAnArrayCount(fieldName)){
89+
if(LightblueUtil.isFieldAnArrayCount(fieldName, fields)){
8890
/*
8991
* This case will be handled by the array itself, allowing this to
9092
* process runs the risk of nulling out the correct value.

0 commit comments

Comments
 (0)