Search in sources :

Example 1 with UnitTestMeta

use of org.apache.servicecomb.core.unittest.UnitTestMeta in project incubator-servicecomb-java-chassis by apache.

the class TestConsumerSchemaFactory method init.

@BeforeClass
public static void init() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
    schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    SchemaLoader schemaLoader = new SchemaLoader() {

        @Override
        public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
        }
    };
    CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
    ReflectUtils.setField(consumerSchemaFactory, "schemaListenerManager", schemaListenerManager);
    ReflectUtils.setField(consumerSchemaFactory, "microserviceMetaManager", microserviceMetaManager);
    ReflectUtils.setField(consumerSchemaFactory, "schemaLoader", schemaLoader);
    ReflectUtils.setField(consumerSchemaFactory, "compositeSwaggerGeneratorContext", compositeSwaggerGeneratorContext);
    SchemaMeta schemaMeta = new UnitTestMeta().getOrCreateSchemaMeta(TestConsumerSchemaFactoryImpl.class);
    String content = UnitTestSwaggerUtils.pretty(schemaMeta.getSwagger());
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceId("0");
    microservice.setServiceName("ms");
    microservice.setVersion("1.0.0");
    microservice.addSchema("schema", content);
    serviceRegistry.getServiceRegistryClient().registerMicroservice(microservice);
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setServiceId("0");
    instance.setInstanceId("0");
    serviceRegistry.getServiceRegistryClient().registerMicroserviceInstance(instance);
}
Also used : MicroserviceMetaManager(org.apache.servicecomb.core.definition.MicroserviceMetaManager) SchemaLoader(org.apache.servicecomb.core.definition.loader.SchemaLoader) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) SchemaListenerManager(org.apache.servicecomb.core.definition.loader.SchemaListenerManager) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) BeforeClass(org.junit.BeforeClass)

Example 2 with UnitTestMeta

use of org.apache.servicecomb.core.unittest.UnitTestMeta in project incubator-servicecomb-java-chassis by apache.

the class TestOperationProtobuf method testOperationProtobuf.

@Test
public void testOperationProtobuf() throws Exception {
    UnitTestMeta meta = new UnitTestMeta();
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(Impl.class);
    OperationMeta operationMeta = schemaMeta.findOperation("test");
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    Assert.assertEquals(operationMeta, operationProtobuf.getOperationMeta());
    Assert.assertEquals(ArgsNotWrapSchema.class, operationProtobuf.getRequestSchema().getClass());
    Assert.assertEquals(NormalWrapSchema.class, operationProtobuf.getResponseSchema().getClass());
    WrapSchema responseSchema = operationProtobuf.findResponseSchema(200);
    Assert.assertEquals(operationProtobuf.getResponseSchema(), responseSchema);
    responseSchema = operationProtobuf.findResponseSchema(300);
    Assert.assertNotNull(responseSchema);
    Assert.assertNotEquals(operationProtobuf.getResponseSchema(), responseSchema);
}
Also used : UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) ArgsNotWrapSchema(org.apache.servicecomb.codec.protobuf.utils.schema.ArgsNotWrapSchema) NormalWrapSchema(org.apache.servicecomb.codec.protobuf.utils.schema.NormalWrapSchema) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 3 with UnitTestMeta

use of org.apache.servicecomb.core.unittest.UnitTestMeta in project incubator-servicecomb-java-chassis by apache.

the class TestCseClientHttpRequest method testNormal.

@Test
public void testNormal() throws IOException {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    UnitTestMeta meta = new UnitTestMeta();
    CseContext.getInstance().getSchemaListenerManager().setSchemaListenerList(Arrays.asList(new RestEngineSchemaListener()));
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(SpringmvcImpl.class);
    CseContext.getInstance().getSchemaListenerManager().notifySchemaListener(schemaMeta);
    Holder<Invocation> holder = new Holder<>();
    CseClientHttpRequest client = new CseClientHttpRequest(URI.create("cse://app:test/" + SpringmvcImpl.class.getSimpleName() + "/bytes"), HttpMethod.POST) {

        /**
         * {@inheritDoc}
         */
        @Override
        protected Response doInvoke(Invocation invocation) {
            holder.value = invocation;
            return Response.ok("result");
        }
    };
    byte[] body = "abc".getBytes();
    client.setRequestBody(body);
    client.execute();
    Assert.assertArrayEquals(body, holder.value.getSwaggerArgument(0));
}
Also used : RestEngineSchemaListener(org.apache.servicecomb.common.rest.RestEngineSchemaListener) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) Invocation(org.apache.servicecomb.core.Invocation) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) Holder(javax.xml.ws.Holder) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Example 4 with UnitTestMeta

use of org.apache.servicecomb.core.unittest.UnitTestMeta in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayServerInvoke method setup.

@Before
public void setup() {
    unitTestMeta = new UnitTestMeta();
    socketAddress = new MockUp<SocketAddress>() {

        @Mock
        public String host() {
            return "127.0.0.1";
        }

        @Mock
        public int port() {
            return 8080;
        }
    }.getMockInstance();
    netSocket = new MockUp<NetSocket>() {

        @Mock
        public SocketAddress remoteAddress() {
            return socketAddress;
        }
    }.getMockInstance();
    connection = new MockUp<TcpConnection>() {

        @Mock
        public void write(ByteBuf data) {
            netSocketBuffer = data;
        }

        @Mock
        public NetSocket getNetSocket() {
            return netSocket;
        }
    }.getMockInstance();
}
Also used : UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) MockUp(mockit.MockUp) ByteBuf(io.netty.buffer.ByteBuf) Before(org.junit.Before)

Example 5 with UnitTestMeta

use of org.apache.servicecomb.core.unittest.UnitTestMeta in project incubator-servicecomb-java-chassis by apache.

the class TestOperationMeta method testOperationMeta.

@Test
public void testOperationMeta() {
    UnitTestMeta meta = new UnitTestMeta();
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(Impl.class);
    OperationMeta operationMeta = schemaMeta.findOperation("test");
    Assert.assertEquals("POST", operationMeta.getHttpMethod());
    Assert.assertEquals("/test", operationMeta.getOperationPath());
    Assert.assertEquals(schemaMeta, operationMeta.getSchemaMeta());
    Assert.assertEquals(Impl.class.getName() + ".test", operationMeta.getSchemaQualifiedName());
    Assert.assertEquals("app:test." + Impl.class.getName() + ".test", operationMeta.getMicroserviceQualifiedName());
    Assert.assertEquals("app:test", operationMeta.getMicroserviceName());
    Assert.assertEquals("test", operationMeta.getOperationId());
    Assert.assertEquals("x", operationMeta.getParamName(0));
    operationMeta.putExtData("ext", 1);
    Assert.assertEquals(1, (int) operationMeta.getExtData("ext"));
    ResponseMeta responseMeta = operationMeta.findResponseMeta(200);
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getJavaType().getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h1").getGenericSignature());
    Assert.assertEquals("Ljava/util/List<Ljava/lang/String;>;", responseMeta.getHeaders().get("h2").getGenericSignature());
    Assert.assertEquals(null, responseMeta.getHeaders().get("h3"));
    responseMeta = operationMeta.findResponseMeta(300);
    Assert.assertEquals("Ljava/lang/String;", responseMeta.getJavaType().getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h1").getGenericSignature());
    Assert.assertEquals("Ljava/util/List<Ljava/lang/String;>;", responseMeta.getHeaders().get("h2").getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h3").getGenericSignature());
}
Also used : UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) ResponseMeta(org.apache.servicecomb.swagger.invocation.response.ResponseMeta) Test(org.junit.Test)

Aggregations

UnitTestMeta (org.apache.servicecomb.core.unittest.UnitTestMeta)5 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)3 Test (org.junit.Test)3 ServiceRegistry (org.apache.servicecomb.serviceregistry.ServiceRegistry)2 ByteBuf (io.netty.buffer.ByteBuf)1 Holder (javax.xml.ws.Holder)1 MockUp (mockit.MockUp)1 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)1 ArgsNotWrapSchema (org.apache.servicecomb.codec.protobuf.utils.schema.ArgsNotWrapSchema)1 NormalWrapSchema (org.apache.servicecomb.codec.protobuf.utils.schema.NormalWrapSchema)1 RestEngineSchemaListener (org.apache.servicecomb.common.rest.RestEngineSchemaListener)1 Invocation (org.apache.servicecomb.core.Invocation)1 MicroserviceMetaManager (org.apache.servicecomb.core.definition.MicroserviceMetaManager)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 SchemaListenerManager (org.apache.servicecomb.core.definition.loader.SchemaListenerManager)1 SchemaLoader (org.apache.servicecomb.core.definition.loader.SchemaLoader)1 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)1 CompositeSwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext)1 ResponseMeta (org.apache.servicecomb.swagger.invocation.response.ResponseMeta)1