Skip to content

Commit

Permalink
fix bug for string convert to list
Browse files Browse the repository at this point in the history
  • Loading branch information
qifanwang committed Oct 15, 2024
1 parent af4d3ee commit 51d5912
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ctrip.xpipe.redis.console.resources;

import com.ctrip.xpipe.api.codec.GenericTypeReference;
import com.ctrip.xpipe.api.foundation.FoundationService;
import com.ctrip.xpipe.codec.JsonCodec;
import com.ctrip.xpipe.monitor.CatTransactionMonitor;
Expand Down Expand Up @@ -254,8 +255,7 @@ public List<RouteModel> getActiveRoutes() {
HttpMethod.GET, null, String.class, "getActiveRoutes"
);
String content = resp.getBody();
JsonCodec pretty = new JsonCodec(true, true);
return pretty.decode(content, List.class);
return JsonCodec.DEFAULT.decode(content, new GenericTypeReference<List<RouteModel>>(){});
}

public void updateKeeperStatus(String dcId, String clusterId, String shardId, KeeperMeta newActiveKeeper) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.ctrip.xpipe.redis.console.resources;

import com.ctrip.xpipe.redis.console.config.ConsoleConfig;
import com.ctrip.xpipe.redis.console.model.RouteModel;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ConsolePortalServiceTest {

@Mock
private ConsoleConfig config;

@InjectMocks
private ConsolePortalService consoleService;

@Mock
private RestTemplate restTemplate;

@Test
public void testGetActiveRoutes() {
// Arrange
String url = "http://example.com/api/routes/active";
String jsonResponse = "[ {\n" +
" \"id\" : 12,\n" +
" \"orgId\" : 0,\n" +
" \"clusterType\" : \"\",\n" +
" \"srcProxyIds\" : \"\",\n" +
" \"dstProxyIds\" : \"869,870\",\n" +
" \"optionProxyIds\" : \"\",\n" +
" \"srcDcName\" : \"SHAFQ\",\n" +
" \"dstDcName\" : \"FRA-AWS\",\n" +
" \"tag\" : \"console\",\n" +
" \"active\" : true,\n" +
" \"isPublic\" : true,\n" +
" \"description\" : \"---->21034014\"\n" +
"}, {\n" +
" \"id\" : 22,\n" +
" \"orgId\" : 0,\n" +
" \"clusterType\" : \"\",\n" +
" \"srcProxyIds\" : \"914,915\",\n" +
" \"dstProxyIds\" : \"869,870\",\n" +
" \"optionProxyIds\" : \"\",\n" +
" \"srcDcName\" : \"SHARB\",\n" +
" \"dstDcName\" : \"FRA-AWS\",\n" +
" \"tag\" : \"console\",\n" +
" \"active\" : true,\n" +
" \"isPublic\" : true,\n" +
" \"description\" : \"[公共] xreg 21062164---->21061327\"\n" +
"}]";
ResponseEntity<String> responseEntity = ResponseEntity.ok(jsonResponse);

when(config.getConsoleNoDbDomain()).thenReturn("http://example.com");
when(restTemplate.exchange(url, HttpMethod.GET, null, String.class))
.thenReturn(responseEntity);

List<RouteModel> routeModels = consoleService.getActiveRoutes();
for(RouteModel routeModel : routeModels) {
System.out.println(routeModel.getDescription());
}
Assert.assertEquals(routeModels.size(), 2);
Assert.assertEquals(routeModels.get(1).getId(), 22);

}
}

0 comments on commit 51d5912

Please sign in to comment.