@Override public void onStart(SourceCallback<InputStream, Void> sourceCallback) throws MuleException { if (nextOperationReference != null) { Component proc = locator.find(builderFromStringRepresentation(location.getParts().get(0).getPartPath()).addProcessorsPart() .addIndexPart(0).build()) .orElseThrow(() -> new IllegalArgumentException("Missing processor after this source")); Map<String, Object> parameters = (Map<String, Object>) proc.getAnnotation(ANNOTATION_PARAMETERS); if (!parameters.get("name").equals(nextOperationReference)) { throw new IllegalArgumentException(String.format("Next processor %s does not match the expected operation %s", parameters.get("name"), nextOperationReference)); } } scheduler = schedulerService.cpuLightScheduler(); sourceCallbackHandleTask = scheduler.scheduleAtFixedRate(() -> sourceCallback.handle(Result.<InputStream, Void>builder() .output(new ByteArrayInputStream(getSpellBytes(spell))) .build()), 0, castFrequencyInMillis, MILLISECONDS); }
/** * Test template that sets an "invalid" classloader in TCCL different from the one that was used to register the extension and * asserts that, it sets back the original classloader to TCCL. Done in this way due to it is not possible to change extension * model classloader property once it is registered. */ private void resolveTestWithContextClassLoader(String flowName, Callback<MetadataOperationTestCase> doAction) throws Exception { location = Location.builder().globalName(flowName).addProcessorsPart().addIndexPart(0).build(); TestThreadContextClassLoaderResolver.reset(); final ClassLoader originalClassLoader = org.mule.test.metadata.extension.MetadataConnection.class.getClassLoader(); withContextClassLoader(mock(ClassLoader.class), () -> { doAction.execute(MetadataOperationTestCase.this); assertThat(TestThreadContextClassLoaderResolver.getCurrentState(), is(sameInstance(originalClassLoader))); }); }
Set<Value> getValuesFromConnection(String configName, String parameterName) throws Exception { return checkResultAndRetrieveValues(valueProviderService .getValues(Location.builder().globalName(configName).addConnectionPart().build(), parameterName)); }
@Test public void operationReceivesExclusiveOptionalParameterGroup() throws Exception { location = Location.builder().globalName("inputHasExclusiveOptionalParameterGroup").addProcessorsPart().addIndexPart(0).build(); MetadataResult<ComponentMetadataDescriptor<OperationModel>> operationMetadata = metadataService.getOperationMetadata(location); ParameterGroupModel dessert = getParameterGroup(operationMetadata.get().getModel(), "dessert"); assertThat(dessert.getName(), is("dessert")); }
@Test public void operationWhichReturnsDynamicListOfMessages() throws Exception { location = Location.builder().globalName("dynamicListOfMessages").addProcessorsPart().addIndexPart(0).build(); MetadataType param = getResolvedTypeFromList(); assertMessageType(((ArrayType) param).getType(), personType, TYPE_BUILDER.anyType().build()); }
@Test public void pagedOperationMetadataTestCase() throws Exception { location = Location.builder().globalName(PAGED_OPERATION_METADATA).addProcessorsPart().addIndexPart(0).build(); ComponentMetadataDescriptor metadataDescriptor = getSuccessComponentDynamicMetadata(NULL_METADATA_KEY); final ComponentModel typedModel = metadataDescriptor.getModel(); assertExpectedType(getParameter(typedModel, "animal"), Animal.class); }
@Test public void resolverContentWithContextClassLoader() throws Exception { location = Location.builder().globalName(OUTPUT_METADATA_WITH_KEY_ID).addProcessorsPart().addIndexPart(0).build(); resolveTestWithContextClassLoader(RESOLVER_CONTENT_WITH_CONTEXT_CLASSLOADER, MetadataExtensionFunctionalTestCase::getSuccessComponentDynamicMetadata); }
@Test public void messageAttributesStringTypeMetadata() throws Exception { location = Location.builder().globalName(MESSAGE_ATTRIBUTES_PERSON_TYPE_METADATA).addProcessorsPart().addIndexPart(0).build(); final ComponentMetadataDescriptor<OperationModel> metadataDescriptor = getSuccessComponentDynamicMetadataWithKey(PERSON_METADATA_KEY); final OperationModel typedModel = metadataDescriptor.getModel(); assertExpectedOutput(typedModel, personType, StringAttributes.class); assertExpectedType(getParameter(typedModel, "type"), String.class); }
@Test public void dynamicContentWithoutOutput() throws Exception { // Resolver for content and output type, no return type, resolves only @Content, with key and KeyId location = Location.builder().globalName(CONTENT_ONLY_IGNORES_OUTPUT).addProcessorsPart().addIndexPart(0).build(); final ComponentMetadataDescriptor<OperationModel> metadataDescriptor = getSuccessComponentDynamicMetadataWithKey(PERSON_METADATA_KEY); final OperationModel typedModel = metadataDescriptor.getModel(); assertExpectedOutput(typedModel, void.class, void.class); assertExpectedType(getParameter(typedModel, "type"), String.class); assertExpectedType(getParameter(typedModel, "content"), personType, true); }
@Test public void dynamicOperationMetadata() throws Exception { location = Location.builder().globalName(CONTENT_AND_OUTPUT_METADATA_WITH_KEY_ID).addProcessorsPart().addIndexPart(0).build(); final ComponentMetadataDescriptor<OperationModel> metadataDescriptor = getSuccessComponentDynamicMetadataWithKey(PERSON_METADATA_KEY); final OperationModel typedModel = metadataDescriptor.getModel(); assertExpectedType(getParameter(typedModel, "type"), String.class); assertExpectedType(getParameter(typedModel, "content"), personType, true); }
@Test public void emptyPartialMultilevelKeys() throws Exception { location = Location.builder().globalName("emptyPartialMultilevelKeys").addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> continents = getKeysFromContainer(metadataKeysResult.get()); assertThat(continents, hasSize(2)); assertThat(continents, hasItem(metadataKeyWithId(AMERICA).withDisplayName(AMERICA).withPartName(CONTINENT))); assertThat(continents, hasItem(metadataKeyWithId(EUROPE).withDisplayName(EUROPE).withPartName(CONTINENT))); }
@Test public void getMultilevelKeys() throws Exception { location = Location.builder().globalName(SIMPLE_MULTILEVEL_KEY_RESOLVER).addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> continents = getKeysFromContainer(metadataKeysResult.get()); assertThat(continents, hasSize(2)); assertThat(continents, hasItem(metadataKeyWithId(AMERICA).withDisplayName(AMERICA).withPartName(CONTINENT))); assertThat(continents, hasItem(metadataKeyWithId(EUROPE).withDisplayName(EUROPE).withPartName(CONTINENT))); }
@Test public void shouldInheritOperationResolvers() throws Exception { location = Location.builder().globalName(SHOULD_INHERIT_OPERATION_PARENT_RESOLVERS).addProcessorsPart().addIndexPart(0).build(); final MetadataResult<MetadataKeysContainer> metadataKeysResult = metadataService.getMetadataKeys(location); assertSuccessResult(metadataKeysResult); final Set<MetadataKey> metadataKeys = getKeysFromContainer(metadataKeysResult.get()); assertThat(metadataKeys.size(), is(3)); assertThat(metadataKeys, hasItems(metadataKeyWithId(PERSON), metadataKeyWithId(CAR), metadataKeyWithId(HOUSE))); final ComponentMetadataDescriptor<OperationModel> metadataDescriptor = getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); final OperationModel typedModel = metadataDescriptor.getModel(); assertExpectedOutput(typedModel, personType, void.class); assertExpectedType(getParameter(typedModel, "type"), String.class); // TODO MULE-14190: Revamp MetadataScope annotation // assertExpectedType(getParameter(typedModel, "content"), personType, true); }
@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))); }
public ValueResult getValueResultFromConnection(String configName, String parameterName) { return valueProviderService .getValues(Location.builder().globalName(configName).addConnectionPart().build(), parameterName); }
@Test public void retrieveKeysFromEnumMetadataKey() { location = Location.builder().globalName(ENUM_METADATA_KEY).addProcessorsPart().addIndexPart(0).build(); MetadataResult<MetadataKeysContainer> result = metadataService.getMetadataKeys(location); assertSuccessResult(result); String enumMetadataResolver = "EnumMetadataResolver"; assertThat(result.get().getCategories(), contains(enumMetadataResolver)); Set<MetadataKey> metadataKeys = result.get().getKeys(enumMetadataResolver).get(); MetadataKeyMatcher[] metadataKeyMatchers = Stream.of(AnimalClade.values()) .map(Object::toString) .map(MetadataKeyMatcher::metadataKeyWithId) .toArray(MetadataKeyMatcher[]::new); assertThat(metadataKeys, hasItems(metadataKeyMatchers)); }
@Test public void resolveMetadataWithNoRefDynamicConfig() throws Exception { location = builder().globalName(RESOLVER_WITH_IMPLICIT_DYNAMIC_CONFIG).addProcessorsPart().addIndexPart(0).build(); MetadataKey key = newKey(AMERICA).withChild(newKey(USA).withChild(newKey(SAN_FRANCISCO))).build(); final MetadataResult<ComponentMetadataDescriptor<OperationModel>> metadataResult = getComponentDynamicMetadata(key); assertFailureResult(metadataResult, 1); assertMetadataFailure(metadataResult.getFailures().get(0), "Configuration used for Metadata fetch cannot be dynamic", INVALID_CONFIGURATION, MetadataResolvingException.class.getName(), COMPONENT, ""); } }
@Test public void resolveMetadataWithImplicitDynamicConfig() throws Exception { location = builder().globalName(CONTENT_METADATA_WITH_KEY_ID).addProcessorsPart().addIndexPart(0).build(); final MetadataResult<ComponentMetadataDescriptor<OperationModel>> result = getComponentDynamicMetadata(PERSON_METADATA_KEY); assertFailureResult(result, 1); assertMetadataFailure(result.getFailures().get(0), "Configuration used for Metadata fetch cannot be dynamic", INVALID_CONFIGURATION, MetadataResolvingException.class.getName(), COMPONENT, ""); } }
@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)); }
protected void setLocation(String outputAndMetadataKeyCacheResolver) { location = Location.builder().globalName(outputAndMetadataKeyCacheResolver).addProcessorsPart().addIndexPart(0).build(); }