|
@@ -16,12 +16,20 @@
|
|
|
*/
|
|
|
package com.ctrip.framework.apollo.biz.service;
|
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
|
|
|
+import com.ctrip.framework.apollo.biz.config.BizConfig;
|
|
|
import com.ctrip.framework.apollo.biz.entity.Item;
|
|
|
+import com.ctrip.framework.apollo.biz.repository.ItemRepository;
|
|
|
import com.ctrip.framework.apollo.common.dto.ItemInfoDTO;
|
|
|
import com.ctrip.framework.apollo.common.exception.BadRequestException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import org.junit.Assert;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
+import org.mockito.Mock;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
@@ -32,18 +40,28 @@ public class ItemServiceTest extends AbstractIntegrationTest {
|
|
|
@Autowired
|
|
|
private ItemService itemService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ItemRepository itemRepository;
|
|
|
+ @Autowired
|
|
|
+ private NamespaceService namespaceService;
|
|
|
+ @Autowired
|
|
|
+ private AuditService auditService;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private BizConfig bizConfig;
|
|
|
+
|
|
|
+ private ItemService itemService2;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp() throws Exception {
|
|
|
+ itemService2 = new ItemService(itemRepository, namespaceService, auditService, bizConfig);
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
- @Sql(scripts = "/sql/item-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
|
|
|
+ @Sql(scripts = {"/sql/namespace-test.sql","/sql/item-test.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
|
|
|
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
|
|
|
public void testSaveItem() {
|
|
|
- Item item = new Item();
|
|
|
- item.setNamespaceId(3);
|
|
|
- item.setKey("k3");
|
|
|
- item.setType(-1);
|
|
|
- item.setValue("v3");
|
|
|
- item.setComment("");
|
|
|
- item.setLineNum(3);
|
|
|
-
|
|
|
+ Item item = createItem(1L, "k3", "v3", -1);
|
|
|
try {
|
|
|
itemService.save(item);
|
|
|
Assert.fail();
|
|
@@ -57,16 +75,56 @@ public class ItemServiceTest extends AbstractIntegrationTest {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @Sql(scripts = "/sql/item-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
|
|
|
+ @Sql(scripts = {"/sql/namespace-test.sql", "/sql/item-test.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
|
|
|
+ @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
|
|
|
+ public void testSaveItemWithNamespaceValueLengthLimitOverride() {
|
|
|
+
|
|
|
+ long namespaceId = 1L;
|
|
|
+ String itemValue = "test-demo";
|
|
|
+
|
|
|
+ Map<Long, Integer> namespaceValueLengthOverride = new HashMap<>();
|
|
|
+ namespaceValueLengthOverride.put(namespaceId, itemValue.length() - 1);
|
|
|
+ when(bizConfig.namespaceValueLengthLimitOverride()).thenReturn(namespaceValueLengthOverride);
|
|
|
+ when(bizConfig.itemKeyLengthLimit()).thenReturn(100);
|
|
|
+
|
|
|
+ Item item = createItem(namespaceId, "k3", itemValue, 2);
|
|
|
+ try {
|
|
|
+ itemService2.save(item);
|
|
|
+ Assert.fail();
|
|
|
+ } catch (Exception e) {
|
|
|
+ Assert.assertTrue(e instanceof BadRequestException && e.getMessage().contains("value too long"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @Sql(scripts = {"/sql/namespace-test.sql", "/sql/item-test.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
|
|
|
+ @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
|
|
|
+ public void testSaveItemWithAppIdValueLengthLimitOverride() {
|
|
|
+
|
|
|
+ String appId = "testApp";
|
|
|
+ long namespaceId = 1L;
|
|
|
+ String itemValue = "test-demo";
|
|
|
+
|
|
|
+ Map<String, Integer> appIdValueLengthOverride = new HashMap<>();
|
|
|
+ appIdValueLengthOverride.put(appId, itemValue.length() - 1);
|
|
|
+ when(bizConfig.appIdValueLengthLimitOverride()).thenReturn(appIdValueLengthOverride);
|
|
|
+ when(bizConfig.itemKeyLengthLimit()).thenReturn(100);
|
|
|
+
|
|
|
+ Item item = createItem(namespaceId, "k3", itemValue, 2);
|
|
|
+ try {
|
|
|
+ itemService2.save(item);
|
|
|
+ Assert.fail();
|
|
|
+ } catch (Exception e) {
|
|
|
+ Assert.assertTrue(e instanceof BadRequestException && e.getMessage().contains("value too long"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @Sql(scripts = {"/sql/namespace-test.sql","/sql/item-test.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
|
|
|
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
|
|
|
public void testUpdateItem() {
|
|
|
- Item item = new Item();
|
|
|
+ Item item = createItem(1, "k1", "v1-new", 2);
|
|
|
item.setId(9901);
|
|
|
- item.setNamespaceId(1);
|
|
|
- item.setKey("k1");
|
|
|
- item.setType(2);
|
|
|
- item.setValue("v1-new");
|
|
|
- item.setComment("");
|
|
|
item.setLineNum(1);
|
|
|
|
|
|
Item dbItem = itemService.update(item);
|
|
@@ -96,4 +154,15 @@ public class ItemServiceTest extends AbstractIntegrationTest {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ private Item createItem(long namespaceId, String key, String value, int type) {
|
|
|
+ Item item = new Item();
|
|
|
+ item.setNamespaceId(namespaceId);
|
|
|
+ item.setKey(key);
|
|
|
+ item.setValue(value);
|
|
|
+ item.setType(type);
|
|
|
+ item.setComment("");
|
|
|
+ item.setLineNum(3);
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+
|
|
|
}
|