Skip to content

Commit 0fe74cf

Browse files
authored
Merge branch 'master' into eception
2 parents ccc55e4 + 5903691 commit 0fe74cf

File tree

5 files changed

+175
-44
lines changed

5 files changed

+175
-44
lines changed

apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/ItemController.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,9 @@ public ItemDTO create(@PathVariable("appId") String appId,
7979
}
8080
entity = itemService.save(entity);
8181
dto = BeanUtils.transform(ItemDTO.class, entity);
82-
83-
Commit commit = new Commit();
84-
commit.setAppId(appId);
85-
commit.setClusterName(clusterName);
86-
commit.setNamespaceName(namespaceName);
87-
commit.setChangeSets(new ConfigChangeContentBuilder().createItem(entity).build());
88-
commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
89-
commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
90-
commitService.save(commit);
82+
commitService.createCommit(appId, clusterName, namespaceName, new ConfigChangeContentBuilder().createItem(entity).build(),
83+
dto.getDataChangeLastModifiedBy()
84+
);
9185

9286
return dto;
9387
}
@@ -154,14 +148,7 @@ public ItemDTO update(@PathVariable("appId") String appId,
154148
itemDTO = BeanUtils.transform(ItemDTO.class, entity);
155149

156150
if (builder.hasContent()) {
157-
Commit commit = new Commit();
158-
commit.setAppId(appId);
159-
commit.setClusterName(clusterName);
160-
commit.setNamespaceName(namespaceName);
161-
commit.setChangeSets(builder.build());
162-
commit.setDataChangeCreatedBy(itemDTO.getDataChangeLastModifiedBy());
163-
commit.setDataChangeLastModifiedBy(itemDTO.getDataChangeLastModifiedBy());
164-
commitService.save(commit);
151+
commitService.createCommit(appId, clusterName, namespaceName, builder.build(), itemDTO.getDataChangeLastModifiedBy());
165152
}
166153

167154
return itemDTO;
@@ -178,14 +165,10 @@ public void delete(@PathVariable("itemId") long itemId, @RequestParam String ope
178165

179166
Namespace namespace = namespaceService.findOne(entity.getNamespaceId());
180167

181-
Commit commit = new Commit();
182-
commit.setAppId(namespace.getAppId());
183-
commit.setClusterName(namespace.getClusterName());
184-
commit.setNamespaceName(namespace.getNamespaceName());
185-
commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
186-
commit.setDataChangeCreatedBy(operator);
187-
commit.setDataChangeLastModifiedBy(operator);
188-
commitService.save(commit);
168+
commitService.createCommit(namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName(),
169+
new ConfigChangeContentBuilder().deleteItem(entity).build(), operator
170+
);
171+
189172
}
190173

191174
@GetMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

apollo-adminservice/src/test/resources/controller/cleanup.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ DELETE FROM "Cluster";
2020
DELETE FROM "App";
2121
DELETE FROM "NamespaceLock";
2222
DELETE FROM "ServerConfig";
23+
DELETE FROM "Commit";
2324

apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/CommitService.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ public CommitService(final CommitRepository commitRepository) {
3434
this.commitRepository = commitRepository;
3535
}
3636

37-
@Transactional
38-
public Commit save(Commit commit){
37+
public void createCommit(String appId, String clusterName, String namespaceName, String configChangeContent,
38+
String operator) {
39+
40+
Commit commit = new Commit();
3941
commit.setId(0);//protection
40-
return commitRepository.save(commit);
42+
commit.setAppId(appId);
43+
commit.setClusterName(clusterName);
44+
commit.setNamespaceName(namespaceName);
45+
commit.setChangeSets(configChangeContent);
46+
commit.setDataChangeCreatedBy(operator);
47+
commit.setDataChangeLastModifiedBy(operator);
48+
commitRepository.save(commit);
4149
}
4250

4351
public List<Commit> find(String appId, String clusterName, String namespaceName, Pageable page){

apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/ItemSetService.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.ctrip.framework.apollo.biz.service;
1818

1919
import com.ctrip.framework.apollo.biz.entity.Audit;
20-
import com.ctrip.framework.apollo.biz.entity.Commit;
2120
import com.ctrip.framework.apollo.biz.entity.Item;
2221
import com.ctrip.framework.apollo.biz.entity.Namespace;
2322
import com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder;
@@ -83,8 +82,8 @@ public ItemChangeSets updateSet(String appId, String clusterName,
8382
auditService.audit("ItemSet", null, Audit.OP.DELETE, operator);
8483
}
8584

86-
if (configChangeContentBuilder.hasContent()){
87-
createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(),
85+
if (configChangeContentBuilder.hasContent()) {
86+
commitService.createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(),
8887
changeSet.getDataChangeLastModifiedBy());
8988
}
9089

@@ -147,17 +146,4 @@ private void doCreateItems(List<ItemDTO> toCreateItems, Namespace namespace, Str
147146
}
148147
}
149148

150-
private void createCommit(String appId, String clusterName, String namespaceName, String configChangeContent,
151-
String operator) {
152-
153-
Commit commit = new Commit();
154-
commit.setAppId(appId);
155-
commit.setClusterName(clusterName);
156-
commit.setNamespaceName(namespaceName);
157-
commit.setChangeSets(configChangeContent);
158-
commit.setDataChangeCreatedBy(operator);
159-
commit.setDataChangeLastModifiedBy(operator);
160-
commitService.save(commit);
161-
}
162-
163149
}

0 commit comments

Comments
 (0)