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

Propose new api for context encapsulation #26

Draft
wants to merge 4 commits into
base: release/1.5
Choose a base branch
from
Draft
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
21 changes: 19 additions & 2 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
object Vers {
//Plugins
val asciidoctor = "1.5.9.2"
val kotlin = "1.3.41"
val kotlin_coroutines = "1.2.2"
val kotlin = "1.3.61"
val kotlin_coroutines = "1.3.3"

val sl4j = "1.7.25"
val dokka = "0.9.18"
Expand Down Expand Up @@ -63,4 +63,21 @@ object Libs {

val hamcrest = "org.hamcrest:hamcrest-all:1.3"
val hamkrest = "com.natpryce:hamkrest:1.7.0.0"
}

enum class Proj{
`completable-reactor-example`,
`completable-reactor-graph`,
`completable-reactor-graph-kotlin`,
`completable-reactor-graph-viewer`,
`completable-reactor-graph-viewer-app`,
`completable-reactor-jmh`,
`completable-reactor-model`,
`completable-reactor-parser`,
`completable-reactor-plugin-idea`,
`completable-reactor-runtime`,
`completable-reactor-spring`,
`completable-reactor-test-utils`;

val asDependency get(): String = ":$name"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import ru.fix.completable.reactor.example.services.*;
import ru.fix.completable.reactor.graph.Graph;
import ru.fix.completable.reactor.graph.JGraph;
import ru.fix.completable.reactor.graph.Vertex;
import ru.fix.completable.reactor.runtime.CompletableReactor;

Expand All @@ -15,7 +15,45 @@
* # Defines purchase process when user buys good in the shop.
*/
@Configuration
public class PurchaseGraph extends Graph<PurchasePayload> {
public class PurchaseGraph extends JGraph<
PurchaseSubmit,
PurchaseSubmit.Request,
PurchaseSubmit.Response,
PurchaseGraph.Context> {

class Context {
ServiceInfo serviceInfo;
UserProfile userInfo;
Long bonusService;

public ServiceInfo getServiceInfo() {
return serviceInfo;
}

public Context setServiceInfo(ServiceInfo serviceInfo) {
this.serviceInfo = serviceInfo;
return this;
}

public UserProfile getUserInfo() {
return userInfo;
}

public Context setUserInfo(UserProfile userInfo) {
this.userInfo = userInfo;
return this;
}

public Long getBonusService() {
return bonusService;
}

public Context setBonusService(Long bonusService) {
this.bonusService = bonusService;
return this;
}
}


@Autowired
UserProfileManager userProfileManager;
Expand Down Expand Up @@ -67,7 +105,7 @@ public void initialize() {
return Flow.STOP;

case OK:
pld.intermediateData.setUserInfo(result.userProfile);
pld.context.setUserInfo(result.userProfile);
return Flow.CONTINUE;
}
throw new IllegalArgumentException("result.status = " + result.status);
Expand Down Expand Up @@ -105,8 +143,8 @@ Vertex logTransaction() {
User will end up with negative balance after this operation.
*/
pld -> bank.withdrawMoneyWithMinus(
pld.intermediateData.getUserInfo(),
pld.intermediateData.getServiceInfo())
pld.context.getUserInfo(),
pld.context.getServiceInfo())
).withRoutingMerger(
/*
# CheckWithdraw
Expand Down Expand Up @@ -138,7 +176,7 @@ Vertex logTransaction() {
Update payload response.
*/
pld -> {
if (pld.intermediateData.serviceInfo.isPartnerService()) {
if (pld.context.serviceInfo.isPartnerService()) {
pld.response.isPartnerService = true;
}
});
Expand All @@ -163,7 +201,7 @@ Vertex logTransaction() {
pld.response.setStatus(result.getStatus());
return Flow.STOP;
case OK:
pld.intermediateData.setServiceInfo(result.getServiceInfo());
pld.context.setServiceInfo(result.getServiceInfo());

if (result.getServiceInfo().isActive()) {
return Flow.WITHDRAWAL;
Expand All @@ -180,7 +218,7 @@ Vertex logTransaction() {
Optional<Long> bonus = marketingService.checkBonuses(pld.request.userId, pld.request.serviceId);

if (bonus.isPresent()) {
pld.intermediateData.bonusService = bonus.get();
pld.context.bonusService = bonus.get();
return Flow.BONUS_EXIST;
} else {
return Flow.NO_BONUS;
Expand All @@ -190,17 +228,17 @@ Vertex logTransaction() {
Vertex bonusPurchaseSubgraph =
subgraph(
//# Make bonus purchase
PurchasePayload.class,
PurchaseSubmit.class,
pld -> {
PurchasePayload subgraphRequest = new PurchasePayload();
subgraphRequest.request
PurchaseSubmit.Request request = new PurchaseSubmit.Request();
request
.setServiceId(107L)
.setUserId(pld.request.userId);
return subgraphRequest;
return request;
}

).withMerger((pld, subgraphResult) ->
pld.response.bonusServiceStatus = subgraphResult.response.status
).withMerger((pld, subgraphResponse) ->
pld.response.bonusServiceStatus = subgraphResponse.status
);

{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package ru.fix.completable.reactor.example;


import ru.fix.completable.reactor.example.services.ServiceInfo;
import ru.fix.completable.reactor.example.services.UserProfile;
import ru.fix.completable.reactor.graph.Submit;

import java.math.BigDecimal;

/**
* @author Kamil Asfandiyarov
*/
public class PurchasePayload {
public interface PurchaseSubmit extends Submit<PurchaseSubmit.Request, PurchaseSubmit.Response> {

public static class Request {
class Request {
Long userId;
Long serviceId;

Expand All @@ -34,7 +33,7 @@ public Request setServiceId(Long serviceId) {
}
}

public static class Response {
class Response {
Enum status;
BigDecimal newAmount;
boolean withdrawalWasInMinus;
Expand Down Expand Up @@ -86,44 +85,4 @@ public Response setPartnerService(boolean partnerService) {
return this;
}
}

public static class IntermediateData {
ServiceInfo serviceInfo;
UserProfile userInfo;
Long bonusService;

public ServiceInfo getServiceInfo() {
return serviceInfo;
}

public IntermediateData setServiceInfo(ServiceInfo serviceInfo) {
this.serviceInfo = serviceInfo;
return this;
}

public UserProfile getUserInfo() {
return userInfo;
}

public IntermediateData setUserInfo(UserProfile userInfo) {
this.userInfo = userInfo;
return this;
}

public Long getBonusService() {
return bonusService;
}

public IntermediateData setBonusService(Long bonusService) {
this.bonusService = bonusService;
return this;
}


}

public final Request request = new Request();
public final IntermediateData intermediateData = new IntermediateData();
public final Response response = new Response();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
import ru.fix.completable.reactor.example.services.*
import ru.fix.completable.reactor.example.services.UserProfileManager.Status
import ru.fix.completable.reactor.graph.kotlin.Graph
import ru.fix.completable.reactor.graph.kotlin.KGraph
import ru.fix.completable.reactor.runtime.CompletableReactor
import javax.annotation.PostConstruct

Expand All @@ -14,7 +14,17 @@ import javax.annotation.PostConstruct
* Created by Kamil Asfandiyarov
*/
@Configuration
open class SubscribeGraph : Graph<SubscribePayload>() {
open class SubscribeGraph : KGraph<
SubscribeSubmit,
SubscribeSubmit.Request,
SubscribeSubmit.Response,
SubscribeGraph.Context>() {

class Context {
lateinit var serviceInfo: ServiceInfo
lateinit var accountInfo: AccountInfo
lateinit var userInfo: UserProfile
}

@Autowired
lateinit var userProfile: UserProfileManager
Expand Down Expand Up @@ -63,7 +73,7 @@ open class SubscribeGraph : Graph<SubscribePayload>() {
}

Status.OK -> {
intermediateData.userInfo = it.userProfile
context.userInfo = it.userProfile
Flow.CONTINUE
}

Expand Down Expand Up @@ -114,8 +124,8 @@ open class SubscribeGraph : Graph<SubscribePayload>() {
val withdrawMoney =
handler {
bank.withdrawMoney(
intermediateData.userInfo,
intermediateData.serviceInfo)
context.userInfo,
context.serviceInfo)

}.withRoutingMerger {
//update new amount
Expand Down Expand Up @@ -145,7 +155,7 @@ open class SubscribeGraph : Graph<SubscribePayload>() {
Flow.STOP
}
ServiceRegistry.Status.OK -> {
intermediateData.serviceInfo = result.serviceInfo
context.serviceInfo = result.serviceInfo

if (result.serviceInfo.isActive) {
Flow.CONTINUE
Expand All @@ -160,24 +170,23 @@ open class SubscribeGraph : Graph<SubscribePayload>() {

val checkTrialPeriod = router {
// Checks whether service supports trial period
if (intermediateData.serviceInfo.isSupportTrialPeriod) {
if (context.serviceInfo.isSupportTrialPeriod) {
Flow.NO_WITHDRAWAL
} else {
Flow.WITHDRAWAL
}
}

val bonusPurchaseSubgraph =
subgraph(PurchasePayload::class) {
subgraph(PurchaseSubmit::class) {
//# Make bonus purchase
PurchasePayload().apply {
request
.setServiceId(107L)
.setUserId(request.userId)
PurchaseSubmit.Request().apply {
setServiceId(107L)
setUserId(request.userId)
}

}.withMerger {
response.bonusPurchaseStatus = it.response.status
response.bonusPurchaseStatus = it.status
Flow.CONTINUE
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package ru.fix.completable.reactor.example;

import ru.fix.completable.reactor.example.services.ServiceInfo;
import ru.fix.completable.reactor.example.services.UserProfile;
import ru.fix.completable.reactor.example.services.AccountInfo;
import ru.fix.completable.reactor.graph.Submit
import java.math.BigDecimal

/**
* Created by swarmshine on 16.10.2016.
*/
data class SubscribePayload(val request: Request) {

val intermediateData = IntermediateData()
val response = Response()

interface SubscribeSubmit: Submit<SubscribeSubmit.Request, SubscribeSubmit.Response> {
data class Request(
val userId: Long,
val serviceId: Long
Expand All @@ -26,10 +20,4 @@ data class SubscribePayload(val request: Request) {
var bonusPurchaseStatus: Enum<*>? = null,
var remoteServiceNotificationResult: Boolean = false
)

class IntermediateData {
lateinit var serviceInfo: ServiceInfo
lateinit var accountInfo: AccountInfo
lateinit var userInfo: UserProfile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,32 @@ CompletableReactor reactor() {
CompletableReactor reactor;

@Test()
public void invalid_user_purchases_invalid_service() throws Exception {
void invalid_user_purchases_invalid_service() throws Exception {

PurchasePayload payload = new PurchasePayload();
payload.request.setUserId(UserProfileManager.USER_ID_INVALID).setServiceId(ServiceRegistry.SERVICE_ID_INVALID);
PurchaseSubmit.Request request = new PurchaseSubmit.Request()
.setUserId(UserProfileManager.USER_ID_INVALID)
.setServiceId(ServiceRegistry.SERVICE_ID_INVALID);

PurchasePayload result = reactor.submit(payload).getResultFuture().get(5, TimeUnit.SECONDS);
PurchaseSubmit.Response response = reactor.graph(PurchaseSubmit.class)
.submit(request)
.getResultFuture()
.get(5, TimeUnit.SECONDS);

assertEquals(UserProfileManager.Status.USER_NOT_FOUND, result.response.getStatus());
assertEquals(UserProfileManager.Status.USER_NOT_FOUND, response.getStatus());

}

@Test
public void john_purchase_car_wash() throws Exception {
PurchasePayload payload = new PurchasePayload();
payload.request.setUserId(UserProfileManager.USER_ID_JOHN).setServiceId(ServiceRegistry.SERVICE_ID_CAR_WASH);
void john_purchase_car_wash() throws Exception {
PurchaseSubmit.Request request = new PurchaseSubmit.Request()
.setUserId(UserProfileManager.USER_ID_JOHN)
.setServiceId(ServiceRegistry.SERVICE_ID_CAR_WASH);

PurchasePayload result = reactor.submit(payload).getResultFuture().get(5, TimeUnit.SECONDS);
PurchaseSubmit.Response response = reactor.graph(PurchaseSubmit.class)
.submit(request)
.getResultFuture()
.get(5, TimeUnit.SECONDS);

assertEquals(Bank.Withdraw.Status.OK, result.response.getStatus());
assertEquals(Bank.Withdraw.Status.OK, response.getStatus());
}
}
Loading