Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some failing unit tests in nacos config module #8379

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -39,27 +40,34 @@ public class ConditionDistributedEmbedStorageTest {
@Before
public void init() {
conditionDistributedEmbedStorage = new ConditionDistributedEmbedStorage();
Mockito.mockStatic(PropertyUtil.class);
Mockito.mockStatic(EnvUtil.class);

}

@Test
public void testMatches() {
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true);
MockedStatic<PropertyUtil> propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);
MockedStatic<EnvUtil> envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class);

propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true);
Assert.assertFalse(conditionDistributedEmbedStorage.matches(context, metadata));

Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false);
propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false);
Assert.assertTrue(conditionDistributedEmbedStorage.matches(context, metadata));

Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true);
propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true);
Assert.assertFalse(conditionDistributedEmbedStorage.matches(context, metadata));

Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false);
propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false);
Assert.assertFalse(conditionDistributedEmbedStorage.matches(context, metadata));

propertyUtilMockedStatic.close();
envUtilMockedStatic.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -38,16 +39,18 @@ public class ConditionOnEmbeddedStorageTest {
@Before
public void init() {
conditionOnEmbeddedStorage = new ConditionOnEmbeddedStorage();
Mockito.mockStatic(PropertyUtil.class);
}

@Test
public void testMatches() {
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
MockedStatic<PropertyUtil> mockedStatic = Mockito.mockStatic(PropertyUtil.class);
mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
Assert.assertTrue(conditionOnEmbeddedStorage.matches(context, metadata));
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false);

mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false);
Assert.assertFalse(conditionOnEmbeddedStorage.matches(context, metadata));

mockedStatic.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -38,16 +39,19 @@ public class ConditionOnExternalStorageTest {
@Before
public void init() {
conditionOnExternalStorage = new ConditionOnExternalStorage();
Mockito.mockStatic(PropertyUtil.class);
}

@Test
public void testMatches() {
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
Assert.assertFalse(conditionOnExternalStorage.matches(context, metadata));
MockedStatic<PropertyUtil> mockedStatic = Mockito.mockStatic(PropertyUtil.class);

Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false);
mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
Assert.assertFalse(conditionOnExternalStorage.matches(context, metadata));

mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false);
Assert.assertTrue(conditionOnExternalStorage.matches(context, metadata));

mockedStatic.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -39,27 +40,31 @@ public class ConditionStandaloneEmbedStorageTest {
@Before
public void init() {
conditionStandaloneEmbedStorage = new ConditionStandaloneEmbedStorage();
Mockito.mockStatic(PropertyUtil.class);
Mockito.mockStatic(EnvUtil.class);
}

@Test
public void testMatches() {
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true);
Assert.assertTrue(conditionStandaloneEmbedStorage.matches(context, metadata));
MockedStatic<PropertyUtil> propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);
MockedStatic<EnvUtil> envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class);

Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false);
propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true);
Assert.assertTrue(conditionStandaloneEmbedStorage.matches(context, metadata));

propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false);
Assert.assertFalse(conditionStandaloneEmbedStorage.matches(context, metadata));
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true);

propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true);
Assert.assertFalse(conditionStandaloneEmbedStorage.matches(context, metadata));
Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false);
Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false);

propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false);
envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false);
Assert.assertFalse(conditionStandaloneEmbedStorage.matches(context, metadata));

propertyUtilMockedStatic.close();
envUtilMockedStatic.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.mock.web.MockMultipartFile;
Expand Down Expand Up @@ -389,15 +390,15 @@ public void testExportConfigV2() throws Exception {

@Test
public void testImportAndPublishConfig() throws Exception {
Mockito.mockStatic(ZipUtils.class);

MockedStatic<ZipUtils> zipUtilsMockedStatic = Mockito.mockStatic(ZipUtils.class);
List<ZipUtils.ZipItem> zipItems = new ArrayList<>();
ZipUtils.ZipItem zipItem = new ZipUtils.ZipItem("test/test", "test");
zipItems.add(zipItem);
ZipUtils.UnZipResult unziped = new ZipUtils.UnZipResult(zipItems, null);
MockMultipartFile file = new MockMultipartFile("file", "test.zip", "application/zip", "test".getBytes());

when(ZipUtils.unzip(file.getBytes())).thenReturn(unziped);
zipUtilsMockedStatic.when(() -> ZipUtils.unzip(file.getBytes())).thenReturn(unziped);
when(persistService.tenantInfoCountByTenantId("public")).thenReturn(1);
Map<String, Object> map = new HashMap<>();
map.put("test", "test");
Expand All @@ -416,6 +417,8 @@ public void testImportAndPublishConfig() throws Exception {
Assert.assertEquals("200", code);
Map<String, Object> resultMap = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("data").toString(), Map.class);
Assert.assertEquals(map.get("test"), resultMap.get("test").toString());

zipUtilsMockedStatic.close();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ public void testSetLogLevel() throws Exception {

@Test
public void testDerbyOps() throws Exception {

MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class);
when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
propertyUtil.close();
Mockito.mockStatic(DynamicDataSource.class);
MockedStatic<PropertyUtil> propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);
MockedStatic<DynamicDataSource> dynamicDataSourceMockedStatic = Mockito.mockStatic(DynamicDataSource.class);

propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);
DynamicDataSource dataSource = Mockito.mock(DynamicDataSource.class);
when(DynamicDataSource.getInstance()).thenReturn(dataSource);
dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dataSource);
LocalDataSourceServiceImpl dataSourceService = Mockito.mock(LocalDataSourceServiceImpl.class);
when(dataSource.getDataSource()).thenReturn(dataSourceService);
JdbcTemplate template = Mockito.mock(JdbcTemplate.class);
Expand All @@ -118,23 +117,27 @@ public void testDerbyOps() throws Exception {
.param("sql", "SELECT * FROM TEST");
String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("200", JacksonUtils.toObj(actualValue).get("code").toString());
propertyUtilMockedStatic.close();
dynamicDataSourceMockedStatic.close();

}

@Test
public void testImportDerby() throws Exception {
MockedStatic<PropertyUtil> propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);
MockedStatic<ApplicationUtils> applicationUtilsMockedStatic = Mockito.mockStatic(ApplicationUtils.class);

MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class);
when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
propertyUtil.close();
Mockito.mockStatic(ApplicationUtils.class);
when(ApplicationUtils.getBean(DatabaseOperate.class)).thenReturn(Mockito.mock(DatabaseOperate.class));
propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true);

applicationUtilsMockedStatic.when(() -> ApplicationUtils.getBean(DatabaseOperate.class))
.thenReturn(Mockito.mock(DatabaseOperate.class));
MockMultipartFile file = new MockMultipartFile("file", "test.zip", "application/zip", "test".getBytes());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(Constants.OPS_CONTROLLER_PATH + "/data/removal").file(file);
int actualValue = mockMvc.perform(builder).andReturn().getResponse().getStatus();
Assert.assertEquals(200, actualValue);

propertyUtilMockedStatic.close();
applicationUtilsMockedStatic.close();
}

}
Loading