Search in sources :

Example 1 with TarsUpstream

use of org.apache.shenyu.common.dto.convert.selector.TarsUpstream in project incubator-shenyu by apache.

the class ShenyuClientRegisterDivideServiceImplTest method testBuildHandle.

@Test
public void testBuildHandle() {
    shenyuClientRegisterDivideService = spy(shenyuClientRegisterDivideService);
    final String returnStr = "[{protocol:'http://',upstreamHost:'localhost',upstreamUrl:'localhost:8090',warmup:10,weight:50,status:true,timestamp:1637826588267}," + "{protocol:'http://',upstreamHost:'localhost',upstreamUrl:'localhost:8091',warmup:10,weight:50,status:true,timestamp:1637826588267}]";
    final String expected = "[{\"weight\":50,\"warmup\":10,\"protocol\":\"http://\",\"upstreamHost\":\"localhost\",\"upstreamUrl\":\"localhost:8090\",\"status\":true,\"timestamp\":1637826588267}," + "{\"weight\":50,\"warmup\":10,\"protocol\":\"http://\",\"upstreamHost\":\"localhost\",\"upstreamUrl\":\"localhost:8091\",\"status\":true,\"timestamp\":1637826588267}]";
    List<URIRegisterDTO> list = new ArrayList<>();
    list.add(URIRegisterDTO.builder().protocol("http://").appName("test1").rpcType(RpcTypeEnum.HTTP.getName()).host(LOCALHOST).port(8090).build());
    SelectorDO selectorDO = mock(SelectorDO.class);
    when(selectorDO.getHandle()).thenReturn(returnStr);
    doNothing().when(shenyuClientRegisterDivideService).doSubmit(any(), any());
    String actual = shenyuClientRegisterDivideService.buildHandle(list, selectorDO);
    assertEquals(expected, actual);
    List<TarsUpstream> resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 2);
    list.clear();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.HTTP.getName()).host(LOCALHOST).port(8092).build());
    selectorDO = mock(SelectorDO.class);
    when(selectorDO.getHandle()).thenReturn(returnStr);
    doNothing().when(shenyuClientRegisterDivideService).doSubmit(any(), any());
    actual = shenyuClientRegisterDivideService.buildHandle(list, selectorDO);
    resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 3);
    list.clear();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.HTTP.getName()).host(LOCALHOST).port(8090).build());
    doNothing().when(shenyuClientRegisterDivideService).doSubmit(any(), any());
    selectorDO = mock(SelectorDO.class);
    actual = shenyuClientRegisterDivideService.buildHandle(list, selectorDO);
    resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 1);
}
Also used : SelectorDO(org.apache.shenyu.admin.model.entity.SelectorDO) URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) ArrayList(java.util.ArrayList) TarsUpstream(org.apache.shenyu.common.dto.convert.selector.TarsUpstream) Test(org.junit.jupiter.api.Test)

Example 2 with TarsUpstream

use of org.apache.shenyu.common.dto.convert.selector.TarsUpstream in project incubator-shenyu by apache.

the class ShenyuClientRegisterGrpcServiceImplTest method testBuildHandle.

@Test
public void testBuildHandle() {
    shenyuClientRegisterGrpcService = spy(shenyuClientRegisterGrpcService);
    final String returnStr = "[{upstreamUrl='localhost:8090',weight=1,status=true,timestamp=1637826588267}," + "{upstreamUrl='localhost:8091',weight=2,status=true,timestamp=1637826588267}]";
    final String expected = "[{\"weight\":1,\"upstreamUrl\":\"localhost:8090\",\"status\":true,\"timestamp\":1637826588267}," + "{\"weight\":2,\"upstreamUrl\":\"localhost:8091\",\"status\":true,\"timestamp\":1637826588267}]";
    List<URIRegisterDTO> list = new ArrayList<>();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.GRPC.getName()).host("localhost").port(8090).build());
    SelectorDO selectorDO = mock(SelectorDO.class);
    when(selectorDO.getHandle()).thenReturn(returnStr);
    doNothing().when(shenyuClientRegisterGrpcService).doSubmit(any(), any());
    String actual = shenyuClientRegisterGrpcService.buildHandle(list, selectorDO);
    assertEquals(actual, expected);
    List<TarsUpstream> resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 2);
    list.clear();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.GRPC.getName()).host("localhost").port(8092).build());
    selectorDO = mock(SelectorDO.class);
    when(selectorDO.getHandle()).thenReturn(returnStr);
    doNothing().when(shenyuClientRegisterGrpcService).doSubmit(any(), any());
    actual = shenyuClientRegisterGrpcService.buildHandle(list, selectorDO);
    resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 3);
    list.clear();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.GRPC.getName()).host("localhost").port(8090).build());
    doNothing().when(shenyuClientRegisterGrpcService).doSubmit(any(), any());
    selectorDO = mock(SelectorDO.class);
    actual = shenyuClientRegisterGrpcService.buildHandle(list, selectorDO);
    resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 1);
}
Also used : SelectorDO(org.apache.shenyu.admin.model.entity.SelectorDO) URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) ArrayList(java.util.ArrayList) TarsUpstream(org.apache.shenyu.common.dto.convert.selector.TarsUpstream) Test(org.junit.jupiter.api.Test)

Example 3 with TarsUpstream

use of org.apache.shenyu.common.dto.convert.selector.TarsUpstream in project incubator-shenyu by apache.

the class ShenyuClientRegisterTarsServiceImplTest method testBuildHandle.

@Test
public void testBuildHandle() {
    shenyuClientRegisterTarsService = spy(shenyuClientRegisterTarsService);
    final String returnStr = "[{upstreamUrl:'localhost:8090',weight:1,warmup:10,status:true,timestamp:1637826588267}," + "{upstreamUrl:'localhost:8091',weight:2,warmup:10,status:true,timestamp:1637826588267}]";
    final String expected = "[{\"weight\":1,\"warmup\":10,\"upstreamUrl\":\"localhost:8090\",\"status\":true,\"timestamp\":1637826588267}," + "{\"weight\":2,\"warmup\":10,\"upstreamUrl\":\"localhost:8091\",\"status\":true,\"timestamp\":1637826588267}]";
    List<URIRegisterDTO> list = new ArrayList<>();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.TARS.getName()).host("localhost").port(8090).build());
    SelectorDO selectorDO = mock(SelectorDO.class);
    when(selectorDO.getHandle()).thenReturn(returnStr);
    doNothing().when(shenyuClientRegisterTarsService).doSubmit(any(), any());
    String actual = shenyuClientRegisterTarsService.buildHandle(list, selectorDO);
    assertEquals(actual, expected);
    List<TarsUpstream> resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 2);
    list.clear();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.TARS.getName()).host("localhost").port(8092).build());
    selectorDO = mock(SelectorDO.class);
    when(selectorDO.getHandle()).thenReturn(returnStr);
    doNothing().when(shenyuClientRegisterTarsService).doSubmit(any(), any());
    actual = shenyuClientRegisterTarsService.buildHandle(list, selectorDO);
    resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 3);
    list.clear();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.TARS.getName()).host("localhost").port(8090).build());
    doNothing().when(shenyuClientRegisterTarsService).doSubmit(any(), any());
    selectorDO = mock(SelectorDO.class);
    actual = shenyuClientRegisterTarsService.buildHandle(list, selectorDO);
    resultList = GsonUtils.getInstance().fromCurrentList(actual, TarsUpstream.class);
    assertEquals(resultList.size(), 1);
}
Also used : SelectorDO(org.apache.shenyu.admin.model.entity.SelectorDO) URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) ArrayList(java.util.ArrayList) TarsUpstream(org.apache.shenyu.common.dto.convert.selector.TarsUpstream) Test(org.junit.jupiter.api.Test)

Example 4 with TarsUpstream

use of org.apache.shenyu.common.dto.convert.selector.TarsUpstream in project incubator-shenyu by apache.

the class ApplicationConfigCache method initPrxClass.

/**
 * initPrxClass.
 *
 * @param selectorData selectorData
 */
public void initPrxClass(final SelectorData selectorData) {
    try {
        final List<TarsUpstream> upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), TarsUpstream.class);
        if (CollectionUtils.isEmpty(upstreamList)) {
            invalidate(selectorData.getName());
            return;
        }
        refreshUpstreamCache.put(selectorData.getName(), upstreamList);
        List<MetaData> metaDataList = ctxPathCache.getOrDefault(selectorData.getName(), new ArrayList<>());
        for (MetaData metaData : metaDataList) {
            refreshTarsInvokePrxList(metaData, upstreamList);
        }
    } catch (ExecutionException | NoSuchMethodException e) {
        throw new ShenyuException(e.getCause());
    }
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) ExecutionException(java.util.concurrent.ExecutionException) TarsUpstream(org.apache.shenyu.common.dto.convert.selector.TarsUpstream)

Example 5 with TarsUpstream

use of org.apache.shenyu.common.dto.convert.selector.TarsUpstream in project incubator-shenyu by apache.

the class ShenyuClientRegisterGrpcServiceImplTest method testBuildGrpcUpstreamList.

@Test
public void testBuildGrpcUpstreamList() {
    List<URIRegisterDTO> list = new ArrayList<>();
    list.add(URIRegisterDTO.builder().appName("test1").rpcType(RpcTypeEnum.GRPC.getName()).host("localhost").port(8090).build());
    list.add(URIRegisterDTO.builder().appName("test2").rpcType(RpcTypeEnum.GRPC.getName()).host("localhost").port(8091).build());
    try {
        Method testMethod = shenyuClientRegisterGrpcService.getClass().getDeclaredMethod("buildGrpcUpstreamList", List.class);
        testMethod.setAccessible(true);
        List<TarsUpstream> result = (List<TarsUpstream>) testMethod.invoke(shenyuClientRegisterGrpcService, list);
        assertEquals(result.size(), 2);
    } catch (Exception e) {
        throw new ShenyuException(e.getCause());
    }
}
Also used : URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) Method(java.lang.reflect.Method) TarsUpstream(org.apache.shenyu.common.dto.convert.selector.TarsUpstream) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) Test(org.junit.jupiter.api.Test)

Aggregations

TarsUpstream (org.apache.shenyu.common.dto.convert.selector.TarsUpstream)6 ArrayList (java.util.ArrayList)5 URIRegisterDTO (org.apache.shenyu.register.common.dto.URIRegisterDTO)5 Test (org.junit.jupiter.api.Test)5 SelectorDO (org.apache.shenyu.admin.model.entity.SelectorDO)3 ShenyuException (org.apache.shenyu.common.exception.ShenyuException)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)1 MetaData (org.apache.shenyu.common.dto.MetaData)1