Skip to content

Commit

Permalink
Verify only valid customers are able storable #13 fixes-#11
Browse files Browse the repository at this point in the history
Create Integration test for verifying only valid customers are storable
  • Loading branch information
mossabdeh authored Oct 27, 2023
2 parents cab9549 + 82040f3 commit 976009a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/dev/deh/rys/entity/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import io.jmix.core.metamodel.annotation.JmixEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.NotBlank;

@JmixEntity(name = "rys_Address")
@Embeddable
public class Address {
@NotBlank
@Column(name = "STREET", nullable = false)
@NotNull
private String street;

@Column(name = "POST_CODE")
Expand Down
91 changes: 91 additions & 0 deletions src/test/java/dev/deh/rys/customer/CustomerIntegrationTest.java
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.

}

0 comments on commit 976009a

Please sign in to comment.