Last active
August 18, 2023 14:48
-
-
Save aevans-mms/b89fbcafb737be13c7508f92b7027ff2 to your computer and use it in GitHub Desktop.
User Entity and Builder examples
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
package ucc.models; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.github.javafaker.Faker; | |
import io.restassured.response.Response; | |
import org.mms.qa.EmailBuilder; | |
import org.testng.annotations.Test; | |
import ucc.api.exp.myaccount.MyAccountApi; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
class User { | |
public String firstName; | |
public String lastName; | |
public String emailAddress; | |
//... | |
} | |
class RegisteredUser extends User { | |
public String ucid; | |
//... | |
} | |
class Resident extends User { | |
public String residencyYear; | |
//... | |
} | |
class UserBuilder { | |
User instance; | |
public UserBuilder() { | |
instance = generateDefaultUser(); | |
} | |
public User generateDefaultUser() { | |
instance = new User(); | |
instance.firstName = "First"; | |
instance.lastName = "Last"; | |
instance.emailAddress = EmailBuilder.generateEmailAddress(instance.firstName); | |
//... | |
return instance; | |
} | |
public Resident generateResident() { | |
Resident resident = (Resident) generateDefaultUser(); | |
resident.residencyYear = "Current Year"; | |
//... | |
return resident; | |
} | |
public UserBuilder forResident() { | |
instance = generateResident(); | |
return this; | |
} | |
public UserBuilder withFirstName(String firstName) { | |
instance.firstName = firstName; | |
} | |
public UserBuilder withEmailDomain(String domain) { | |
instance.emailAddress = new EmailBuilder().withDomain(domain).generate(); | |
return this; | |
} | |
public User build() { | |
return instance; | |
} | |
} | |
class CustomerRegistrationJson { | |
@JsonProperty("firstName") | |
public String fName; // to simulate need to transform | |
//... other properties | |
public CustomerRegistrationJson fromUser(User user) { | |
fName = user.firstName; | |
//... transform | |
return this; | |
} | |
public String toJson() throws JsonProcessingException { | |
ObjectMapper mapper = new ObjectMapper(); | |
return mapper.writeValueAsString(this); | |
} | |
} | |
class CustomerRegistrationResponseJson { | |
/// etc. | |
} | |
class UseUserBuilder { | |
@Test | |
public void testUsingUserBuilder() throws JsonProcessingException { | |
Faker faker = new Faker(); | |
String firstName = faker.name().firstName(); | |
User user = new UserBuilder().forResident().withEmailDomain("example.com").withFirstName(firstName).build(); | |
CustomerRegistrationJson customerRegistrationJson = new CustomerRegistrationJson().fromUser(user); | |
MyAccountApi myAccountApi = new MyAccountApi(); | |
Response myAccountResponse = myAccountApi.buildRequest("customers/registration/{product}") | |
.pathParam("product", "Resident360") | |
.body(customerRegistrationJson.toJson()) | |
.post(); | |
assertEquals(200, myAccountResponse.getStatusCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment