-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create Integration test for verifying only valid customers are storable
- Loading branch information
Showing
2 changed files
with
93 additions
and
2 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
91 changes: 91 additions & 0 deletions
91
src/test/java/dev/deh/rys/customer/CustomerIntegrationTest.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,91 @@ | ||
package dev.deh.rys.customer; | ||
|
||
import dev.deh.rys.entity.Address; | ||
import io.jmix.core.DataManager; | ||
import io.jmix.core.security.SystemAuthenticator; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.groups.Default; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
|
||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
class CustomerIntegrationTest { | ||
|
||
@Autowired | ||
DataManager dataManager; | ||
|
||
@Autowired | ||
SystemAuthenticator systemAuthenticator; | ||
|
||
@Autowired | ||
Validator validator; | ||
|
||
private Customer customer; | ||
@BeforeEach | ||
void setUp() { | ||
customer = dataManager.create(Customer.class); | ||
} | ||
|
||
@Test | ||
void given_validCustomer_when_saveCustomer_then_customerIsSaved() { | ||
|
||
|
||
|
||
// given | ||
customer.setFirstName("Foo"); | ||
customer.setLastName("Bar"); | ||
customer.setEmail("foo@bar.com"); | ||
|
||
Address address = dataManager.create(Address.class); | ||
address.setStreet("Foo Street 1"); | ||
address.setCity("Bar"); | ||
address.setPostCode("25001"); | ||
|
||
customer.setAddress(address); | ||
|
||
// when | ||
Customer savedCustomer = systemAuthenticator.withSystem(() -> { | ||
return dataManager.save(customer); | ||
}); | ||
// then | ||
assertThat(savedCustomer.getId()) | ||
.isNotNull(); | ||
|
||
} | ||
|
||
@Test | ||
void given_customerWithInvalidEmail_when_validateCustomer_then_customerIsInvalid() { | ||
// given | ||
customer.setEmail("invalidEmailAddress"); | ||
|
||
// when | ||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer, Default.class); | ||
|
||
// then | ||
assertThat(violations) | ||
.hasSize(2);// Expect 2 violations (lastName and email) | ||
|
||
// and | ||
assertThat(firstViolation(violations).getPropertyPath().toString()) | ||
.isEqualTo("email"); | ||
assertThat(firstViolation(violations).getMessageTemplate()) | ||
.isEqualTo("{jakarta.validation.constraints.Email.message}"); | ||
|
||
|
||
|
||
|
||
} | ||
|
||
private static ConstraintViolation<Customer> firstViolation(Set<ConstraintViolation<Customer>> violations) { | ||
return violations.stream().skip(1).findFirst().orElseThrow(); | ||
} // We skip(1) to specifically target the email violation, excluding the lastName violation. | ||
|
||
} |