-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the createCommit method (#4811)
* add tech-support-qq-4.png * Update README.md * Enhance the user experience in the scenario of submitting duplicate keys * Modify the key-value conflict exception prompt, adjust the code style * refactor(apollo-biz): Refactor the createCommit method. * refactor(apollo-biz): Add test cases related to ItemController. * refactor(apollo-biz): Add license * refactor(apollo-biz): Refactor the createCommit method. * refactor(apollo-biz): Add test cases related to ItemController. * refactor(apollo-biz): Add license * refactor(apollo-biz): Optimize code formatting --------- Co-authored-by: Jason Song <[email protected]>
- Loading branch information
Showing
5 changed files
with
175 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
.../src/test/java/com/ctrip/framework/apollo/adminservice/controller/ItemControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright 2023 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.adminservice.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.ctrip.framework.apollo.biz.entity.Commit; | ||
import com.ctrip.framework.apollo.biz.repository.CommitRepository; | ||
import com.ctrip.framework.apollo.biz.repository.ItemRepository; | ||
import com.ctrip.framework.apollo.common.dto.AppDTO; | ||
import com.ctrip.framework.apollo.common.dto.ClusterDTO; | ||
import com.ctrip.framework.apollo.common.dto.ItemDTO; | ||
import com.ctrip.framework.apollo.common.dto.NamespaceDTO; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase; | ||
|
||
/** | ||
* @author kl (http://kailing.pub) | ||
* @since 2023/3/21 | ||
*/ | ||
public class ItemControllerTest extends AbstractControllerTest { | ||
|
||
@Autowired | ||
private CommitRepository commitRepository; | ||
|
||
@Autowired | ||
private ItemRepository itemRepository; | ||
|
||
@Test | ||
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) | ||
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) | ||
public void testCreate() { | ||
String appId = "someAppId"; | ||
AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); | ||
assert app != null; | ||
ClusterDTO cluster = restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); | ||
assert cluster != null; | ||
NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), | ||
NamespaceDTO.class, app.getAppId(), cluster.getName(), "application"); | ||
|
||
String itemKey = "test-key"; | ||
String itemValue = "test-value"; | ||
ItemDTO item = new ItemDTO(itemKey, itemValue, "", 1); | ||
assert namespace != null; | ||
item.setNamespaceId(namespace.getId()); | ||
item.setDataChangeLastModifiedBy("apollo"); | ||
|
||
ResponseEntity<ItemDTO> response = restTemplate.postForEntity(itemBaseUrl(), | ||
item, ItemDTO.class, app.getAppId(), cluster.getName(), namespace.getNamespaceName()); | ||
Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
Assert.assertEquals(itemKey, Objects.requireNonNull(response.getBody()).getKey()); | ||
|
||
List<Commit> commitList = commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(app.getAppId(), cluster.getName(), namespace.getNamespaceName(), | ||
Pageable.ofSize(10)); | ||
Assert.assertEquals(1, commitList.size()); | ||
|
||
Commit commit = commitList.get(0); | ||
Assert.assertTrue(commit.getChangeSets().contains(itemKey)); | ||
Assert.assertTrue(commit.getChangeSets().contains(itemValue)); | ||
} | ||
|
||
@Test | ||
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) | ||
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) | ||
public void testUpdate() { | ||
this.testCreate(); | ||
|
||
String appId = "someAppId"; | ||
AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); | ||
assert app != null; | ||
ClusterDTO cluster = restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); | ||
assert cluster != null; | ||
NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), | ||
NamespaceDTO.class, app.getAppId(), cluster.getName(), "application"); | ||
|
||
String itemKey = "test-key"; | ||
String itemValue = "test-value-updated"; | ||
|
||
long itemId = itemRepository.findByKey(itemKey, Pageable.ofSize(1)) | ||
.getContent() | ||
.get(0) | ||
.getId(); | ||
ItemDTO item = new ItemDTO(itemKey, itemValue, "", 1); | ||
item.setDataChangeLastModifiedBy("apollo"); | ||
|
||
String updateUrl = url( "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}"); | ||
assert namespace != null; | ||
restTemplate.put(updateUrl, item, app.getAppId(), cluster.getName(), namespace.getNamespaceName(), itemId); | ||
|
||
itemRepository.findById(itemId).ifPresent(item1 -> { | ||
assertThat(item1.getValue()).isEqualTo(itemValue); | ||
assertThat(item1.getKey()).isEqualTo(itemKey); | ||
}); | ||
|
||
List<Commit> commitList = commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(app.getAppId(), cluster.getName(), namespace.getNamespaceName(), | ||
Pageable.ofSize(10)); | ||
assertThat(commitList).hasSize(2); | ||
} | ||
|
||
@Test | ||
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) | ||
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) | ||
public void testDelete() { | ||
this.testCreate(); | ||
|
||
String appId = "someAppId"; | ||
AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); | ||
assert app != null; | ||
ClusterDTO cluster = restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); | ||
assert cluster != null; | ||
NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), | ||
NamespaceDTO.class, app.getAppId(), cluster.getName(), "application"); | ||
|
||
String itemKey = "test-key"; | ||
|
||
long itemId = itemRepository.findByKey(itemKey, Pageable.ofSize(1)) | ||
.getContent() | ||
.get(0) | ||
.getId(); | ||
|
||
String deleteUrl = url( "/items/{itemId}?operator=apollo"); | ||
restTemplate.delete(deleteUrl, itemId); | ||
assertThat(itemRepository.findById(itemId).isPresent()) | ||
.isFalse(); | ||
|
||
assert namespace != null; | ||
List<Commit> commitList = commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(app.getAppId(), cluster.getName(), namespace.getNamespaceName(), | ||
Pageable.ofSize(10)); | ||
assertThat(commitList).hasSize(2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters