Search in sources :

Example 1 with ConfigCacheService

use of com.alibaba.nacos.config.server.service.ConfigCacheService in project nacos by alibaba.

the class ConfigServletInnerTest method testDoGetConfigV2.

@Test
public void testDoGetConfigV2() throws Exception {
    final MockedStatic<DiskUtil> diskUtil = Mockito.mockStatic(DiskUtil.class);
    final MockedStatic<ConfigCacheService> configCacheService = Mockito.mockStatic(ConfigCacheService.class);
    final MockedStatic<PropertyUtil> propertyUtil = Mockito.mockStatic(PropertyUtil.class);
    when(ConfigCacheService.tryReadLock(anyString())).thenReturn(1);
    // isBeta: false
    CacheItem cacheItem = new CacheItem("test");
    cacheItem.setBeta(false);
    List<String> ips4Beta = new ArrayList<>();
    ips4Beta.add("localhost");
    cacheItem.setIps4Beta(ips4Beta);
    when(ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem);
    // if tag is blank and direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    ConfigInfoWrapper configInfoWrapper = new ConfigInfoWrapper();
    configInfoWrapper.setDataId("test");
    configInfoWrapper.setGroup("test");
    configInfoWrapper.setContent("tag is blank and direct read is true");
    when(persistService.findConfigInfo(anyString(), anyString(), anyString())).thenReturn(configInfoWrapper);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("localhost:8080");
    request.addHeader(CLIENT_APPNAME_HEADER, "test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    String actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("tag is blank and direct read is true", response.getContentAsString());
    // if tag is blank and direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    response = new MockHttpServletResponse();
    File file = tempFolder.newFile("test.txt");
    when(DiskUtil.targetFile(anyString(), anyString(), anyString())).thenReturn(file);
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("", response.getContentAsString());
    // if tag is not blank and direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    ConfigInfoTagWrapper configInfoTagWrapper = new ConfigInfoTagWrapper();
    configInfoTagWrapper.setDataId("test");
    configInfoTagWrapper.setGroup("test");
    configInfoTagWrapper.setContent("tag is not blank and direct read is true");
    when(persistService.findConfigInfo4Tag(anyString(), anyString(), anyString(), anyString())).thenReturn(configInfoTagWrapper);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("tag is not blank and direct read is true", response.getContentAsString());
    // if tag is not blank and direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    response = new MockHttpServletResponse();
    when(DiskUtil.targetTagFile(anyString(), anyString(), anyString(), anyString())).thenReturn(file);
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("", response.getContentAsString());
    // if use auto tag and direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    Map<String, String> tagMd5 = new HashMap<>();
    tagMd5.put("auto-tag-test", "auto-tag-test");
    cacheItem.setTagMd5(tagMd5);
    request.addHeader("Vipserver-Tag", "auto-tag-test");
    configInfoTagWrapper.setContent("auto tag mode and direct read is true");
    when(persistService.findConfigInfo4Tag(anyString(), anyString(), anyString(), eq("auto-tag-test"))).thenReturn(configInfoTagWrapper);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("auto tag mode and direct read is true", response.getContentAsString());
    // if use auto tag and direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("", response.getContentAsString());
    diskUtil.close();
    configCacheService.close();
    propertyUtil.close();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ConfigInfoTagWrapper(com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) PropertyUtil(com.alibaba.nacos.config.server.utils.PropertyUtil) DiskUtil(com.alibaba.nacos.config.server.utils.DiskUtil) CacheItem(com.alibaba.nacos.config.server.model.CacheItem) File(java.io.File) ConfigCacheService(com.alibaba.nacos.config.server.service.ConfigCacheService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with ConfigCacheService

use of com.alibaba.nacos.config.server.service.ConfigCacheService in project nacos by alibaba.

the class ConfigServletInnerTest method testDoGetConfigV3.

@Test
public void testDoGetConfigV3() throws Exception {
    final MockedStatic<ConfigCacheService> configCacheService = Mockito.mockStatic(ConfigCacheService.class);
    // if lockResult equals 0
    when(ConfigCacheService.tryReadLock(anyString())).thenReturn(0);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    String actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND + "", actualValue);
    // if lockResult less than 0
    when(ConfigCacheService.tryReadLock(anyString())).thenReturn(-1);
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_CONFLICT + "", actualValue);
    configCacheService.close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConfigCacheService(com.alibaba.nacos.config.server.service.ConfigCacheService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with ConfigCacheService

use of com.alibaba.nacos.config.server.service.ConfigCacheService in project nacos by alibaba.

the class ConfigServletInnerTest method testDoGetConfigV1.

@Test
public void testDoGetConfigV1() throws Exception {
    final MockedStatic<DiskUtil> diskUtil = Mockito.mockStatic(DiskUtil.class);
    final MockedStatic<ConfigCacheService> configCacheService = Mockito.mockStatic(ConfigCacheService.class);
    final MockedStatic<PropertyUtil> propertyUtil = Mockito.mockStatic(PropertyUtil.class);
    when(ConfigCacheService.tryReadLock(anyString())).thenReturn(1);
    // isBeta: true
    CacheItem cacheItem = new CacheItem("test");
    cacheItem.setBeta(true);
    List<String> ips4Beta = new ArrayList<>();
    ips4Beta.add("localhost");
    cacheItem.setIps4Beta(ips4Beta);
    when(ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem);
    // if direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    ConfigInfoBetaWrapper configInfoBetaWrapper = new ConfigInfoBetaWrapper();
    configInfoBetaWrapper.setDataId("test");
    configInfoBetaWrapper.setGroup("test");
    configInfoBetaWrapper.setContent("isBeta:true, direct read: true");
    when(persistService.findConfigInfo4Beta(anyString(), anyString(), anyString())).thenReturn(configInfoBetaWrapper);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("localhost:8080");
    request.addHeader(CLIENT_APPNAME_HEADER, "test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    String actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("true", response.getHeader("isBeta"));
    Assert.assertEquals("isBeta:true, direct read: true", response.getContentAsString());
    // if direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    File file = tempFolder.newFile("test.txt");
    when(DiskUtil.targetBetaFile(anyString(), anyString(), anyString())).thenReturn(file);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("true", response.getHeader("isBeta"));
    Assert.assertEquals("", response.getContentAsString());
    diskUtil.close();
    configCacheService.close();
    propertyUtil.close();
}
Also used : ConfigInfoBetaWrapper(com.alibaba.nacos.config.server.model.ConfigInfoBetaWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PropertyUtil(com.alibaba.nacos.config.server.utils.PropertyUtil) DiskUtil(com.alibaba.nacos.config.server.utils.DiskUtil) CacheItem(com.alibaba.nacos.config.server.model.CacheItem) File(java.io.File) ConfigCacheService(com.alibaba.nacos.config.server.service.ConfigCacheService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with ConfigCacheService

use of com.alibaba.nacos.config.server.service.ConfigCacheService in project nacos by alibaba.

the class MD5UtilTest method testCompareMd5.

@Test
public void testCompareMd5() {
    final MockedStatic<ConfigCacheService> configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class);
    when(ConfigCacheService.isUptodate(anyString(), anyString(), anyString(), anyString())).thenReturn(false);
    Map<String, String> clientMd5Map = new HashMap<>();
    clientMd5Map.put("test", "test");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Vipserver-Tag", "test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    List<String> changedGroupKeys = MD5Util.compareMd5(request, response, clientMd5Map);
    Assert.assertEquals(1, changedGroupKeys.size());
    Assert.assertEquals("test", changedGroupKeys.get(0));
    configCacheServiceMockedStatic.close();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConfigCacheService(com.alibaba.nacos.config.server.service.ConfigCacheService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ConfigCacheService (com.alibaba.nacos.config.server.service.ConfigCacheService)4 Test (org.junit.Test)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 CacheItem (com.alibaba.nacos.config.server.model.CacheItem)2 DiskUtil (com.alibaba.nacos.config.server.utils.DiskUtil)2 PropertyUtil (com.alibaba.nacos.config.server.utils.PropertyUtil)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConfigInfoBetaWrapper (com.alibaba.nacos.config.server.model.ConfigInfoBetaWrapper)1 ConfigInfoTagWrapper (com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper)1 ConfigInfoWrapper (com.alibaba.nacos.config.server.model.ConfigInfoWrapper)1