@Test public void shouldNotExtractNestedKeyFieldWhenParentFieldIsNotANaturalKey() throws NoNaturalKeysDefinedException { Entity e = setup(); Map<String, String> parentValue = new HashMap<String, String>(); parentValue.put("childField", "someNestedValue"); e.getBody().put("parentField", parentValue); AppInfo mockAppInfo = Mockito.mock(AppInfo.class); Mockito.when(mockAppInfo.applyNaturalKeys()).thenReturn(true); Mockito.when(mockAppInfo.getType()).thenReturn(AnnotationType.APPINFO); mockSchema.addAnnotation(mockAppInfo); ComplexSchema parentFieldSchema = new ComplexSchema(); mockSchema.addField("parentField", parentFieldSchema); AppInfo mockAppInfoForParent = Mockito.mock(AppInfo.class); Mockito.when(mockAppInfoForParent.isNaturalKey()).thenReturn(false); Mockito.when(mockAppInfoForParent.getType()).thenReturn(AnnotationType.APPINFO); parentFieldSchema.addAnnotation(mockAppInfoForParent); NeutralSchema mockChildFieldSchema = Mockito.mock(NeutralSchema.class); AppInfo mockFieldAppInfo = Mockito.mock(AppInfo.class); Mockito.when(mockChildFieldSchema.getAppInfo()).thenReturn(mockFieldAppInfo); Mockito.when(mockFieldAppInfo.isNaturalKey()).thenReturn(true); Mockito.when(mockFieldAppInfo.isRequired()).thenReturn(false); parentFieldSchema.addField("childField", mockChildFieldSchema); Map<String, String> naturalKeys = naturalKeyExtractor.getNaturalKeys(e); Assert.assertEquals(1, naturalKeys.size()); Assert.assertEquals("someValue", naturalKeys.get("someField")); Assert.assertNull("The nested field should not be extracted", naturalKeys.get("parentField.childField")); Mockito.verify(entitySchemaRegistry, Mockito.times(1)).getSchema(Mockito.anyString()); }
@Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; } }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }  }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }  @Override Map<String, String> getNaturalKeys(Entity entity); @Override Map<String, Boolean> getNaturalKeyFields(Entity entity); @Override NaturalKeyDescriptor getNaturalKeyDescriptor(Entity entity); String getCollectionName(Entity entity); }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }  @Override Map<String, String> getNaturalKeys(Entity entity); @Override Map<String, Boolean> getNaturalKeyFields(Entity entity); @Override NaturalKeyDescriptor getNaturalKeyDescriptor(Entity entity); String getCollectionName(Entity entity);  }