Skip to content

Commit

Permalink
test(apollo-biz): Optimize the test case for ItemSetControllerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
klboke committed Mar 29, 2023
1 parent 541bccf commit 53328db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ public abstract class AbstractControllerTest {
private HttpMessageConverters httpMessageConverters;

protected RestTemplate restTemplate = (new TestRestTemplate()).getRestTemplate();
protected RestTemplate noErrorHandlerRestTemplate = (new TestRestTemplate()).getRestTemplate();

@PostConstruct
private void postConstruct() {
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
restTemplate.setMessageConverters(httpMessageConverters.getConverters());
noErrorHandlerRestTemplate.setMessageConverters(httpMessageConverters.getConverters());
}

@Value("${local.server.port}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;

import com.ctrip.framework.apollo.common.exception.BadRequestException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -94,9 +95,14 @@ public void testItemSetCreatedWithInvalidNamespaceId() {
ItemChangeSets itemSet = mockCreateItemChangeSets(namespace, createdSize);
itemSet.getCreateItems().get(createdSize - 1).setNamespaceId(someNamespaceId);

ResponseEntity<Void> response =
noErrorHandlerRestTemplate.postForEntity(itemSetBaseUrl(), itemSet, Void.class, appId, clusterName, namespaceName);
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
try {
ResponseEntity<Void> response =
restTemplate.postForEntity(itemSetBaseUrl(), itemSet, Void.class, appId, clusterName, namespaceName);
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("invalid request, item and namespace do not match!"));
Assert.assertTrue(e.getMessage().contains(BadRequestException.class.getName()));
}
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(someNamespaceId);
Assert.assertEquals(0, items.size());
}
Expand Down Expand Up @@ -186,9 +192,15 @@ public void testItemSetUpdatedWithInvalidNamespaceId() {
updateChangeSet.addUpdateItem(items[i]);
}

response = noErrorHandlerRestTemplate.postForEntity(itemSetBaseUrl(),
updateChangeSet, Void.class, appId, clusterName, someNamespaceName);
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
try {
response = restTemplate.postForEntity(itemSetBaseUrl(),
updateChangeSet, Void.class, appId, clusterName, someNamespaceName);
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("invalid request, item and namespace do not match!"));
Assert.assertTrue(e.getMessage().contains(BadRequestException.class.getName()));
}

List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(someNamespace.getId());
Assert.assertEquals(0, savedItems.size());
}
Expand Down Expand Up @@ -289,9 +301,15 @@ public void testItemSetDeletedWithInvalidNamespaceId() {
deleteChangeSet.addDeleteItem(items[i]);
}

response = noErrorHandlerRestTemplate.postForEntity(itemSetBaseUrl(),
deleteChangeSet, Void.class, appId, clusterName, someNamespaceName);
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
try {
response = restTemplate.postForEntity(itemSetBaseUrl(),
deleteChangeSet, Void.class, appId, clusterName, someNamespaceName);
Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("invalid request, item and namespace do not match!"));
Assert.assertTrue(e.getMessage().contains(BadRequestException.class.getName()));
}

List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
Assert.assertEquals(createdSize, savedItems.size());
}
Expand Down

0 comments on commit 53328db

Please sign in to comment.