This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added recurring expense with system test
- Loading branch information
1 parent
53ae760
commit 1852d34
Showing
14 changed files
with
458 additions
and
18 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
114 changes: 114 additions & 0 deletions
114
expenses-service-st/src/test/java/de/roamingthings/expenses/expense/ExpenseServiceST.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,114 @@ | ||
package de.roamingthings.expenses.expense; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.hasItems; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
/** | ||
* @author Alexander Sparkowsky [info@roamingthings.de] | ||
* @version 2017/04/10 | ||
*/ | ||
public class ExpenseServiceST { | ||
@Rule | ||
public RecurringExpenseClient client = new RecurringExpenseClient("http://localhost:8080/recurring_expenses"); | ||
|
||
@Test | ||
public void shouldCreateRecurringExpense() { | ||
final URI createdAccountUri1 = createAndAssertRecurringExpense( | ||
"Recurring Payment 1", | ||
"testpayment", | ||
"MEMBERSHIP", | ||
"YEARLY", | ||
BigDecimal.valueOf(1.23), | ||
"EUR", | ||
"Creditor 1", | ||
"Note 1" | ||
); | ||
final URI createdAccountUri2 = createAndAssertRecurringExpense( | ||
"Recurring Payment 2", | ||
"testpayment", | ||
"MEMBERSHIP", | ||
"MONTHLY", | ||
BigDecimal.valueOf(2.34), | ||
"USD", | ||
"Creditor 2", | ||
"Note 2" | ||
); | ||
final URI createdAccountUri3 = createAndAssertRecurringExpense( | ||
"Recurring Payment 3", | ||
"testpayment", | ||
"MEMBERSHIP", | ||
"WEEKLY", | ||
BigDecimal.valueOf(5.67), | ||
"GBP", | ||
"Creditor 3", | ||
"Note 3" | ||
); | ||
|
||
assertRecurringPaymentExist(createdAccountUri1, createdAccountUri2, createdAccountUri3); | ||
|
||
deletePaymentAccount(createdAccountUri1); | ||
deletePaymentAccount(createdAccountUri2); | ||
|
||
assertNotFound(createdAccountUri1); | ||
assertNotFound(createdAccountUri2); | ||
assertFound(createdAccountUri3); | ||
} | ||
|
||
private URI createAndAssertRecurringExpense( | ||
final String description, | ||
final String label, | ||
final String expenseType, | ||
final String recurringPeriod, | ||
final BigDecimal amount, | ||
final String currency, | ||
final String creditorName, | ||
final String note | ||
) { | ||
final URI uri = client.create(description, | ||
label, | ||
recurringPeriod, | ||
expenseType, | ||
amount, | ||
currency, | ||
creditorName, | ||
note); | ||
final RecurringExpense recurringExpense = client.retrieve(uri); | ||
|
||
assertThat(recurringExpense.getLabel(), is(label)); | ||
assertThat(recurringExpense.getExpenseType(), is(expenseType)); | ||
assertThat(recurringExpense.getRecurrencePeriod(), is(recurringPeriod)); | ||
assertThat(recurringExpense.getAmount(), is(amount)); | ||
assertThat(recurringExpense.getCurrency(), is(currency)); | ||
assertThat(recurringExpense.getCreditorName(), is(creditorName)); | ||
assertThat(recurringExpense.getNote(), is(note)); | ||
|
||
return uri; | ||
} | ||
|
||
private void assertRecurringPaymentExist(final URI... uris) { | ||
final List<URI> paymentAccounts = client.retrieve(); | ||
assertThat(paymentAccounts, hasItems(uris)); | ||
} | ||
|
||
private void deletePaymentAccount(URI uri) { | ||
client.delete(uri); | ||
} | ||
|
||
private void assertNotFound(URI uri) { | ||
assertNull(client.retrieve(uri)); | ||
} | ||
|
||
private void assertFound(URI uri) { | ||
assertNotNull(client.retrieve(uri)); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
expenses-service-st/src/test/java/de/roamingthings/expenses/expense/RecurringExpense.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,68 @@ | ||
package de.roamingthings.expenses.expense; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* @author Alexander Sparkowsky [info@roamingthings.de] | ||
* @version 2017/04/10 | ||
*/ | ||
public class RecurringExpense { | ||
private final String description; | ||
|
||
private final String label; | ||
|
||
private final String recurrencePeriod; | ||
|
||
private final String expenseType; | ||
|
||
private final BigDecimal amount; | ||
|
||
private final String currency; | ||
|
||
private final String creditorName; | ||
|
||
private final String note; | ||
|
||
public RecurringExpense(String description, String label, String recurrencePeriod, String expenseType, BigDecimal amount, String currency, String creditorName, String note) { | ||
this.description = description; | ||
this.label = label; | ||
this.recurrencePeriod = recurrencePeriod; | ||
this.expenseType = expenseType; | ||
this.amount = amount; | ||
this.currency = currency; | ||
this.creditorName = creditorName; | ||
this.note = note; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
public String getRecurrencePeriod() { | ||
return recurrencePeriod; | ||
} | ||
|
||
public String getExpenseType() { | ||
return expenseType; | ||
} | ||
|
||
public BigDecimal getAmount() { | ||
return amount; | ||
} | ||
|
||
public String getCurrency() { | ||
return currency; | ||
} | ||
|
||
public String getCreditorName() { | ||
return creditorName; | ||
} | ||
|
||
public String getNote() { | ||
return note; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...es-service-st/src/test/java/de/roamingthings/expenses/expense/RecurringExpenseClient.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,120 @@ | ||
package de.roamingthings.expenses.expense; | ||
|
||
import org.junit.rules.ExternalResource; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonObject; | ||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.math.BigDecimal; | ||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* @author Alexander Sparkowsky [info@roamingthings.de] | ||
* @version 2017/04/10 | ||
*/ | ||
public class RecurringExpenseClient extends ExternalResource { | ||
|
||
private final String baseURI; | ||
private Client client; | ||
private WebTarget baseTarget; | ||
|
||
public RecurringExpenseClient(String baseURI) { | ||
this.baseURI = baseURI; | ||
} | ||
|
||
|
||
@Override | ||
protected void before() throws Throwable { | ||
client = ClientBuilder.newClient(); | ||
|
||
baseTarget = client.target(baseURI); | ||
} | ||
|
||
public URI create( | ||
final String description, | ||
final String label, | ||
final String recurrencePeriod, | ||
final String expenseType, | ||
final BigDecimal amount, | ||
final String currency, | ||
final String creditorName, | ||
final String note | ||
) { | ||
final JsonObject expenseObject = Json.createObjectBuilder() | ||
.add("description", description) | ||
.add("label", label) | ||
.add("recurrencePeriod", recurrencePeriod) | ||
.add("expenseType", expenseType) | ||
.add("amount", amount) | ||
.add("currency", currency) | ||
.add("creditorName", creditorName) | ||
.add("note", note) | ||
.build(); | ||
|
||
final Response response = baseTarget | ||
.request() | ||
.post(Entity.json(expenseObject)); | ||
|
||
assertStatus(Response.Status.CREATED, response); | ||
|
||
final URI uri = response.getLocation(); | ||
assertNotNull(uri); | ||
|
||
return uri; | ||
} | ||
|
||
public List<URI> retrieve() { | ||
final Response response = baseTarget.request(MediaType.APPLICATION_JSON_TYPE).get(); | ||
assertStatus(Response.Status.OK, response); | ||
|
||
final JsonArray jsonArray = response.readEntity(JsonObject.class).getJsonObject("_embedded").getJsonArray("recurring_expenses"); | ||
assertThat(jsonArray, notNullValue()); | ||
return jsonArray.getValuesAs(JsonObject.class).stream() | ||
.map(o -> o.getJsonObject("_links").getJsonObject("self").getString("href")) | ||
.map(URI::create) | ||
.collect(toList()); | ||
} | ||
|
||
public RecurringExpense retrieve(URI uri) { | ||
final Response response = client.target(uri).request(MediaType.APPLICATION_JSON_TYPE).get(); | ||
if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) { | ||
return null; | ||
} | ||
assertStatus(Response.Status.OK, response); | ||
|
||
final JsonObject object = response.readEntity(JsonObject.class); | ||
|
||
return new RecurringExpense( | ||
object.getString("description"), | ||
object.getString("label"), | ||
object.getString("recurrencePeriod"), | ||
object.getString("expenseType"), | ||
object.getJsonNumber("amount").bigDecimalValue(), | ||
object.getString("currency"), | ||
object.getString("creditorName"), | ||
object.getString("note")); | ||
} | ||
|
||
|
||
public void delete(URI uri) { | ||
final Response response = client.target(uri).request().delete(); | ||
assertStatus(Response.Status.NO_CONTENT, response); | ||
} | ||
|
||
private void assertStatus(Response.Status expectedStatus, Response response) { | ||
assertThat(response.getStatus(), is(expectedStatus.getStatusCode())); | ||
} | ||
} |
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
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
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
9 changes: 9 additions & 0 deletions
9
...-service/src/main/java/de/roamingthings/expenses/business/expense/domain/ExpenseType.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,9 @@ | ||
package de.roamingthings.expenses.business.expense.domain; | ||
|
||
/** | ||
* @author Alexander Sparkowsky [info@roamingthings.de] | ||
* @version 2017/04/10 | ||
*/ | ||
public enum ExpenseType { | ||
MEMBERSHIP, SUBSCRIPTION, INSURANCE, FEE, TAX, | ||
} |
Oops, something went wrong.