|
| 1 | +/* |
| 2 | + * Copyright 2023 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.adminservice.controller; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import com.ctrip.framework.apollo.biz.entity.Commit; |
| 22 | +import com.ctrip.framework.apollo.biz.repository.CommitRepository; |
| 23 | +import com.ctrip.framework.apollo.biz.repository.ItemRepository; |
| 24 | +import com.ctrip.framework.apollo.common.dto.AppDTO; |
| 25 | +import com.ctrip.framework.apollo.common.dto.ClusterDTO; |
| 26 | +import com.ctrip.framework.apollo.common.dto.ItemDTO; |
| 27 | +import com.ctrip.framework.apollo.common.dto.NamespaceDTO; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Test; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.data.domain.Pageable; |
| 34 | +import org.springframework.http.HttpStatus; |
| 35 | +import org.springframework.http.ResponseEntity; |
| 36 | +import org.springframework.test.context.jdbc.Sql; |
| 37 | +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author kl (http://kailing.pub) |
| 41 | + * @since 2023/3/21 |
| 42 | + */ |
| 43 | +public class ItemControllerTest extends AbstractControllerTest { |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private CommitRepository commitRepository; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private ItemRepository itemRepository; |
| 50 | + |
| 51 | + @Test |
| 52 | + @Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) |
| 53 | + @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) |
| 54 | + public void testCreate() { |
| 55 | + String appId = "someAppId"; |
| 56 | + AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); |
| 57 | + assert app != null; |
| 58 | + ClusterDTO cluster = restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); |
| 59 | + assert cluster != null; |
| 60 | + NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), |
| 61 | + NamespaceDTO.class, app.getAppId(), cluster.getName(), "application"); |
| 62 | + |
| 63 | + String itemKey = "test-key"; |
| 64 | + String itemValue = "test-value"; |
| 65 | + ItemDTO item = new ItemDTO(itemKey, itemValue, "", 1); |
| 66 | + assert namespace != null; |
| 67 | + item.setNamespaceId(namespace.getId()); |
| 68 | + item.setDataChangeLastModifiedBy("apollo"); |
| 69 | + |
| 70 | + ResponseEntity<ItemDTO> response = restTemplate.postForEntity(itemBaseUrl(), |
| 71 | + item, ItemDTO.class, app.getAppId(), cluster.getName(), namespace.getNamespaceName()); |
| 72 | + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 73 | + Assert.assertEquals(itemKey, Objects.requireNonNull(response.getBody()).getKey()); |
| 74 | + |
| 75 | + List<Commit> commitList = commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(app.getAppId(), cluster.getName(), namespace.getNamespaceName(), |
| 76 | + Pageable.ofSize(10)); |
| 77 | + Assert.assertEquals(1, commitList.size()); |
| 78 | + |
| 79 | + Commit commit = commitList.get(0); |
| 80 | + Assert.assertTrue(commit.getChangeSets().contains(itemKey)); |
| 81 | + Assert.assertTrue(commit.getChangeSets().contains(itemValue)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) |
| 86 | + @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) |
| 87 | + public void testUpdate() { |
| 88 | + this.testCreate(); |
| 89 | + |
| 90 | + String appId = "someAppId"; |
| 91 | + AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); |
| 92 | + assert app != null; |
| 93 | + ClusterDTO cluster = restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); |
| 94 | + assert cluster != null; |
| 95 | + NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), |
| 96 | + NamespaceDTO.class, app.getAppId(), cluster.getName(), "application"); |
| 97 | + |
| 98 | + String itemKey = "test-key"; |
| 99 | + String itemValue = "test-value-updated"; |
| 100 | + |
| 101 | + long itemId = itemRepository.findByKey(itemKey, Pageable.ofSize(1)) |
| 102 | + .getContent() |
| 103 | + .get(0) |
| 104 | + .getId(); |
| 105 | + ItemDTO item = new ItemDTO(itemKey, itemValue, "", 1); |
| 106 | + item.setDataChangeLastModifiedBy("apollo"); |
| 107 | + |
| 108 | + String updateUrl = url( "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}"); |
| 109 | + assert namespace != null; |
| 110 | + restTemplate.put(updateUrl, item, app.getAppId(), cluster.getName(), namespace.getNamespaceName(), itemId); |
| 111 | + |
| 112 | + itemRepository.findById(itemId).ifPresent(item1 -> { |
| 113 | + assertThat(item1.getValue()).isEqualTo(itemValue); |
| 114 | + assertThat(item1.getKey()).isEqualTo(itemKey); |
| 115 | + }); |
| 116 | + |
| 117 | + List<Commit> commitList = commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(app.getAppId(), cluster.getName(), namespace.getNamespaceName(), |
| 118 | + Pageable.ofSize(10)); |
| 119 | + assertThat(commitList).hasSize(2); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) |
| 124 | + @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) |
| 125 | + public void testDelete() { |
| 126 | + this.testCreate(); |
| 127 | + |
| 128 | + String appId = "someAppId"; |
| 129 | + AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); |
| 130 | + assert app != null; |
| 131 | + ClusterDTO cluster = restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); |
| 132 | + assert cluster != null; |
| 133 | + NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), |
| 134 | + NamespaceDTO.class, app.getAppId(), cluster.getName(), "application"); |
| 135 | + |
| 136 | + String itemKey = "test-key"; |
| 137 | + |
| 138 | + long itemId = itemRepository.findByKey(itemKey, Pageable.ofSize(1)) |
| 139 | + .getContent() |
| 140 | + .get(0) |
| 141 | + .getId(); |
| 142 | + |
| 143 | + String deleteUrl = url( "/items/{itemId}?operator=apollo"); |
| 144 | + restTemplate.delete(deleteUrl, itemId); |
| 145 | + assertThat(itemRepository.findById(itemId).isPresent()) |
| 146 | + .isFalse(); |
| 147 | + |
| 148 | + assert namespace != null; |
| 149 | + List<Commit> commitList = commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(app.getAppId(), cluster.getName(), namespace.getNamespaceName(), |
| 150 | + Pageable.ofSize(10)); |
| 151 | + assertThat(commitList).hasSize(2); |
| 152 | + } |
| 153 | +} |
0 commit comments