Skip to content

Commit

Permalink
Merge pull request #207 from commercetools/docs_querying
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Oct 6, 2021
2 parents b00452d + 3ef2f7f commit 615f3c4
Show file tree
Hide file tree
Showing 71 changed files with 988 additions and 566 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ plugins {
}

configurations {
taglet
taglet {
resolutionStrategy.force("net.sourceforge.plantuml:plantuml:1.2021.10")
}
}
dependencies {
taglet 'com.commercetools.build.taglets:commercetools-taglets:2.1.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import static commercetools.utils.CommercetoolsTestUtils.assertEventually;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Consumer;

import com.commercetools.api.client.QueryUtils;
import com.commercetools.api.models.DomainResource;
import com.commercetools.api.models.PagedQueryResourceRequest;
import com.commercetools.api.models.ResourcePagedQueryResponse;
import com.commercetools.api.models.category.CategoryPagedQueryResponse;
import com.commercetools.api.models.common.BaseResource;
import com.commercetools.api.models.state.*;
import commercetools.cart.CartsFixtures;
import commercetools.cart_discount.CartDiscountFixtures;
import commercetools.category.CategoryFixtures;
Expand All @@ -36,9 +36,6 @@
import commercetools.utils.CommercetoolsTestUtils;
import commercetools.zone.ZoneFixtures;

import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.error.NotFoundException;

import org.assertj.core.api.Assertions;
import org.junit.Test;

Expand All @@ -47,36 +44,8 @@
*/
public class DeleteEverythingIntegrationTest {

final int concurrency = 30;
final BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(concurrency);
final ExecutorService threadPool = Executors.newFixedThreadPool(concurrency);

private void initWorkers() {
for (int i = 0; i < concurrency; i++) {
threadPool.execute(() -> {
try {
while (!threadPool.isTerminated()) {
Runnable runnable = blockingQueue.poll(10, TimeUnit.SECONDS);
if (runnable == null) {
break;
}
try {
runnable.run();
}
catch (NotFoundException ignored) {
}
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}

@Test
public void execute() {
initWorkers();
try {
deleteAllExtensions();
deleteAllOrderEdits();
Expand All @@ -95,132 +64,135 @@ public void execute() {
deleteAllProductDiscounts();
deleteAllCustomObjects();
deleteAllCustomers();
deleteAllStates();
deleteAllStores();
deleteAllChannels();
deleteAllCustomerGroups();
deleteAllTypes();
deleteAllZones();
deleteAllStates();
}
catch (Exception e) {
System.out.println(e);
}
threadPool.shutdown();
}

private <T extends PagedQueryResourceRequest<T, TResult>, TResult extends ResourcePagedQueryResponse<TResource>, TResource extends BaseResource> void deleteAllResources(
PagedQueryResourceRequest<T, TResult> request, Consumer<TResource> deleteFn) {
private <TMethod extends PagedQueryResourceRequest<TMethod, TResult>, TResult extends ResourcePagedQueryResponse<TElement>, TElement extends DomainResource<TElement>> void deleteAllResources(
PagedQueryResourceRequest<TMethod, TResult> request, Consumer<TElement> deleteFn) {

ApiHttpResponse<TResult> response = request.withLimit(100).withSort("id ASC").executeBlocking();

do {
List<TResource> results = response.getBody().getResults();
if (results.size() > 0) {
results.forEach(deleteFn);
String lastId = results.get(results.size() - 1).getId();
response = request.withLimit(100)
.withSort("id ASC")
.withWhere("id > :lastId")
.withPredicateVar("lastId", lastId)
.executeBlocking();
}
} while (response.getBody().getCount() >= response.getBody().getLimit());
QueryUtils.queryAll(request, list -> {
list.forEach(deleteFn);
}, 100).toCompletableFuture().join();
}

private void checkDepends(Runnable block) {
assertEventually(Duration.ofSeconds(60), Duration.ofMillis(1000), block);
}

private void deleteAllZones() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().zones().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().zones().get(),
(zone) -> ZoneFixtures.deleteZone(zone.getId(), zone.getVersion()));
}

private void deleteAllOrderEdits() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().orders().edits().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().orders().edits().get(),
(orderEdit) -> OrdersFixtures.deleteOrderEdit(orderEdit.getId(), orderEdit.getVersion()));
}

private void deleteAllOrders() {
checkDepends(() -> Assertions.assertThat(
CommercetoolsTestUtils.getProjectRoot().orders().edits().get().executeBlocking().getBody().getCount())
CommercetoolsTestUtils.getProjectApiRoot().orders().edits().get().executeBlocking().getBody().getCount())
.isZero());

deleteAllResources(CommercetoolsTestUtils.getProjectRoot().orders().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().orders().get(),
(order) -> OrdersFixtures.deleteOrder(order.getId(), order.getVersion()));
}

private void deleteAllCarts() {
checkDepends(() -> Assertions
.assertThat(
CommercetoolsTestUtils.getProjectRoot().orders().get().executeBlocking().getBody().getCount())
CommercetoolsTestUtils.getProjectApiRoot().orders().get().executeBlocking().getBody().getCount())
.isZero());
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().carts().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().carts().get(),
(cart) -> CartsFixtures.deleteCart(cart.getId(), cart.getVersion()));
}

private void deleteAllTypes() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().types().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().types().get(),
(type) -> TypeFixtures.deleteType(type.getId(), type.getVersion()));
}

private void deleteAllStores() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().stores().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().stores().get(),
(store) -> StoreFixtures.deleteStore(store.getId(), store.getVersion()));
}

private void deleteAllStates() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().states().get().withWhere("initial = false"),
QueryUtils.queryAll(CommercetoolsTestUtils.getProjectApiRoot().states().get(), states -> {
states.forEach(state -> {
if (state.getTransitions() != null) {
CommercetoolsTestUtils.getProjectApiRoot()
.states()
.withId(state.getId())
.post(StateUpdateBuilder.of()
.version(state.getVersion())
.actions(StateSetTransitionsActionBuilder.of().build())
.build())
.executeBlocking();
}
});
}, 100).toCompletableFuture().join();

deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().states().get().withWhere("builtIn = false"),
(state) -> StateFixtures.deleteState(state.getId(), state.getVersion()));
}

private void deleteAllShoppingLists() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().shoppingLists().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().shoppingLists().get(),
(shoppingList) -> ShoppingListFixtures.deleteShoppingList(shoppingList.getId(), shoppingList.getVersion()));
}

private void deleteAllShippingMethods() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().shippingMethods().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().shippingMethods().get(),
(shippingMethod) -> ShippingMethodFixtures.deleteShippingMethod(shippingMethod.getId(),
shippingMethod.getVersion()));
}

private void deleteAllExtensions() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().extensions().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().extensions().get(),
(extension) -> ExtensionFixtures.deleteExtension(extension.getId(), extension.getVersion()));
}

private void deleteAllCustomerGroups() {
checkDepends(() -> Assertions
.assertThat(
CommercetoolsTestUtils.getProjectRoot().customers().get().executeBlocking().getBody().getCount())
CommercetoolsTestUtils.getProjectApiRoot().customers().get().executeBlocking().getBody().getCount())
.isZero());
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().customerGroups().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().customerGroups().get(),
(customerGroup) -> CustomerGroupFixtures.deleteCustomerGroup(customerGroup.getId(),
customerGroup.getVersion()));
}

private void deleteAllCustomers() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().customers().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().customers().get(),
(customer) -> CustomerFixtures.deleteCustomer(customer.getId(), customer.getVersion()));
}

private void deleteAllCustomObjects() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().customObjects().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().customObjects().get(),
(customObject) -> CustomObjectFixtures.deleteCustomObject(customObject.getContainer(),
customObject.getKey(), customObject.getVersion()));
}

private void deleteAllChannels() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().channels().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().channels().get(),
(channel) -> ChannelFixtures.deleteChannel(channel.getId(), channel.getVersion()));
}

private void deleteAllCategories() {
CategoryPagedQueryResponse response;

do {
response = CommercetoolsTestUtils.getProjectRoot().categories().get().executeBlocking().getBody();
response = CommercetoolsTestUtils.getProjectApiRoot().categories().get().executeBlocking().getBody();
response.getResults().forEach(category -> {
CategoryFixtures.deleteCategory(category.getId(), category.getVersion());
});
Expand All @@ -229,49 +201,49 @@ private void deleteAllCategories() {

private void deleteAllCartDiscounts() {
checkDepends(() -> Assertions.assertThat(
CommercetoolsTestUtils.getProjectRoot().discountCodes().get().executeBlocking().getBody().getCount())
CommercetoolsTestUtils.getProjectApiRoot().discountCodes().get().executeBlocking().getBody().getCount())
.isZero());

deleteAllResources(CommercetoolsTestUtils.getProjectRoot().cartDiscounts().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().cartDiscounts().get(),
(cartDiscount) -> CartDiscountFixtures.deleteCartDiscount(cartDiscount.getId(), cartDiscount.getVersion()));
}

private void deleteAllInventories() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().inventory().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().inventory().get(),
(inventoryEntry) -> InventoryEntryFixtures.delete(inventoryEntry.getId()));
}

private void deleteAllProducts() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().products().get(), ProductFixtures::deleteProduct);
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().products().get(), ProductFixtures::deleteProduct);
}

private void deleteAllProductDiscounts() {
checkDepends(() -> Assertions
.assertThat(
CommercetoolsTestUtils.getProjectRoot().products().get().executeBlocking().getBody().getCount())
CommercetoolsTestUtils.getProjectApiRoot().products().get().executeBlocking().getBody().getCount())
.isZero());
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().productDiscounts().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().productDiscounts().get(),
(productDiscount) -> ProductDiscountFixtures.deleteProductDiscount(productDiscount.getId(),
productDiscount.getVersion()));
}

private void deleteAllProductTypes() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().productTypes().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().productTypes().get(),
(productType) -> ProductTypeFixtures.deleteProductType(productType.getId(), productType.getVersion()));
}

private void deleteAllReviews() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().reviews().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().reviews().get(),
(review) -> ReviewFixtures.delete(review.getId(), review.getVersion()));
}

private void deleteAllTaxCategories() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().taxCategories().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().taxCategories().get(),
(taxCategory) -> TaxCategoryFixtures.deleteTaxCategory(taxCategory.getId(), taxCategory.getVersion()));
}

private void deleteAllDiscountCodes() {
deleteAllResources(CommercetoolsTestUtils.getProjectRoot().discountCodes().get(),
deleteAllResources(CommercetoolsTestUtils.getProjectApiRoot().discountCodes().get(),
(discountCode) -> DiscountCodeFixtures.deleteDiscountCode(discountCode.getId(), discountCode.getVersion()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ExceptionTest {
@Test
public void testException() {
Assertions.assertThatExceptionOfType(NotFoundException.class)
.isThrownBy(() -> CommercetoolsTestUtils.getProjectRoot()
.isThrownBy(() -> CommercetoolsTestUtils.getProjectApiRoot()
.categories()
.withKey("unknown-category")
.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static ApiClient createApiClient() {
.scope("manage_project:" + CommercetoolsTestUtils.getProjectKey())
.build();

ApiClient apiClient = CommercetoolsTestUtils.getProjectRoot()
ApiClient apiClient = CommercetoolsTestUtils.getProjectApiRoot()
.apiClients()
.post(apiClientDraft)
.executeBlocking()
Expand All @@ -36,7 +36,7 @@ public static ApiClient createApiClient() {
}

public static ApiClient deleteApiClient(final String id) {
ApiClient apiClient = CommercetoolsTestUtils.getProjectRoot()
ApiClient apiClient = CommercetoolsTestUtils.getProjectApiRoot()
.apiClients()
.withId(id)
.delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void createAndDeleteById() {
@Test
public void getById() {
ApiClientFixtures.withApiClient(apiClient -> {
ApiClient queriedApiClient = CommercetoolsTestUtils.getProjectRoot()
ApiClient queriedApiClient = CommercetoolsTestUtils.getProjectApiRoot()
.apiClients()
.withId(apiClient.getId())
.get()
Expand All @@ -36,7 +36,7 @@ public void getById() {
@Test
public void query() {
ApiClientFixtures.withApiClient(apiClient -> {
ApiClientPagedQueryResponse response = CommercetoolsTestUtils.getProjectRoot()
ApiClientPagedQueryResponse response = CommercetoolsTestUtils.getProjectApiRoot()
.apiClients()
.get()
.withWhere("id=" + "\"" + apiClient.getId() + "\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class CartsFixtures {
public static Cart deleteCart(final String id, final Long version) {
Cart cart = CommercetoolsTestUtils.getProjectRoot()
Cart cart = CommercetoolsTestUtils.getProjectApiRoot()
.carts()
.withId(id)
.delete()
Expand Down Expand Up @@ -59,7 +59,7 @@ public static void withUpdateableCart(final UnaryOperator<Cart> operator) {
}

public static Cart createCart(final CartDraft cartDraft) {
Cart cart = CommercetoolsTestUtils.getProjectRoot().carts().post(cartDraft).executeBlocking().getBody();
Cart cart = CommercetoolsTestUtils.getProjectApiRoot().carts().post(cartDraft).executeBlocking().getBody();

Assert.assertNotNull(cart);
Assert.assertEquals(cart.getCountry(), cartDraft.getCountry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static CartDiscount createCartDiscount() {
.permyriad(10L)
.build();

List<CartDiscount> cartDiscounts = CommercetoolsTestUtils.getProjectRoot()
List<CartDiscount> cartDiscounts = CommercetoolsTestUtils.getProjectApiRoot()
.cartDiscounts()
.get()
.withWhere("sortOrder=\"0.41\"")
Expand All @@ -58,7 +58,7 @@ public static CartDiscount createCartDiscount() {
.stackingMode(StackingMode.STACKING)
.build();

CartDiscount cartDiscount = CommercetoolsTestUtils.getProjectRoot()
CartDiscount cartDiscount = CommercetoolsTestUtils.getProjectApiRoot()
.cartDiscounts()
.post(cartDiscountDraft)
.executeBlocking()
Expand All @@ -71,7 +71,7 @@ public static CartDiscount createCartDiscount() {
}

public static CartDiscount deleteCartDiscount(final String id, final Long version) {
CartDiscount deletedCartDiscount = CommercetoolsTestUtils.getProjectRoot()
CartDiscount deletedCartDiscount = CommercetoolsTestUtils.getProjectApiRoot()
.cartDiscounts()
.withId(id)
.delete()
Expand Down
Loading

0 comments on commit 615f3c4

Please sign in to comment.