/** * Given a {@link MetadataKey}, this is navigated recursively cloning each {@link MetadataKey} of the tree structure creating a * {@link MultilevelMetadataKeyBuilder} and adding the partName of each {@link MetadataKey} found. * * @param key {@link MetadataKey} to be cloned and enriched * @param partOrderMapping {@link Map} that contains the mapping of the name of each part of the {@link MetadataKey} * @param level the current level of the part of the {@link MetadataKey} to be cloned and enriched * @return a {@link MetadataKeyBuilder} with the cloned and enriched keys */ private MetadataKeyBuilder cloneAndEnrichMetadataKey(MetadataKey key, Map<Integer, ParameterModel> partOrderMapping, int level) { final MetadataKeyBuilder keyBuilder = newKey(key.getId(), partOrderMapping.get(level).getName()).withDisplayName(key.getDisplayName()); key.getProperties().forEach(keyBuilder::withProperty); key.getChilds().forEach(childKey -> keyBuilder.withChild(cloneAndEnrichMetadataKey(childKey, partOrderMapping, level + 1))); return keyBuilder; } }
@Override protected boolean matchesSafely(MetadataKey metadataKey) { try { validateEquals(id, metadataKey.getId()); validateEquals(displayName, metadataKey.getDisplayName()); validateEquals(partName, metadataKey.getPartName()); return true; } catch (RuntimeException e) { return false; } }
private Map<String, String> getCurrentParts(MetadataKey key) throws MetadataResolvingException { Map<String, String> keyParts = new HashMap<>(); keyParts.put(key.getPartName(), key.getId()); while (!key.getChilds().isEmpty()) { if (key.getChilds().size() > 1) { final List<String> keyNames = key.getChilds().stream().map(MetadataKey::getId).collect(toList()); throw buildException(format("MetadataKey used for Metadata resolution must only have one child per level. " + "Key '%s' has %s as children.", key.getId(), keyNames)); } key = key.getChilds().iterator().next(); keyParts.put(key.getPartName(), key.getId()); } return keyParts; }
/** * Given {@link MetadataKey}, return the populated key in the Type that the component parameter requires. * * @param key the {@link MetadataKey} associated to the {@link MetadataKeyId} * @return a new instance of the {@link MetadataKeyId} parameter {@code type} with the values of the passed {@link MetadataKey} * @throws MetadataResolvingException if: * <ul> * <li>Parameter types is not instantiable</li> * <li>{@param key} does not provide the required levels</li> * </ul> */ public Object resolve(MetadataKey key) throws MetadataResolvingException { if (isKeyLess()) { return NullMetadataKey.ID; } final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component); MetadataType type = keyIdModelProperty.getType(); KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(key.getId(), getType(type)) { @Override protected Map<Field, String> getFieldValuesMap() throws MetadataResolvingException { return keyToFieldValueMap(key); } }; type.accept(visitor); return visitor.getResultId(); }
protected <T extends ComponentModel> void assertResolvedKey(MetadataResult<ComponentMetadataDescriptor<T>> result, MetadataKey metadataKey) { assertThat(result.get().getMetadataAttributes().getKey().isPresent(), is(true)); MetadataKey resultKey = result.get().getMetadataAttributes().getKey().get(); assertSameKey(metadataKey, resultKey); MetadataKey child = metadataKey.getChilds().stream().findFirst().orElseGet(() -> null); MetadataKey otherChild = resultKey.getChilds().stream().findFirst().orElseGet(() -> null); while (child != null && otherChild != null) { assertSameKey(child, otherChild); child = child.getChilds().stream().findFirst().orElseGet(() -> null); otherChild = otherChild.getChilds().stream().findFirst().orElseGet(() -> null); } assertThat(child == null && otherChild == null, is(true)); }
private Map<String, String> getCurrentParts(MetadataKey key) throws MetadataResolvingException { Map<String, String> keyParts = new HashMap<>(); keyParts.put(key.getPartName(), key.getId()); while (!key.getChilds().isEmpty()) { if (key.getChilds().size() > 1) { final List<String> keyNames = key.getChilds().stream().map(MetadataKey::getId).collect(toList()); throw buildException(format("MetadataKey used for Metadata resolution must only have one child per level. " + "Key '%s' has %s as children.", key.getId(), keyNames)); } key = key.getChilds().iterator().next(); keyParts.put(key.getPartName(), key.getId()); } return keyParts; }
public MetadataResult<TypeMetadataDescriptor> getEntityMetadata(MetadataContext context, MetadataKey entityKey) { try { MetadataType entityMetadata = resolverFactory.getQueryEntityResolver().getEntityMetadata(context, entityKey.getId()); return success(TypeMetadataDescriptor.builder().withType(entityMetadata).build()); } catch (Exception e) { return failure(newFailure(e).onEntity()); } }
@Test public void twoLevelPartialMultilevelKeys() throws Exception { location = Location.builder().globalName("twoLevelPartialMultiLevelKeyResolver").addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> continents = getKeysFromContainer(metadataKeysResult.get()); assertThat(continents, hasSize(1)); Set<MetadataKey> countries = continents.iterator().next().getChilds(); assertThat(countries, hasSize(1)); Set<MetadataKey> cities = countries.iterator().next().getChilds(); assertThat(cities, hasSize(2)); assertThat(cities, hasItem(metadataKeyWithId(BUENOS_AIRES).withPartName(CITY))); assertThat(cities, hasItem(metadataKeyWithId(LA_PLATA).withPartName(CITY))); }
/** * Given a {@link MetadataKey}, this is navigated recursively cloning each {@link MetadataKey} of the tree structure creating a * {@link MultilevelMetadataKeyBuilder} and adding the partName of each {@link MetadataKey} found. * * @param key {@link MetadataKey} to be cloned and enriched * @param partOrderMapping {@link Map} that contains the mapping of the name of each part of the {@link MetadataKey} * @param level the current level of the part of the {@link MetadataKey} to be cloned and enriched * @return a {@link MetadataKeyBuilder} with the cloned and enriched keys */ private MetadataKeyBuilder cloneAndEnrichMetadataKey(MetadataKey key, Map<Integer, ParameterModel> partOrderMapping, int level) { final MetadataKeyBuilder keyBuilder = newKey(key.getId(), partOrderMapping.get(level).getName()).withDisplayName(key.getDisplayName()); key.getProperties().forEach(keyBuilder::withProperty); key.getChilds().forEach(childKey -> keyBuilder.withChild(cloneAndEnrichMetadataKey(childKey, partOrderMapping, level + 1))); return keyBuilder; } }
@Override protected boolean matchesSafely(MetadataKey metadataKey) { try { validateEquals(id, metadataKey.getId()); validateEquals(displayName, metadataKey.getDisplayName()); validateEquals(partName, metadataKey.getPartName()); return true; } catch (RuntimeException e) { return false; } }
@Test public void metadataKeyDefaultValue() throws Exception { location = Location.builder().globalName(METADATA_KEY_DEFAULT_VALUE).addProcessorsPart().addIndexPart(0).build(); MetadataResult<ComponentMetadataDescriptor<OperationModel>> result = metadataService.getOperationMetadata(location); assertSuccessResult(result); MetadataType type = result.get().getModel().getOutput().getType(); assertThat(type, is(instanceOf(ObjectType.class))); ObjectType objectType = (ObjectType) type; assertThat(objectType.getFields(), hasSize(2)); objectType.getFields().forEach(f -> assertThat(f.getKey().getName().getLocalPart(), isOneOf(TIRES, BRAND))); Optional<MetadataKey> metadataKeyOptional = result.get().getMetadataAttributes().getKey(); assertThat(metadataKeyOptional.isPresent(), is(true)); assertThat(metadataKeyOptional.get().getId(), is(CAR)); }
public MetadataResult<MetadataKeysContainer> getEntityKeys(MetadataContext context) { try { QueryEntityResolver queryEntityResolver = resolverFactory.getQueryEntityResolver(); Set<MetadataKey> entityKeys = queryEntityResolver.getEntityKeys(context); final MetadataKeysContainerBuilder keyBuilder = MetadataKeysContainerBuilder.getInstance(); if (entityKeys.stream().anyMatch(key -> key.getChilds().size() > 0)) { return failure(newFailure() .withMessage("Error retrieving entity keys, Only single level keys are supported for entity metadata") .withReason("There are at least one key that contains childs") .withFailureCode(INVALID_METADATA_KEY).onKeys()); } return success(keyBuilder.add(queryEntityResolver.getResolverName(), entityKeys).build()); } catch (Exception e) { return failure(newFailure(e).onKeys()); } }
@Before public void setup() throws Exception { event = CoreEvent.builder(testEvent()).message(of("")).build(); personType = getMetadata(PERSON_METADATA_KEY.getId()); }
protected <T extends ComponentModel> void assertResolvedKey(MetadataResult<ComponentMetadataDescriptor<T>> result, MetadataKey metadataKey) { assertThat(result.get().getMetadataAttributes().getKey().isPresent(), is(true)); MetadataKey resultKey = result.get().getMetadataAttributes().getKey().get(); assertSameKey(metadataKey, resultKey); MetadataKey child = metadataKey.getChilds().stream().findFirst().orElseGet(() -> null); MetadataKey otherChild = resultKey.getChilds().stream().findFirst().orElseGet(() -> null); while (child != null && otherChild != null) { assertSameKey(child, otherChild); child = child.getChilds().stream().findFirst().orElseGet(() -> null); otherChild = otherChild.getChilds().stream().findFirst().orElseGet(() -> null); } assertThat(child == null && otherChild == null, is(true)); }
@Test public void getMetadataKeysForConfig() throws Exception { final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataManager.getMetadataKeys(builder().globalName("apple").build()); assertThat(metadataKeysResult.isSuccess(), is(true)); final Map<String, Set<MetadataKey>> metadataKeys = getKeyMapFromContainer(metadataKeysResult); assertThat(metadataKeys.size(), is(1)); Set<MetadataKey> harvestedKeys = metadataKeys.get("HarvestedKeys"); assertThat(harvestedKeys.size(), is(1)); assertThat(harvestedKeys.iterator().next().getId(), is("HARVESTED")); }
@Test public void twoLevelPartialMultilevelKeys() throws Exception { location = Location.builder().globalName("twoLevelPartialMultiLevelKeyResolver").addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> continents = getKeysFromContainer(metadataKeysResult.get()); assertThat(continents, hasSize(1)); Set<MetadataKey> countries = continents.iterator().next().getChilds(); assertThat(countries, hasSize(1)); Set<MetadataKey> cities = countries.iterator().next().getChilds(); assertThat(cities, hasSize(2)); assertThat(cities, hasItem(metadataKeyWithId(BUENOS_AIRES).withPartName(CITY))); assertThat(cities, hasItem(metadataKeyWithId(LA_PLATA).withPartName(CITY))); }
@Test public void metadataKeyDefaultValue() throws Exception { location = Location.builder().globalName(METADATA_KEY_DEFAULT_VALUE).addProcessorsPart().addIndexPart(0).build(); MetadataResult<ComponentMetadataDescriptor<OperationModel>> result = metadataService.getOperationMetadata(location); assertSuccessResult(result); MetadataType type = result.get().getModel().getOutput().getType(); assertThat(type, is(instanceOf(ObjectType.class))); ObjectType objectType = (ObjectType) type; assertThat(objectType.getFields(), hasSize(2)); objectType.getFields().forEach(f -> assertThat(f.getKey().getName().getLocalPart(), isOneOf(TIRES, BRAND))); Optional<MetadataKey> metadataKeyOptional = result.get().getMetadataAttributes().getKey(); assertThat(metadataKeyOptional.isPresent(), is(true)); assertThat(metadataKeyOptional.get().getId(), is(CAR)); }
public MetadataResult<MetadataKeysContainer> getEntityKeys(MetadataContext context) { try { QueryEntityResolver queryEntityResolver = resolverFactory.getQueryEntityResolver(); Set<MetadataKey> entityKeys = queryEntityResolver.getEntityKeys(context); final MetadataKeysContainerBuilder keyBuilder = MetadataKeysContainerBuilder.getInstance(); if (entityKeys.stream().anyMatch(key -> key.getChilds().size() > 0)) { return failure(newFailure() .withMessage("Error retrieving entity keys, Only single level keys are supported for entity metadata") .withReason("There are at least one key that contains childs") .withFailureCode(INVALID_METADATA_KEY).onKeys()); } return success(keyBuilder.add(queryEntityResolver.getResolverName(), entityKeys).build()); } catch (Exception e) { return failure(newFailure(e).onKeys()); } }