Search in sources :

Example 26 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestMicroserviceInstanceHeartbeatTask method testOtherMicroservice.

@Test
public void testOtherMicroservice(@Mocked ServiceRegistryClient srClient, @Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked MicroserviceWatchTask watchTask) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setServiceName("ms1");
    new Expectations() {

        {
            watchTask.getMicroservice();
            result = otherMicroservice;
        }
    };
    MicroserviceInstanceHeartbeatTask heartbeatTask = new MicroserviceInstanceHeartbeatTask(eventBus, srClient, microservice);
    watchTask.taskStatus = TaskStatus.READY;
    eventBus.post(watchTask);
    heartbeatTask.run();
    Assert.assertNull(heartbeatTask.getHeartbeatResult());
    Assert.assertEquals(false, heartbeatTask.isNeedRegisterInstance());
    Assert.assertEquals(0, heartbeatTaskList.size());
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Test(org.junit.Test)

Example 27 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestMicroserviceRegisterTask method testReRegisteredSetForProd.

/**
 * There is schema in service center which is different from local schema.
 */
@Test(expected = IllegalStateException.class)
public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setAppId(microservice.getAppId());
    otherMicroservice.setServiceName("ms1");
    otherMicroservice.addSchema("s1", "");
    List<GetSchemaResponse> list = new ArrayList<>();
    GetSchemaResponse resp = new GetSchemaResponse();
    resp.setSchemaId("s1");
    resp.setSummary("c1188d709631a9038874f9efc6eb894f");
    list.add(resp);
    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
    onlineSchemasHolder.setValue(list).setStatusCode(200);
    new Expectations() {

        {
            srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
            result = "serviceId";
            srClient.getMicroservice(anyString);
            result = otherMicroservice;
            srClient.getSchemas(anyString);
            result = onlineSchemasHolder;
        }
    };
    microservice.addSchema("s1", "");
    microservice.setEnvironment("prod");
    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
    registerTask.run();
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Holder(org.apache.servicecomb.serviceregistry.client.http.Holder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GetSchemaResponse(org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse) Test(org.junit.Test)

Example 28 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestMicroserviceRegisterTask method testLocalSchemaAndServiceCenterSchemaDiff.

/**
 * There is schema in service center which is different from local schema.
 */
@Test
public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked ServiceRegistryClient srClient) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setAppId(microservice.getAppId());
    otherMicroservice.setServiceName("ms1");
    otherMicroservice.addSchema("s1", "abcd");
    List<GetSchemaResponse> list = new ArrayList<>();
    GetSchemaResponse resp = new GetSchemaResponse();
    resp.setSchemaId("s1");
    resp.setSummary("c1188d709631a9038874f9efc6eb894f");
    list.add(resp);
    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
    onlineSchemasHolder.setValue(list).setStatusCode(200);
    new Expectations() {

        {
            srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
            result = "serviceId";
            srClient.getMicroservice(anyString);
            result = otherMicroservice;
            srClient.getSchemas(anyString);
            result = onlineSchemasHolder;
            srClient.getSchema(anyString, anyString);
            result = "swagger: \"2.0\"\n" + "info:\n" + "  version: \"1.0.0\"\n" + "  title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" + "  x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" + "basePath: \"/clientreqtimeout\"\n" + "consumes:\n" + "- \"application/json\"\n" + "produces:\n" + "- \"application/json\"\n" + "paths:\n" + "  /sayhello:\n" + "    post:\n" + "      operationId: \"sayHello\"\n" + "      parameters:\n" + "      - in: \"body\"\n" + "        name: \"student\"\n" + "        required: false\n" + "        schema:\n" + "          $ref: \"#/definitions/Student\"\n" + "      responses:\n" + "        200:\n" + "          description: \"response of 200\"\n" + "          schema:\n" + "            $ref: \"#/definitions/Student\"\n" + "definitions:\n" + "  Student:\n" + "    type: \"object\"\n" + "    required:\n" + "    - \"name\"\n" + "    properties:\n" + "      name:\n" + "        type: \"string\"\n" + "      age:\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        maximum: 20\n" + "    x-java-class: \"org.apache.servicecomb.demo.validator.Student\"";
        }
    };
    microservice.addSchema("s1", "swagger: \"2.0\"\n" + "info:\n" + "  version: \"1.0.0\"\n" + "  title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" + "  x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" + "basePath: \"/clientreqtimeout\"\n" + "consumes:\n" + "- \"application/json\"\n" + "produces:\n" + "- \"application/json\"\n" + "paths:\n" + "  /sayhello:\n" + "    post:\n" + "      operationId: \"sayHello\"\n" + "      parameters:\n" + "      - in: \"body\"\n" + "        name: \"student\"\n" + "        required: false\n" + "        schema:\n" + "          $ref: \"#/definitions/Student\"\n" + "      responses:\n" + "        200:\n" + "          description: \"response of 200\"\n" + "          schema:\n" + "            type: \"string\"\n" + "definitions:\n" + "  Student:\n" + "    type: \"object\"\n" + "    required:\n" + "    - \"name\"\n" + "    properties:\n" + "      name:\n" + "        type: \"string\"\n" + "      age:\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        maximum: 20\n" + "    x-java-class: \"org.apache.servicecomb.demo.validator.Student\"");
    microservice.setEnvironment("prod");
    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
    Boolean isIlleagalException = false;
    try {
        registerTask.run();
    } catch (IllegalStateException exception) {
        isIlleagalException = true;
        List<LoggingEvent> events = collector.getEvents().stream().filter(e -> {
            return MicroserviceRegisterTask.class.getName().equals(e.getLoggerName());
        }).collect(Collectors.toList());
        Assert.assertEquals("service center schema and local schema both are different:\n" + " service center schema:\n" + "[swagger: \"2.0\"\n" + "info:\n" + "  version: \"1.0.0\"\n" + "  title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" + "  x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" + "basePath: \"/clientreqtimeout\"\n" + "consumes:\n" + "- \"application/json\"\n" + "produces:\n" + "- \"application/json\"\n" + "paths:\n" + "  /sayhello:\n" + "    post:\n" + "      operationId: \"sayHello\"\n" + "      parameters:\n" + "      - in: \"body\"\n" + "        name: \"student\"\n" + "        required: false\n" + "        schema:\n" + "          $ref: \"#/definitions/Student\"\n" + "      responses:\n" + "        200:\n" + "          description: \"response of 200\"\n" + "          schema:\n" + "            $ref: \"#/definitions/Student\"\n" + "definitions:\n" + "  Student:\n" + "    type: \"object\"\n" + "    required:\n" + "    - \"name\"\n" + "    properties:\n" + "      name:\n" + "        type: \"string\"\n" + "      age:\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        maximum: 20\n" + "    x-java-class: \"org.apache.servicecomb.demo.validator.Student\"\n" + " local schema:\n" + "[swagger: \"2.0\"\n" + "info:\n" + "  version: \"1.0.0\"\n" + "  title: \"swagger definition for org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" + "  x-java-interface: \"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" + "basePath: \"/clientreqtimeout\"\n" + "consumes:\n" + "- \"application/json\"\n" + "produces:\n" + "- \"application/json\"\n" + "paths:\n" + "  /sayhello:\n" + "    post:\n" + "      operationId: \"sayHello\"\n" + "      parameters:\n" + "      - in: \"body\"\n" + "        name: \"student\"\n" + "        required: false\n" + "        schema:\n" + "          $ref: \"#/definitions/Student\"\n" + "      responses:\n" + "        200:\n" + "          description: \"response of 200\"\n" + "          schema:\n" + "            type: \"string\"\n" + "definitions:\n" + "  Student:\n" + "    type: \"object\"\n" + "    required:\n" + "    - \"name\"\n" + "    properties:\n" + "      name:\n" + "        type: \"string\"\n" + "      age:\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        maximum: 20\n" + "    x-java-class: \"org.apache.servicecomb.demo.validator.Student\"]", events.get(4).getMessage());
        Assert.assertEquals("The difference in local schema:\n" + "[type: \"string\"\n" + "definitions:\n" + "  Student:\n" + "    type: \"object\"\n" + "    required:\n" + "    - \"name\"\n" + "    properties:\n" + "      name:\n" + "        type: \"string\"\n" + "      age:\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        maximum: 20\n" + "    x-java-class: \"org.apache.servicecomb.demo.validator.Student\"]", events.get(5).getMessage());
    }
    Assert.assertEquals(true, isIlleagalException);
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Holder(org.apache.servicecomb.serviceregistry.client.http.Holder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GetSchemaResponse(org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse) Test(org.junit.Test)

Example 29 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestMicroserviceRegisterTask method testAlreadyRegisteredSchemaIdSetNotMatch.

@Test(expected = IllegalStateException.class)
public void testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClient srClient) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setAppId(microservice.getAppId());
    otherMicroservice.setServiceName("ms1");
    otherMicroservice.addSchema("s1", "");
    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
    onlineSchemasHolder.setStatusCode(200);
    ArrayList<GetSchemaResponse> schemaResponseList = new ArrayList<>();
    onlineSchemasHolder.setValue(schemaResponseList);
    GetSchemaResponse schemaResponse = new GetSchemaResponse();
    schemaResponseList.add(schemaResponse);
    schemaResponse.setSchemaId("s1");
    new Expectations() {

        {
            srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
            result = "serviceId";
            srClient.getMicroservice(anyString);
            result = otherMicroservice;
            srClient.getSchemas("serviceId");
            result = onlineSchemasHolder;
        }
    };
    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
    registerTask.run();
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Holder(org.apache.servicecomb.serviceregistry.client.http.Holder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GetSchemaResponse(org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse) Test(org.junit.Test)

Example 30 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestMicroserviceRegisterTask method testReRegisterForProductAndLocalSchemasAreLess.

/**
 * env = production and there are schemas only existing in service center
 */
@Test(expected = IllegalStateException.class)
public void testReRegisterForProductAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setAppId(microservice.getAppId());
    otherMicroservice.setServiceName("ms1");
    otherMicroservice.addSchema("s1", null);
    otherMicroservice.addSchema("s2", null);
    List<GetSchemaResponse> list = new ArrayList<>();
    GetSchemaResponse resp = new GetSchemaResponse();
    resp.setSchemaId("s1");
    list.add(resp);
    resp = new GetSchemaResponse();
    resp.setSchemaId("s2");
    list.add(resp);
    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
    onlineSchemasHolder.setValue(list).setStatusCode(200);
    new Expectations() {

        {
            srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
            result = "serviceId";
            srClient.getMicroservice(anyString);
            result = otherMicroservice;
            srClient.getSchemas(anyString);
            result = onlineSchemasHolder;
            srClient.getSchema("serviceId", "s1");
            result = null;
            srClient.registerSchema("serviceId", "s1", "s1Content");
            result = true;
        }
    };
    microservice.addSchema("s1", "s1Content");
    microservice.setEnvironment("production");
    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
    registerTask.run();
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Holder(org.apache.servicecomb.serviceregistry.client.http.Holder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GetSchemaResponse(org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse) Test(org.junit.Test)

Aggregations

Microservice (org.apache.servicecomb.registry.api.registry.Microservice)86 Test (org.junit.Test)53 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)20 Expectations (mockit.Expectations)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 GetSchemaResponse (org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse)10 Holder (org.apache.servicecomb.serviceregistry.client.http.Holder)10 HashMap (java.util.HashMap)9 MicroserviceFactory (org.apache.servicecomb.registry.api.registry.MicroserviceFactory)9 Swagger (io.swagger.models.Swagger)8 Before (org.junit.Before)8 MockUp (mockit.MockUp)6 Configuration (org.apache.commons.configuration.Configuration)6 AccessController (org.apache.servicecomb.authentication.provider.AccessController)6 Subscribe (com.google.common.eventbus.Subscribe)5 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)5 EventBus (com.google.common.eventbus.EventBus)4 Version (org.apache.servicecomb.foundation.common.Version)4 HashSet (java.util.HashSet)3