-
Notifications
You must be signed in to change notification settings - Fork 39
fix: enable component client for integration tests #1744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,24 +1,26 @@ | ||
package customer.api; | ||
|
||
|
||
import com.google.protobuf.any.Any; | ||
import customer.Main; | ||
import customer.api.CustomerEntity.Confirm; | ||
import customer.domain.Address; | ||
import customer.domain.Customer; | ||
import customer.view.CustomerView; | ||
import kalix.javasdk.DeferredCall; | ||
import kalix.spring.testkit.KalixIntegrationTestKitSupport; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Duration; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static java.time.temporal.ChronoUnit.SECONDS; | ||
import static org.awaitility.Awaitility.await; | ||
|
@@ -27,7 +29,6 @@ | |
@SpringBootTest(classes = Main.class) | ||
public class CustomerIntegrationTest extends KalixIntegrationTestKitSupport { | ||
|
||
|
||
@Autowired | ||
private WebClient webClient; | ||
|
||
|
@@ -38,15 +39,11 @@ public void create() { | |
String id = UUID.randomUUID().toString(); | ||
Customer customer = new Customer("[email protected]", "Johanna", null); | ||
|
||
ResponseEntity<CustomerEntity.Confirm> response = | ||
webClient.post() | ||
.uri("/customer/" + id + "/create") | ||
.bodyValue(customer) | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
Confirm response = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::create) | ||
.params(customer)); | ||
|
||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, response); | ||
Assertions.assertEquals("Johanna", getCustomerById(id).name()); | ||
} | ||
|
||
|
@@ -55,25 +52,18 @@ public void changeName() { | |
String id = UUID.randomUUID().toString(); | ||
Customer customer = new Customer("[email protected]", "Johanna", null); | ||
|
||
ResponseEntity<CustomerEntity.Confirm> resCreation = | ||
webClient.post() | ||
.uri("/customer/" + id + "/create") | ||
.body(Mono.just(customer), Customer.class) | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
Confirm response = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::create) | ||
.params(customer)); | ||
|
||
Assertions.assertEquals(HttpStatus.OK, resCreation.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, response); | ||
|
||
ResponseEntity<CustomerEntity.Confirm> resUpdate = | ||
webClient.post() | ||
.uri("/customer/" + id + "/changeName/" + "Katarina") | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
Confirm resUpdate = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::changeName) | ||
.params("Katarina")); | ||
|
||
|
||
Assertions.assertEquals(HttpStatus.OK, resUpdate.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, resUpdate); | ||
Assertions.assertEquals("Katarina", getCustomerById(id).name()); | ||
} | ||
|
||
|
@@ -82,27 +72,19 @@ public void changeAddress() { | |
String id = UUID.randomUUID().toString(); | ||
Customer customer = new Customer("[email protected]", "Johanna", null); | ||
|
||
ResponseEntity<CustomerEntity.Confirm> resCreation = | ||
webClient.post() | ||
.uri("/customer/" + id + "/create") | ||
.body(Mono.just(customer), Customer.class) | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
Confirm response = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::create) | ||
.params(customer)); | ||
|
||
Assertions.assertEquals(HttpStatus.OK, resCreation.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, response); | ||
|
||
Address address = new Address("Elm st. 5", "New Orleans"); | ||
ResponseEntity<CustomerEntity.Confirm> resUpdate = | ||
webClient.post() | ||
.uri("/customer/" + id + "/changeAddress") | ||
.body(Mono.just(address), Address.class) | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
|
||
Confirm resUpdate = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::changeAddress) | ||
.params(address)); | ||
|
||
Assertions.assertEquals(HttpStatus.OK, resUpdate.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, resUpdate); | ||
Assertions.assertEquals("Elm st. 5", getCustomerById(id).address().street()); | ||
} | ||
|
||
|
@@ -111,23 +93,19 @@ public void changeAddress() { | |
public void findByName() { | ||
String id = UUID.randomUUID().toString(); | ||
Customer customer = new Customer("[email protected]", "Foo", null); | ||
ResponseEntity<CustomerEntity.Confirm> response = | ||
webClient.post() | ||
.uri("/customer/" + id + "/create") | ||
.bodyValue(customer) | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
Confirm response = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::create) | ||
.params(customer)); | ||
|
||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, response); | ||
|
||
// the view is eventually updated | ||
await() | ||
.ignoreExceptions() | ||
.atMost(20, TimeUnit.SECONDS) | ||
.until(() -> | ||
webClient.get() | ||
.uri("/customer/by_name/Foo") | ||
.ignoreExceptions() | ||
.atMost(20, TimeUnit.SECONDS) | ||
.until(() -> | ||
webClient.get() | ||
.uri("/customer/by_name/Foo") | ||
.retrieve() | ||
.bodyToMono(CustomerView.class) | ||
.block(timeout) | ||
|
@@ -140,38 +118,38 @@ public void findByName() { | |
public void findByEmail() { | ||
String id = UUID.randomUUID().toString(); | ||
Customer customer = new Customer("[email protected]", "Bar", null); | ||
ResponseEntity<CustomerEntity.Confirm> response = | ||
webClient.post() | ||
.uri("/customer/" + id + "/create") | ||
.bodyValue(customer) | ||
.retrieve() | ||
.toEntity(CustomerEntity.Confirm.class) | ||
.block(timeout); | ||
Confirm response = execute(componentClient.forEventSourcedEntity(id) | ||
.call(CustomerEntity::create) | ||
.params(customer)); | ||
|
||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
Assertions.assertEquals(Confirm.done, response); | ||
|
||
// the view is eventually updated | ||
await() | ||
.ignoreExceptions() | ||
.atMost(20, TimeUnit.SECONDS) | ||
.until(() -> | ||
webClient.get() | ||
.uri("/customer/by_email/[email protected]") | ||
.retrieve() | ||
.bodyToMono(CustomerView.class) | ||
.block(timeout) | ||
.name(), | ||
new IsEqual("Bar") | ||
); | ||
.ignoreExceptions() | ||
.atMost(20, TimeUnit.SECONDS) | ||
.until(() -> | ||
webClient.get() | ||
.uri("/customer/by_email/[email protected]") | ||
.retrieve() | ||
.bodyToMono(CustomerView.class) | ||
.block(timeout) | ||
.name(), | ||
new IsEqual("Bar") | ||
); | ||
} | ||
|
||
private Customer getCustomerById(String customerId) { | ||
return webClient | ||
.get() | ||
.uri("/customer/" + customerId) | ||
.retrieve() | ||
.bodyToMono(Customer.class) | ||
.block(timeout); | ||
return execute(componentClient.forEventSourcedEntity(customerId) | ||
.call(CustomerEntity::getCustomer)); | ||
} | ||
|
||
protected <T> T execute(DeferredCall<Any, T> deferredCall) { | ||
try { | ||
return deferredCall.execute().toCompletableFuture().get(timeout.toMillis(), TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException | ExecutionException | TimeoutException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.