/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.servicecomb.core;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import io.servicecomb.core.endpoint.EndpointsCache;
import io.servicecomb.serviceregistry.api.registry.Microservice;
import io.servicecomb.swagger.invocation.AsyncResponse;
import mockit.Mocked;

public class TestTransport {
  @Test
  public void testEndpoint() throws Exception {
    Endpoint oEndpoint = new Endpoint(new Transport() {

      @Override
      public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
      }

      @Override
      public Object parseAddress(String address) {
        return "127.0.0.1";
      }

      @Override
      public boolean init() throws Exception {
        return true;
      }

      @Override
      public String getName() {
        return "test";
      }

      @Override
      public Endpoint getEndpoint() {
        return (new Endpoint(this, "testEndpoint"));
      }

      @Override
      public Endpoint getPublishEndpoint() throws Exception {
        return (new Endpoint(this, "testEndpoint"));
      }
    }, "rest://127.0.0.1:8080");
    oEndpoint.getTransport().init();
    Assert.assertEquals("rest://127.0.0.1:8080", oEndpoint.getEndpoint());
    Assert.assertEquals("127.0.0.1", oEndpoint.getAddress());
    Assert.assertEquals("test", oEndpoint.getTransport().getName());
    Assert.assertEquals("rest://127.0.0.1:8080", oEndpoint.getEndpoint().toString());
  }

  @Test
  public void testAbstractTransport(@Mocked Microservice microservice) throws Exception {

    EndpointsCache oEndpointsCache = new EndpointsCache("app", "testname", "test", "test");

    try {
      List<Endpoint> endpoionts = oEndpointsCache.getLatestEndpoints();
      Assert.assertEquals(endpoionts.size(), 0);
    } catch (Exception e) {
      Assert.assertEquals(null, e.getMessage());
    }
  }
}

@@ -843,20 +843,96 @@
 va.util.
-List
+ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -1027,16 +1027,71 @@
 sCache;%0A
+import io.servicecomb.core.transport.TransportManager;%0A
 import i
@@ -1147,16 +1147,273 @@
 ervice;%0A
+import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;%0Aimport io.servicecomb.serviceregistry.cache.CacheEndpoint;%0Aimport io.servicecomb.serviceregistry.cache.InstanceCache;%0Aimport io.servicecomb.serviceregistry.cache.InstanceCacheManager;%0A
 import i
@@ -1460,16 +1460,70 @@
 sponse;%0A
+import mockit.Expectations;%0Aimport mockit.Injectable;%0A
 import m
@@ -2770,37 +2770,301 @@
 oservice
-) throws Exception %7B%0A
+,%0A      @Injectable InstanceCacheManager instanceCacheManager, @Injectable TransportManager transportManager,%0A      @Mocked InstanceCache instanceCache, @Injectable MicroserviceInstance instance)%0A      throws Exception %7B%0A    EndpointsCache.init(instanceCacheManager, transportManager);
 %0A    End
@@ -3144,17 +3144,17 @@
 %22, %22
-t
+r
 est%22);%0A%0A
     
@@ -3153,20 +3153,8 @@
 );%0A%0A
-    try %7B%0A  
     
@@ -3219,26 +3219,24 @@
 ints();%0A    
-  
 Assert.asser
@@ -3270,90 +3270,735 @@
 0);%0A
+%0A
     
-%7D catch (Exception e) %7B%0A      Assert.assertEquals(null, e.getMessage
+Map%3CString, List%3CCacheEndpoint%3E%3E allTransportMap = new HashMap%3C%3E();%0A    CacheEndpoint cacheEndpoint = new CacheEndpoint(%22rest://127.0.0.1:9999%22, instance);%0A    List%3CCacheEndpoint%3E restEndpoints = new ArrayList%3C%3E();%0A    restEndpoints.add(cacheEndpoint);%0A    allTransportMap.put(%22rest%22, restEndpoints);%0A%0A    new Expectations() %7B%0A      %7B%0A        instanceCacheManager.getOrCreate(anyString, anyString, anyString);%0A        result = instanceCache;%0A        instanceCache.cacheChanged((InstanceCache) any);%0A        result = true;%0A        instanceCache.getOrCreateTransportMap();%0A        result = allTransportMap;%0A      %7D%0A    %7D;%0A%0A    endpoionts = oEndpointsCache.getLatestEndpoints
 ()
-)
 ;%0A    
-%7D
+Assert.assertEquals(endpoionts.size(), 1);
 %0A  %7D

add ut test cases