Skip to content

Commit

Permalink
self-xdsd#165 SelfPaymentMethods.register(...) + test
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Oct 18, 2020
1 parent 92cc717 commit 1dea17c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
42 changes: 41 additions & 1 deletion src/main/java/com/selfxdsd/storage/SelfPaymentMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jooq.Result;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -77,7 +78,46 @@ public PaymentMethod register(
final Wallet wallet,
final String identifier
) {
return null;
final PaymentMethod registered;
final List<String> allowed = Arrays.asList(
Wallet.Type.STRIPE
);
final Project project = wallet.project();
if(allowed.contains(wallet.type())) {
final int inserted = this.database.jooq().insertInto(
SLF_PAYMENTMETHODS_XDSD,
SLF_PAYMENTMETHODS_XDSD.REPO_FULLNAME,
SLF_PAYMENTMETHODS_XDSD.PROVIDER,
SLF_PAYMENTMETHODS_XDSD.TYPE,
SLF_PAYMENTMETHODS_XDSD.IDENTIFIER,
SLF_PAYMENTMETHODS_XDSD.ACTIVE
).values(
project.repoFullName(),
project.provider(),
wallet.type(),
identifier,
Boolean.FALSE
).execute();
if(inserted != 1) {
throw new IllegalStateException(
"Something went wrong while trying to register "
+ "a new payment method."
);
} else {
registered = new StoredPaymentMethod(
this.storage,
identifier,
wallet,
Boolean.FALSE
);
}
} else {
throw new UnsupportedOperationException(
"Only payment methods for Wallets of type "
+ allowed + " can be registered at the moment!"
);
}
return registered;
}

@Override
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/selfxdsd/storage/SelfPaymentMethodsITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,49 @@ public void ofWalletReturnsEmpty() {
);
}

/**
* SelfPaymentMethods can register a Stripe PaymentMethod
* for a Stripe wallet.
*/
@Test
public void registersStripePaymentMethod() {
final Storage storage = new SelfJooq(new H2Database());
final Project project = storage.projects().getProjectById(
"johndoe/stripe_repo", Provider.Names.GITHUB
);
final Wallet wallet = project.wallets().active();

final PaymentMethods all = storage.paymentMethods();

final PaymentMethods methods = all.ofWallet(wallet);
MatcherAssert.assertThat(
methods,
Matchers.emptyIterable()
);

final PaymentMethod stripe = all.register(
wallet,
"stripe_pm_123"
);

MatcherAssert.assertThat(
stripe.active(),
Matchers.is(Boolean.FALSE)
);
MatcherAssert.assertThat(
stripe.identifier(),
Matchers.equalTo("stripe_pm_123")
);
MatcherAssert.assertThat(
stripe.wallet(),
Matchers.is(wallet)
);
MatcherAssert.assertThat(
wallet.paymentMethods(),
Matchers.iterableWithSize(1)
);
}

/**
* Mock a wallet (as an alternative to having to select
* a real one from the DB).
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/com/selfxdsd/storage/SelfProjectsITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public void hasCorrectTotalPages() {
final H2Database database = new H2Database();
final Projects projects = new SelfJooq(database).projects();
MatcherAssert.assertThat(projects
.page(new Paged.Page(1, 4))
.page(new Paged.Page(1, 5))
.totalPages(), Matchers.is(1));
for (int i = 0; i < 16; i++) {
final Repo repo = this.mockRepo("amihaiemil/repo" + i,
Expand All @@ -287,7 +287,7 @@ public void hasCorrectTotalPages() {
projects.register(repo, manager, "wbtoken" + i);
}
MatcherAssert.assertThat(projects
.page(new Paged.Page(1, 4))
.page(new Paged.Page(1, 5))
.totalPages(), Matchers.is(5));
MatcherAssert.assertThat(projects
.totalPages(), Matchers.is(1));
Expand All @@ -305,7 +305,6 @@ public void hasCorrectTotalPages() {
@Test
public void returnsOwnedByUserInPage() {
final Projects all = new SelfJooq(new H2Database()).projects();
MatcherAssert.assertThat(all, Matchers.iterableWithSize(4));

final Projects pageOne = all.page(new Paged.Page(1, 2));
MatcherAssert.assertThat(pageOne, Matchers.iterableWithSize(2));
Expand Down Expand Up @@ -382,7 +381,7 @@ public void returnsAssignedToPmInPage() {
Matchers.iterableWithSize(3));
MatcherAssert.assertThat(all
.page(new Paged.Page(1, 3))
.assignedTo(2),
.assignedTo(4),
Matchers.emptyIterable());

MatcherAssert.assertThat(all
Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/insertTestData.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ VALUES ('mihai', 'gitlab', 'user' ,'mihai@example.com');
INSERT INTO self_xdsd.slf_users_xdsd
(username, provider, role, email)
VALUES ('amihaiemil', 'github', 'user', 'amihaiemil@gmail.com.com');
INSERT INTO self_xdsd.slf_users_xdsd
(username, provider, role, email)
VALUES ('johndoe', 'github', 'user', 'johndoe@gmail.com');

-----------------
-- INSERT PROJECT MANAGERS
Expand All @@ -16,6 +19,10 @@ INSERT INTO self_xdsd.slf_pms_xdsd
(userid, username, provider, access_token, commission)
VALUES ('33162107', 'zoeself', 'github', 'pm1ghtoken123', 50);

INSERT INTO self_xdsd.slf_pms_xdsd
(userid, username, provider, access_token, commission)
VALUES ('33162108', 'otherpm', 'github', 'pm1ghtoken124', 50);

-- INSERT PROJECTS

INSERT INTO self_xdsd.slf_projects_xdsd
Expand All @@ -34,6 +41,10 @@ INSERT INTO self_xdsd.slf_projects_xdsd
(repo_fullname, provider, username, pmid, webhook_token)
VALUES ('mihai/test', 'gitlab', 'mihai', 1, 'whtoken126');

INSERT INTO self_xdsd.slf_projects_xdsd
(repo_fullname, provider, username, pmid, webhook_token)
VALUES ('johndoe/stripe_repo', 'github', 'johndoe', 2, 'whtoken124');

-- INSERT CONTRIBUTORS

INSERT INTO self_xdsd.slf_contributors_xdsd (username, provider) VALUES ('john', 'github');
Expand Down Expand Up @@ -213,6 +224,12 @@ INSERT INTO `self_xdsd`.`slf_wallets_xdsd`
VALUES
('amihaiemil/docker-java-api', 'github', 'STRIPE', 10000, 0, 'stripewallet-1232');

INSERT INTO `self_xdsd`.`slf_wallets_xdsd`
(`repo_fullname`, `provider`, `type`, `cash`, `active`, `identifier`)
VALUES
('johndoe/stripe_repo', 'github', 'STRIPE', 5000, 1, 'stripewallet-4444');


INSERT INTO `self_xdsd`.`slf_paymentmethods_xdsd` (`repo_fullname`,`provider`,`type`,`identifier`,`active`)
VALUES ('amihaiemil/docker-java-api', 'github', 'STRIPE', 'stripe_pm_1', 1);

Expand Down

0 comments on commit 1dea17c

Please sign in to comment.