|
| 1 | +/* |
| 2 | + * Copyright 2024 Apollo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package com.ctrip.framework.apollo.biz.service; |
| 18 | + |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import com.ctrip.framework.apollo.biz.AbstractIntegrationTest; |
| 22 | +import com.ctrip.framework.apollo.biz.config.BizConfig; |
| 23 | +import com.ctrip.framework.apollo.biz.entity.Item; |
| 24 | +import com.ctrip.framework.apollo.biz.entity.Namespace; |
| 25 | +import com.ctrip.framework.apollo.common.dto.ItemChangeSets; |
| 26 | +import com.ctrip.framework.apollo.common.dto.ItemDTO; |
| 27 | +import com.ctrip.framework.apollo.common.exception.BadRequestException; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Test; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 32 | +import org.springframework.test.context.jdbc.Sql; |
| 33 | + |
| 34 | +public class ItemSetServiceTest extends AbstractIntegrationTest { |
| 35 | + |
| 36 | + @MockBean |
| 37 | + private BizConfig bizConfig; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private ItemService itemService; |
| 41 | + @Autowired |
| 42 | + private NamespaceService namespaceService; |
| 43 | + |
| 44 | + @Autowired |
| 45 | + private ItemSetService itemSetService; |
| 46 | + |
| 47 | + @Test |
| 48 | + @Sql(scripts = "/sql/itemset-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 49 | + @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 50 | + public void testUpdateSetWithoutItemNumLimit() { |
| 51 | + |
| 52 | + when(bizConfig.itemKeyLengthLimit()).thenReturn(128); |
| 53 | + when(bizConfig.itemValueLengthLimit()).thenReturn(20000); |
| 54 | + |
| 55 | + when(bizConfig.isItemNumLimitEnabled()).thenReturn(false); |
| 56 | + when(bizConfig.itemNumLimit()).thenReturn(5); |
| 57 | + |
| 58 | + Namespace namespace = namespaceService.findOne(1L); |
| 59 | + |
| 60 | + ItemChangeSets changeSets = new ItemChangeSets(); |
| 61 | + changeSets.addCreateItem(buildNormalItem(0L, namespace.getId(), "k6", "v6", "test item num limit", 6)); |
| 62 | + changeSets.addCreateItem(buildNormalItem(0L, namespace.getId(), "k7", "v7", "test item num limit", 7)); |
| 63 | + |
| 64 | + try { |
| 65 | + itemSetService.updateSet(namespace, changeSets); |
| 66 | + } catch (Exception e) { |
| 67 | + Assert.fail(); |
| 68 | + } |
| 69 | + |
| 70 | + int size = itemService.findNonEmptyItemCount(namespace.getId()); |
| 71 | + Assert.assertEquals(7, size); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @Sql(scripts = "/sql/itemset-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 77 | + @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 78 | + public void testUpdateSetWithItemNumLimit() { |
| 79 | + |
| 80 | + when(bizConfig.itemKeyLengthLimit()).thenReturn(128); |
| 81 | + when(bizConfig.itemValueLengthLimit()).thenReturn(20000); |
| 82 | + |
| 83 | + when(bizConfig.isItemNumLimitEnabled()).thenReturn(true); |
| 84 | + when(bizConfig.itemNumLimit()).thenReturn(5); |
| 85 | + |
| 86 | + Namespace namespace = namespaceService.findOne(1L); |
| 87 | + Item item9901 = itemService.findOne(9901); |
| 88 | + Item item9902 = itemService.findOne(9902); |
| 89 | + |
| 90 | + ItemChangeSets changeSets = new ItemChangeSets(); |
| 91 | + changeSets.addUpdateItem(buildNormalItem(item9901.getId(), item9901.getNamespaceId(), item9901.getKey(), item9901.getValue() + " update", item9901.getComment(), item9901.getLineNum())); |
| 92 | + changeSets.addDeleteItem(buildNormalItem(item9902.getId(), item9902.getNamespaceId(), item9902.getKey(), item9902.getValue() + " update", item9902.getComment(), item9902.getLineNum())); |
| 93 | + changeSets.addCreateItem(buildNormalItem(0L, item9901.getNamespaceId(), "k6", "v6", "test item num limit", 6)); |
| 94 | + changeSets.addCreateItem(buildNormalItem(0L, item9901.getNamespaceId(), "k7", "v7", "test item num limit", 7)); |
| 95 | + |
| 96 | + try { |
| 97 | + itemSetService.updateSet(namespace, changeSets); |
| 98 | + Assert.fail(); |
| 99 | + } catch (Exception e) { |
| 100 | + Assert.assertTrue(e instanceof BadRequestException); |
| 101 | + } |
| 102 | + |
| 103 | + int size = itemService.findNonEmptyItemCount(namespace.getId()); |
| 104 | + Assert.assertEquals(5, size); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @Sql(scripts = "/sql/itemset-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 110 | + @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 111 | + public void testUpdateSetWithItemNumLimit2() { |
| 112 | + |
| 113 | + when(bizConfig.itemKeyLengthLimit()).thenReturn(128); |
| 114 | + when(bizConfig.itemValueLengthLimit()).thenReturn(20000); |
| 115 | + |
| 116 | + when(bizConfig.isItemNumLimitEnabled()).thenReturn(true); |
| 117 | + when(bizConfig.itemNumLimit()).thenReturn(5); |
| 118 | + |
| 119 | + Namespace namespace = namespaceService.findOne(1L); |
| 120 | + Item item9901 = itemService.findOne(9901); |
| 121 | + Item item9902 = itemService.findOne(9902); |
| 122 | + |
| 123 | + ItemChangeSets changeSets = new ItemChangeSets(); |
| 124 | + changeSets.addUpdateItem(buildNormalItem(item9901.getId(), item9901.getNamespaceId(), item9901.getKey(), item9901.getValue() + " update", item9901.getComment(), item9901.getLineNum())); |
| 125 | + changeSets.addDeleteItem(buildNormalItem(item9902.getId(), item9902.getNamespaceId(), item9902.getKey(), item9902.getValue() + " update", item9902.getComment(), item9902.getLineNum())); |
| 126 | + changeSets.addCreateItem(buildNormalItem(0L, item9901.getNamespaceId(), "k6", "v6", "test item num limit", 6)); |
| 127 | + |
| 128 | + try { |
| 129 | + itemSetService.updateSet(namespace, changeSets); |
| 130 | + } catch (Exception e) { |
| 131 | + Assert.fail(); |
| 132 | + } |
| 133 | + |
| 134 | + int size = itemService.findNonEmptyItemCount(namespace.getId()); |
| 135 | + Assert.assertEquals(5, size); |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + private ItemDTO buildNormalItem(Long id, Long namespaceId, String key, String value, String comment, int lineNum) { |
| 141 | + ItemDTO item = new ItemDTO(key, value, comment, lineNum); |
| 142 | + item.setId(id); |
| 143 | + item.setNamespaceId(namespaceId); |
| 144 | + return item; |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments