use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.
the class AdvancedProcessorImplTest method serialization.
@Test
void serialization() throws IOException, ClassNotFoundException {
final Processor processor = new ProcessorImpl("Root", "Test", "Plugin", new SampleOutput());
final Processor copy = Serializer.roundTrip(processor);
assertNotSame(copy, processor);
assertEquals("Root", copy.rootName());
assertEquals("Test", copy.name());
assertEquals("Plugin", copy.plugin());
}
use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.
the class MycompProcessorTest method map.
@Test
@Ignore("You need to complete this test")
public void map() throws IOException {
// Processor configuration
// Setup your component configuration for the test here
final MycompProcessorConfiguration configuration = new MycompProcessorConfiguration();
// We create the component processor instance using the configuration filled above
final Processor processor = COMPONENT_FACTORY.createProcessor(MycompProcessor.class, configuration);
// The join input factory construct inputs test data for every input branch you have defined for this component
// Make sure to fil in some test data for the branches you want to test
// You can also remove the branches that you don't need from the factory below
final JoinInputFactory joinInputFactory = new JoinInputFactory().withInput("__default__", asList());
// Run the flow and get the outputs
final SimpleComponentRule.Outputs outputs = COMPONENT_FACTORY.collect(processor, joinInputFactory);
// TODO - Test Asserts
// test of the output branches count of the component
assertEquals(1, outputs.size());
// Here you have all your processor output branches
// You can fill in the expected data for every branch to test them
final List<JsonObject> value___default__ = outputs.get(JsonObject.class, "__default__");
assertEquals(asList(), value___default__);
}
use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.
the class TaCoKitGuessSchema method fromOutputEmitterPojo.
public void fromOutputEmitterPojo(final Processor processor, final String outBranchName) {
Object o = processor;
while (Delegated.class.isInstance(o)) {
o = Delegated.class.cast(o).getDelegate();
}
final ClassLoader classLoader = o.getClass().getClassLoader();
final Thread thread = Thread.currentThread();
final ClassLoader old = thread.getContextClassLoader();
thread.setContextClassLoader(classLoader);
try {
final Optional<java.lang.reflect.Type> type = Stream.of(o.getClass().getMethods()).filter(m -> m.isAnnotationPresent(ElementListener.class)).flatMap(m -> IntStream.range(0, m.getParameterCount()).filter(i -> m.getParameters()[i].isAnnotationPresent(Output.class) && outBranchName.equals(m.getParameters()[i].getAnnotation(Output.class).value())).mapToObj(i -> m.getGenericParameterTypes()[i]).filter(t -> ParameterizedType.class.isInstance(t) && ParameterizedType.class.cast(t).getRawType() == OutputEmitter.class && ParameterizedType.class.cast(t).getActualTypeArguments().length == 1).map(p -> ParameterizedType.class.cast(p).getActualTypeArguments()[0])).findFirst();
if (type.isPresent() && Class.class.isInstance(type.get())) {
final Class<?> clazz = Class.class.cast(type.get());
if (clazz != JsonObject.class) {
guessSchemaThroughResultClass(clazz);
}
}
} finally {
thread.setContextClassLoader(old);
}
}
use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.
the class ExecutionResource method write.
/**
* Sends records using a processor instance. Note that the processor should have only an input.
* Behavior for other processors is undefined.
* The input format is a JSON based format where each like is a json record - same as for the symmetric endpoint.
*
* @param family the component family.
* @param component the component name.
* @param chunkSize the bundle size (chunk size).
*/
@POST
@Deprecated
@Consumes("talend/stream")
@Produces(MediaType.APPLICATION_JSON)
@Path("write/{family}/{component}")
public void write(@Suspended final AsyncResponse response, @Context final Providers providers, @PathParam("family") final String family, @PathParam("component") final String component, @QueryParam("group-size") @DefaultValue("50") final long chunkSize, final InputStream stream) {
response.setTimeoutHandler(asyncResponse -> log.warn("Timeout on dataset retrieval"));
response.setTimeout(appConfiguration.datasetRetrieverTimeout(), SECONDS);
executorService.submit(() -> {
Processor processor = null;
final WriteStatistics statistics = new WriteStatistics(0);
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
String line = reader.readLine();
if (line == null || line.trim().isEmpty()) {
response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(ACTION_ERROR, "No configuration sent")).build()));
return;
}
final JsonObject configuration = inlineStreamingMapper.fromJson(line, JsonObject.class);
final Map<String, String> config = convertConfig(configuration);
final Optional<Processor> processorOptional = manager.findProcessor(family, component, getConfigComponentVersion(config), config);
if (!processorOptional.isPresent()) {
response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(COMPONENT_MISSING, "Didn't find the output component")).build()));
return;
}
processor = processorOptional.get();
processor.start();
int groupCount = 0;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
final JsonObject object = inlineStreamingMapper.fromJson(line, JsonObject.class);
if (groupCount == 0) {
processor.beforeGroup();
}
groupCount++;
processor.onNext(name -> {
if (!Branches.DEFAULT_BRANCH.equals(name)) {
throw new IllegalArgumentException("Can't access branch '" + name + "' from component " + family + "#" + name);
}
return inlineStreamingMapper.fromJson(inlineStreamingMapper.toJson(object), JsonObject.class);
}, mockOutputFactory);
statistics.setCount(statistics.getCount() + 1);
if (groupCount == chunkSize) {
processor.afterGroup(mockOutputFactory);
}
}
}
} catch (final Exception e) {
response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(ACTION_ERROR, "Didn't find the input component")).build()));
} finally {
ofNullable(processor).ifPresent(Processor::stop);
}
response.resume(statistics);
});
}
use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.
the class SimpleComponentRuleTest method processorCollector.
@Test
public void processorCollector() {
final Processor processor = COMPONENT_FACTORY.createProcessor(Transform.class, null);
final SimpleComponentRule.Outputs outputs = COMPONENT_FACTORY.collect(processor, new JoinInputFactory().withInput("__default__", asList(new Transform.Record("a"), new Transform.Record("bb"))).withInput("second", asList(new Transform.Record("1"), new Transform.Record("2"))));
assertEquals(2, outputs.size());
assertEquals(asList(2, 3), outputs.get(Integer.class, "size"));
assertEquals(asList("a1", "bb2"), outputs.get(String.class, "value"));
}
Aggregations