Skip to content
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

Experiment to try and make it clearer where the "contract" is defined #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion account-service/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# define the name of the application as its registered with eureka
spring:
application:
name: account-service
4 changes: 3 additions & 1 deletion account-service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="DEBUG"/>
<logger name="org.xpdojo.bank.cdc.account" level="DEBUG"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
Expand Down
4 changes: 3 additions & 1 deletion account-service/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.xpdojo.bank.cdc.account" level="DEBUG"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
public class AccountData {

private final Long accountNumber;
private final String accountDescription;
private final String description;
private final Double overdraftFacility;
private final Double balance;

public AccountData(@JsonProperty("accountNumber") final Long accountNumber,
@JsonProperty("description") final String accountDescription,
@JsonProperty("description") final String description,
@JsonProperty("overdraftFacility") final Amount overdraftFacility,
@JsonProperty("balance") final Amount balance) {
this.accountNumber = accountNumber;
this.accountDescription = accountDescription;
this.description = description;
this.overdraftFacility = overdraftFacility.getValue();
this.balance = balance.getValue();
}
Expand All @@ -28,8 +28,8 @@ public Long getAccountNumber() {
return accountNumber;
}

public String getAccountDescription() {
return accountDescription;
public String getDescription() {
return description;
}

public Double getOverdraftFacility() {
Expand All @@ -46,21 +46,21 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
AccountData that = (AccountData) o;
return Objects.equals(accountNumber, that.accountNumber) &&
Objects.equals(accountDescription, that.accountDescription) &&
Objects.equals(description, that.description) &&
Objects.equals(overdraftFacility, that.overdraftFacility) &&
Objects.equals(balance, that.balance);
}

@Override
public int hashCode() {
return Objects.hash(accountNumber, accountDescription, overdraftFacility, balance);
return Objects.hash(accountNumber, description, overdraftFacility, balance);
}

@Override
public String toString() {
return "AccountData{" +
"accountNumber=" + accountNumber +
", accountDescription='" + accountDescription + '\'' +
", description='" + description + '\'' +
", overdraftFacility=" + overdraftFacility +
", balance=" + balance +
'}';
Expand Down
1 change: 0 additions & 1 deletion atm-service/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# define the name of the application as its registered with eureka
spring:
application:
name: atm-service
3 changes: 2 additions & 1 deletion atm-service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.xpdojo.bank.cdc.atm" level="DEBUG"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void checkWeCanProcessTheAccountData(MockServer mockProvider) throws IOException

AccountData accountData = Jackson2ObjectMapperBuilder.json().build().readValue(response.getBody(), AccountData.class);
assertThat(accountData.getAccountNumber()).isEqualTo(30002468L);
assertThat(accountData.getAccountDescription()).isNotEmpty();
assertThat(accountData.getDescription()).isNotEmpty();
assertThat(accountData.getOverdraftFacility()).isEqualTo(23.0D);
assertThat(accountData.getBalance()).isEqualTo(1000.0D);
}
Expand Down
4 changes: 3 additions & 1 deletion atm-service/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="au.com.dius.pact" level="DEBUG"/>
<logger name="org.xpdojo.bank.cdc.atm" level="DEBUG"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
Expand Down
3 changes: 3 additions & 0 deletions discovery-service/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: discovery-service
3 changes: 2 additions & 1 deletion discovery-service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>

<root level="INFO">
Expand Down
1 change: 0 additions & 1 deletion mobile-banking-service/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# define the name of the application as its registered with eureka
spring:
application:
name: mobile-banking-service
3 changes: 2 additions & 1 deletion mobile-banking-service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.xpdojo.bank.cdc.mobile" level="DEBUG"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.xpdojo.bank.cdc.mobile.pact;

import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.RequestResponsePact;

import java.util.HashMap;
import java.util.Map;

interface Contract {

static RequestResponsePact accountBalanceContract(PactDslWithProvider builder) {
return builder
.uponReceiving("Request for all accounts")
.path("/accounts/30002468/balance")
.method("GET")
.willRespondWith()
.status(200)
.headers(expectedHeaders())
.body(expectedAccountsBody())
.toPact();
}

private static Map<String, String> expectedHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}

private static PactDslJsonBody expectedAccountsBody() {
return new PactDslJsonBody()
.id("accountNumber")
.stringType("description")
.object("overdraftFacility", valueObject())
.object("balance", valueObject())
.asBody();
}

private static DslPart valueObject() {
return new PactDslJsonBody().decimalType("value");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
Expand All @@ -18,68 +16,39 @@
import org.xpdojo.bank.cdc.mobile.domain.Account;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "account_provider")
public class MobileConsumerAccountSummaryPactTest {

private static final Logger LOG = LoggerFactory.getLogger(MobileConsumerAccountSummaryPactTest.class);
private static final Logger LOG = LoggerFactory.getLogger(MobileConsumerAccountSummaryPactTest.class);

@Pact(provider = "account_provider", consumer = "mobile_consumer")
public RequestResponsePact configureMockServer(PactDslWithProvider builder) {
return builder
.uponReceiving("Request for all accounts")
.path("/accounts/30002468/balance")
.method("GET")
.willRespondWith()
.status(200)
.headers(expectedHeaders())
.body(expectedAccountsBody())
.toPact();
}
@Pact(provider = "account_provider", consumer = "mobile_consumer")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Pact annotations are BS. Is there a way to do this programmatically within the test.

Annotations = bad news (generally) 🔥

public RequestResponsePact getContract(PactDslWithProvider builder) {
return Contract.accountBalanceContract(builder);
}

private Map<String, String> expectedHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
@Test
void checkWeCanProcessTheAccountData(MockServer mockProvider) throws IOException {
ResponseEntity<String> response = retrieveAccountData(mockProvider);

private PactDslJsonBody expectedAccountsBody() {
return new PactDslJsonBody()
.id("accountNumber")
.stringType("description")
.object("overdraftFacility", valueObject())
.object("balance", valueObject())
.asBody();
}
assertThat(response.getStatusCode().value()).isEqualTo(200);
assertThat(response.getHeaders().get("Content-Type")).contains("application/json");
assertThat(response.getBody()).as("Response body from the accounts service is expected to be populated").isNotEmpty();

private DslPart valueObject() {
return new PactDslJsonBody().decimalType("value");
}
LOG.info(response.getBody());

@Test
void checkWeCanProcessTheAccountData(MockServer mockProvider) throws IOException {
ResponseEntity<String> response = retrieveAccountData(mockProvider);
Account accountData = Jackson2ObjectMapperBuilder.json().build().readValue(response.getBody(), Account.class);
assertThat(accountData.getAccountNumber()).isNotZero();
assertThat(accountData.getDescription()).isNotEmpty();
assertThat(accountData.getOverdraftFacility()).isNotZero();
assertThat(accountData.getBalance()).isNotZero();
}

assertThat(response.getStatusCode().value()).isEqualTo(200);
assertThat(response.getHeaders().get("Content-Type")).contains("application/json");
assertThat(response.getBody()).as("Response body from the accounts service is expected to be populated").isNotEmpty();

LOG.info(response.getBody());

Account accountData = Jackson2ObjectMapperBuilder.json().build().readValue(response.getBody(), Account.class);
assertThat(accountData.getAccountNumber()).isNotZero();
assertThat(accountData.getDescription()).isNotEmpty();
assertThat(accountData.getOverdraftFacility()).isNotZero();
assertThat(accountData.getBalance()).isNotZero();
}

private ResponseEntity<String> retrieveAccountData(MockServer mockProvider) {
return new RestTemplate().getForEntity(mockProvider.getUrl() + "/accounts/30002468/balance", String.class);
}
private ResponseEntity<String> retrieveAccountData(MockServer mockProvider) {
return new RestTemplate().getForEntity(mockProvider.getUrl() + "/accounts/30002468/balance", String.class);
}

}
4 changes: 3 additions & 1 deletion mobile-banking-service/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
</appender>

<logger name="root" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.xpdojo.bank.cdc.mobile" level="DEBUG"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
Expand Down