Search in sources :

Example 31 with Microservice

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

the class TestMicroserviceRegisterTask method testLocalSchemaAndServiceCenterSchemaNoDiff.

@Test
public void testLocalSchemaAndServiceCenterSchemaNoDiff(@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" + "            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.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" + "    properties:\n" + "      name:\n" + "        type: \"string\"\n" + "      age:\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        maximum: 20\n" + "    type: \"object\"\n" + "    required:\n" + "    - \"name\"\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;
    }
    Assert.assertEquals(false, 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 32 with Microservice

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

the class TestMicroserviceRegisterTask method testFirstRegisterForProd.

/**
 * There is microservice information but no schema in service center.
 */
@Test
public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setAppId(microservice.getAppId());
    otherMicroservice.setServiceName("ms1");
    otherMicroservice.addSchema("s1", null);
    List<GetSchemaResponse> list = new ArrayList<>();
    GetSchemaResponse resp = new GetSchemaResponse();
    resp.setSchemaId("s1");
    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();
    Assert.assertEquals(true, registerTask.isRegistered());
    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
    Assert.assertEquals("serviceId", microservice.getServiceId());
    Assert.assertEquals(1, taskList.size());
}
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 33 with Microservice

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

the class TestMicroserviceRegisterTask method testAlreadyRegisteredGetSchemaIdSetFailed.

@Test
public void testAlreadyRegisteredGetSchemaIdSetFailed(@Mocked ServiceRegistryClient srClient) {
    Microservice otherMicroservice = new Microservice();
    otherMicroservice.setAppId(microservice.getAppId());
    otherMicroservice.setServiceName("ms1");
    otherMicroservice.addSchema("s1", "");
    new Expectations() {

        {
            srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
            result = "serviceId";
            srClient.getMicroservice(anyString);
            result = null;
        }
    };
    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
    registerTask.run();
    Assert.assertEquals(false, registerTask.isRegistered());
    Assert.assertEquals(false, registerTask.isSchemaIdSetMatch());
    Assert.assertEquals("serviceId", microservice.getServiceId());
    Assert.assertEquals(1, taskList.size());
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Test(org.junit.Test)

Example 34 with Microservice

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

the class TestMicroserviceRegisterTask method testLocalSchemaAndServiceCenterSchemaIgnoreDiff.

@Test
public void testLocalSchemaAndServiceCenterSchemaIgnoreDiff(@Mocked ServiceRegistryClient srClient, @Mocked ServiceRegistryConfig serviceRegistryConfig) {
    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() {

        {
            serviceRegistryConfig.isIgnoreSwaggerDifference();
            result = true;
            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;
    }
    Assert.assertEquals(false, 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 35 with Microservice

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

the class RefreshableMicroserviceCacheTest method setUp.

@Before
public void setUp() throws Exception {
    srClient = new MockUp<ServiceRegistryClient>() {

        @Mock
        MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
            return findServiceInstancesOprHolder.value.apply(new Object[] { consumerId, appId, serviceName, versionRule, revision });
        }
    }.getMockInstance();
    consumerService = new Microservice();
    consumerService.setServiceId("consumerId");
    microserviceCache = new RefreshableMicroserviceCache(consumerService, MicroserviceCacheKey.builder().env("env").appId("app").serviceName("svc").build(), srClient, false);
    findServiceInstancesOprHolder.value = params -> {
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setNeedRefresh(true);
        microserviceInstances.setRevision("rev0");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(pulledInstances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MockUp(mockit.MockUp) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) Before(org.junit.Before)

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