Search in sources :

Example 1 with ReflectionService

use of org.talend.sdk.component.runtime.manager.reflect.ReflectionService in project component-runtime by Talend.

the class ReflectionServiceTest method copiable.

@Test
void copiable() throws NoSuchMethodException {
    final HashMap<Class<?>, Object> precomputed = new HashMap<>();
    precomputed.put(UserHttpClient.class, new HttpClientFactoryImpl("test", new ReflectionService(new ParameterModelService()), JsonbBuilder.create(), emptyMap()).create(UserHttpClient.class, "http://foo"));
    final Method httpMtd = TableOwner.class.getMethod("http", UserHttpClient.class);
    final HttpClient client1 = HttpClient.class.cast(reflectionService.parameterFactory(httpMtd, precomputed).apply(emptyMap())[0]);
    final HttpClient client2 = HttpClient.class.cast(reflectionService.parameterFactory(httpMtd, precomputed).apply(emptyMap())[0]);
    assertNotSame(client1, client2);
    final InvocationHandler handler1 = Proxy.getInvocationHandler(client1);
    final InvocationHandler handler2 = Proxy.getInvocationHandler(client2);
    assertNotSame(handler1, handler2);
    assertEquals(handler1.toString(), handler2.toString());
}
Also used : HashMap(java.util.HashMap) HttpClient(org.talend.sdk.component.api.service.http.HttpClient) HttpClientFactoryImpl(org.talend.sdk.component.runtime.manager.service.HttpClientFactoryImpl) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.jupiter.api.Test)

Example 2 with ReflectionService

use of org.talend.sdk.component.runtime.manager.reflect.ReflectionService in project component-runtime by Talend.

the class ReflectionServiceTest method tables.

@Test
void tables() throws NoSuchMethodException {
    final Method factory = TableOwner.class.getMethod("factory", TableOwner.class);
    final Object[] tests = new ReflectionService(new ParameterModelService()).parameterFactory(factory, emptyMap()).apply(new HashMap<String, String>() {

        {
            put("root.table[0].value1", "test1");
            put("root.table[0].value2", "12");
            put("root.table[1].value1", "test2");
            put("root.table[1].value2", "22");
            put("root.table[1].nestedList[0].value1", "nested");
            put("root.table[1].nestedList[0].value2", "1");
            put("root.map.key[0]", "test1k");
            put("root.map.value[0].value1", "test1v");
            put("root.map.key[1]", "test2k");
            put("root.map.value[1].value1", "test2v");
        }
    });
    assertEquals(1, tests.length);
    assertTrue(TableOwner.class.isInstance(tests[0]));
    final TableOwner tableOwner = TableOwner.class.cast(tests[0]);
    {
        assertNotNull(tableOwner.table);
        assertEquals(2, tableOwner.table.size());
        assertEquals(Stream.of("test1", "test2").collect(toList()), tableOwner.table.stream().map(Column::getValue1).collect(toList()));
        assertArrayEquals(IntStream.of(12, 22).toArray(), tableOwner.table.stream().mapToInt(Column::getValue2).toArray());
        assertNotNull(tableOwner.table.get(1).nestedList);
        assertEquals(1, tableOwner.table.get(1).nestedList.size());
        assertEquals("nested", tableOwner.table.get(1).nestedList.get(0).value1);
        assertEquals(1, tableOwner.table.get(1).nestedList.get(0).value2);
    }
    {
        assertNotNull(tableOwner.map);
        assertEquals(2, tableOwner.map.size());
        assertEquals("test1v", tableOwner.map.get("test1k").value1);
        assertEquals("test2v", tableOwner.map.get("test2k").value1);
    }
}
Also used : Method(java.lang.reflect.Method) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Test(org.junit.jupiter.api.Test)

Example 3 with ReflectionService

use of org.talend.sdk.component.runtime.manager.reflect.ReflectionService in project component-runtime by Talend.

the class HttpClientFactoryImplTest method decoderWithServices.

@Test
void decoderWithServices() throws IOException {
    final HttpServer server = createTestServer(HttpURLConnection.HTTP_OK);
    try {
        server.start();
        final DecoderWithService client = new HttpClientFactoryImpl("test", new ReflectionService(new ParameterModelService()), JsonbBuilder.create(), new HashMap<Class<?>, Object>() {

            {
                put(MyService.class, new MyService());
                put(MyI18nService.class, (MyI18nService) () -> "error from i18n service");
            }
        }).create(DecoderWithService.class, null);
        client.base("http://localhost:" + server.getAddress().getPort() + "/api");
        assertThrows(IllegalStateException.class, () -> client.error("search yes"));
        assertEquals(MyService.class.getCanonicalName(), client.ok().value);
    } finally {
        server.stop(0);
    }
}
Also used : HashMap(java.util.HashMap) HttpServer(com.sun.net.httpserver.HttpServer) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Test(org.junit.jupiter.api.Test)

Example 4 with ReflectionService

use of org.talend.sdk.component.runtime.manager.reflect.ReflectionService in project component-runtime by Talend.

the class RepositoryModelBuilderTest method notRootConfig.

@Test
void notRootConfig() {
    final RepositoryModel model = new RepositoryModelBuilder().create(new ComponentManager.AllServices(emptyMap()), singleton(new ComponentFamilyMeta("test", emptyList(), "noicon", "test", "test") {

        {
            final ParameterMeta store = new ParameterMeta(null, DataStore1.class, ParameterMeta.Type.OBJECT, "config.store", "store", new String[0], emptyList(), emptyList(), new HashMap<String, String>() {

                {
                    put("tcomp::configurationtype::type", "datastore");
                    put("tcomp::configurationtype::name", "testDatastore");
                }
            });
            final ParameterMeta wrapper = new ParameterMeta(null, WrappingStore.class, ParameterMeta.Type.OBJECT, "config", "config", new String[0], singletonList(store), emptyList(), emptyMap());
            getPartitionMappers().put("test", new PartitionMapperMeta(this, "mapper", "noicon", 1, PartitionMapper1.class, singletonList(wrapper), m -> null, (a, b) -> null, true) {
            });
        }
    }), new MigrationHandlerFactory(new ReflectionService(new ParameterModelService())));
    final List<Config> configs = model.getFamilies().stream().flatMap(f -> f.getConfigs().stream()).collect(toList());
    assertEquals(1, configs.size());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) HashMap(java.util.HashMap) ClassReader(org.apache.xbean.asm6.ClassReader) Collections.singletonList(java.util.Collections.singletonList) DataStore1(org.talend.test.DataStore1) JarEntry(java.util.jar.JarEntry) Collections.singleton(java.util.Collections.singleton) ClassRemapper(org.apache.xbean.asm6.commons.ClassRemapper) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EXPAND_FRAMES(org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES) JarOutputStream(java.util.jar.JarOutputStream) Remapper(org.apache.xbean.asm6.commons.Remapper) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) Collections.emptyMap(java.util.Collections.emptyMap) COMPUTE_FRAMES(org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES) Container(org.talend.sdk.component.container.Container) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) FileOutputStream(java.io.FileOutputStream) WrappingStore(org.talend.test.WrappingStore) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IdGenerator(org.talend.sdk.component.runtime.manager.util.IdGenerator) File(java.io.File) TestInfo(org.junit.jupiter.api.TestInfo) RepositoryModel(org.talend.sdk.component.design.extension.RepositoryModel) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) ClassWriter(org.apache.xbean.asm6.ClassWriter) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) MigrationHandlerFactory(org.talend.sdk.component.runtime.manager.reflect.MigrationHandlerFactory) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) InputStream(java.io.InputStream) PartitionMapper1(org.talend.test.PartitionMapper1) RepositoryModel(org.talend.sdk.component.design.extension.RepositoryModel) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) PartitionMapper1(org.talend.test.PartitionMapper1) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) MigrationHandlerFactory(org.talend.sdk.component.runtime.manager.reflect.MigrationHandlerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 ParameterModelService (org.talend.sdk.component.runtime.manager.reflect.ParameterModelService)4 ReflectionService (org.talend.sdk.component.runtime.manager.reflect.ReflectionService)4 HashMap (java.util.HashMap)3 Method (java.lang.reflect.Method)2 HttpServer (com.sun.net.httpserver.HttpServer)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singleton (java.util.Collections.singleton)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Optional.ofNullable (java.util.Optional.ofNullable)1 JarEntry (java.util.jar.JarEntry)1 JarOutputStream (java.util.jar.JarOutputStream)1