Search in sources :

Example 11 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class MetadataTest method testInnerClassType.

/**
 */
@Test
public void testInnerClassType() {
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    TypeDefinition td = builder.build(OuterClass.InnerClass.class, OuterClass.InnerClass.class);
    System.out.println(">> testInnerClassType: " + new Gson().toJson(td));
    Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass", td.getType());
    Assertions.assertEquals(1, td.getProperties().size());
    Assertions.assertNotNull(td.getProperties().get("name"));
    Assertions.assertEquals(DefaultTypeBuilder.class.getName(), td.getTypeBuilderName());
    ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
    System.out.println(">> testInnerClassType: " + new Gson().toJson(sd));
    Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
    Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
    boolean containsType = false;
    for (TypeDefinition type : sd.getTypes()) {
        if (type.getType().equals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass")) {
            containsType = true;
            break;
        }
    }
    Assertions.assertTrue(containsType);
}
Also used : OuterClass(org.apache.dubbo.metadata.definition.common.OuterClass) TestService(org.apache.dubbo.metadata.definition.common.TestService) Gson(com.google.gson.Gson) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) DefaultTypeBuilder(org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Test(org.junit.jupiter.api.Test)

Example 12 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class MetadataUtils method generateMetadata.

/**
 * com.taobao.hsf.metadata.store.MetadataInfoStoreServiceRedis.publishClassInfo(ServiceMetadata) 生成元数据的代码
 */
public static ServiceDefinition generateMetadata(Class<?> interfaceClass) {
    ServiceDefinition sd = new ServiceDefinition();
    sd.setCanonicalName(interfaceClass.getCanonicalName());
    sd.setCodeSource(ClassUtils.getCodeSource(interfaceClass));
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    List<Method> methods = ClassUtils.getPublicNonStaticMethods(interfaceClass);
    for (Method method : methods) {
        MethodDefinition md = new MethodDefinition();
        md.setName(method.getName());
        Class<?>[] paramTypes = method.getParameterTypes();
        Type[] genericParamTypes = method.getGenericParameterTypes();
        String[] parameterTypes = new String[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            try {
                TypeDefinition td = builder.build(genericParamTypes[i], paramTypes[i]);
                parameterTypes[i] = td.getType();
            } catch (Exception e) {
                parameterTypes[i] = paramTypes[i].getName();
            }
        }
        md.setParameterTypes(parameterTypes);
        try {
            TypeDefinition td = builder.build(method.getGenericReturnType(), method.getReturnType());
            md.setReturnType(td.getType());
        } catch (Exception e) {
            md.setReturnType(method.getReturnType().getName());
        }
        sd.getMethods().add(md);
    }
    sd.setTypes(builder.getTypeDefinitions());
    return sd;
}
Also used : Method(java.lang.reflect.Method) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Type(java.lang.reflect.Type) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition)

Example 13 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class FailoverMetadataReportTest method testReadWriteAllMetadataReport.

@Test
public void testReadWriteAllMetadataReport() {
    URL url = mockURL.addParameter("strategy", "all");
    FailoverMetadataReport report = getFailoverReport(url);
    Assertions.assertNotNull(report.getProxyReports(), "metadata reports should not be null.");
    Assertions.assertEquals(2, report.getProxyReports().size(), "expect 2 metadata report, actual " + report.getProxyReports().size());
    MetadataIdentifier identifier = new MetadataIdentifier("helloService", null, null, null, "test");
    ServiceDefinition definition = new ServiceDefinition();
    definition.setCanonicalName("helloService");
    report.storeProviderMetadata(identifier, definition);
    Assertions.assertNotNull(report.getServiceDefinition(identifier));
    // assert all metadata report write already.
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        Assertions.assertNotNull(holder.report.getServiceDefinition(identifier));
    }
    HashMap parameterMap = new HashMap();
    report.storeConsumerMetadata(identifier, parameterMap);
    // assert all metadata report write already.
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        Assertions.assertEquals(parameterMap, ((MockMetadataReport) holder.report).consumerMetadata.get(identifier));
    }
    SubscriberMetadataIdentifier subscribeIdentifier = new SubscriberMetadataIdentifier("test", "1.0");
    MetadataInfo metadataInfo = new MetadataInfo(subscribeIdentifier.getApplication(), subscribeIdentifier.getRevision(), null);
    report.publishAppMetadata(subscribeIdentifier, metadataInfo);
    Assertions.assertEquals(metadataInfo, report.getAppMetadata(subscribeIdentifier, null));
    // assert all metadata report write already.
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        Assertions.assertEquals(metadataInfo, holder.report.getAppMetadata(subscribeIdentifier, null));
    }
    report.registerServiceAppMapping("helloService", "test", null);
    Set<String> appNames = report.getServiceAppMapping("helloService", null, null);
    Assertions.assertEquals(appNames, report.getServiceAppMapping("helloService", null, null));
    // assert all metadata report write already.
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        Assertions.assertEquals(appNames, holder.report.getServiceAppMapping("helloService", null, null));
    }
    ServiceMetadataIdentifier serviceIdentifier = new ServiceMetadataIdentifier("helloService", null, null, null, "1.0", "dubbo");
    report.saveServiceMetadata(serviceIdentifier, url);
    Assertions.assertNotNull(report.getExportedURLs(serviceIdentifier));
    // assert all metadata report write already.
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        Assertions.assertNotNull(holder.report.getExportedURLs(serviceIdentifier));
    }
    report.saveSubscribedData(subscribeIdentifier, new HashSet<>());
    Assertions.assertNotNull(report.getSubscribedURLs(subscribeIdentifier));
    // assert all metadata report write already.
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        Assertions.assertNotNull(holder.report.getSubscribedURLs(subscribeIdentifier));
    }
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) HashMap(java.util.HashMap) URL(org.apache.dubbo.common.URL) ServiceMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier) MetadataIdentifier(org.apache.dubbo.metadata.report.identifier.MetadataIdentifier) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier) ServiceMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) Test(org.junit.jupiter.api.Test)

Example 14 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class ServiceDefinitionBuilderTest method testBuild.

@Test
public void testBuild() {
    ServiceDefinition serviceDefinition = build(processingEnv, getType(TestServiceImpl.class));
    assertEquals(TestServiceImpl.class.getTypeName(), serviceDefinition.getCanonicalName());
    assertEquals("org/apache/dubbo/metadata/tools/TestServiceImpl.class", serviceDefinition.getCodeSource());
    // types
    int i = 0;
    assertEquals("org.apache.dubbo.metadata.tools.TestServiceImpl", serviceDefinition.getTypes().get(i++).getType());
    assertEquals("org.apache.dubbo.metadata.tools.GenericTestService", serviceDefinition.getTypes().get(i++).getType());
    assertEquals("org.apache.dubbo.metadata.tools.DefaultTestService", serviceDefinition.getTypes().get(i++).getType());
    assertEquals("org.apache.dubbo.metadata.tools.TestService", serviceDefinition.getTypes().get(i++).getType());
    assertEquals("java.lang.AutoCloseable", serviceDefinition.getTypes().get(i++).getType());
    assertEquals("java.io.Serializable", serviceDefinition.getTypes().get(i++).getType());
    assertEquals("java.util.EventListener", serviceDefinition.getTypes().get(i++).getType());
    // methods
    assertEquals(14, serviceDefinition.getMethods().size());
}
Also used : ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) TestServiceImpl(org.apache.dubbo.metadata.tools.TestServiceImpl) Test(org.junit.jupiter.api.Test) AbstractAnnotationProcessingTest(org.apache.dubbo.metadata.annotation.processing.AbstractAnnotationProcessingTest)

Aggregations

ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)14 Test (org.junit.jupiter.api.Test)8 Gson (com.google.gson.Gson)5 TypeDefinition (org.apache.dubbo.metadata.definition.model.TypeDefinition)5 TestService (org.apache.dubbo.metadata.definition.common.TestService)4 HashMap (java.util.HashMap)2 URL (org.apache.dubbo.common.URL)2 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)2 MapTypeBuilder (org.apache.dubbo.metadata.definition.builder.MapTypeBuilder)2 FullServiceDefinition (org.apache.dubbo.metadata.definition.model.FullServiceDefinition)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)2 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)2 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1 TypeElement (javax.lang.model.element.TypeElement)1 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)1