-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRewardServiceTest.groovy
More file actions
163 lines (140 loc) · 4.78 KB
/
RewardServiceTest.groovy
File metadata and controls
163 lines (140 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.gfrison
import org.apache.camel.Exchange;
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import javax.ws.rs.core.Response
import org.apache.camel.EndpointInject
import org.apache.camel.Processor
import org.apache.camel.Produce
import org.apache.camel.ProducerTemplate
import org.apache.camel.builder.ExpressionBuilder
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.component.mock.MockEndpoint
import org.apache.camel.test.junit4.CamelTestSupport
import org.apache.commons.dbcp.BasicDataSource
import org.apache.log4j.BasicConfigurator
import org.apache.log4j.Level
import org.apache.log4j.Logger
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.springframework.jdbc.core.JdbcTemplate
import com.gfrison.services.InitDevelopmentDatabase;
import com.gfrison.services.RewardService
/**
* @author [email protected] <Giancarlo Frison>
*
*/
class RewardServiceTest extends CamelTestSupport{
@EndpointInject(uri="mock:eligibility")
MockEndpoint eligibility
@Produce(uri = "direct:rewards")
ProducerTemplate template;
static JdbcTemplate db
RewardService rewardService
def json
@BeforeClass
static void init(){
def datasource = new BasicDataSource()
datasource.setDriverClassName("org.hsqldb.jdbcDriver")
datasource.setUrl("jdbc:hsqldb:mem:.")
datasource.setUsername("sa")
datasource.setPassword("")
db = new JdbcTemplate(datasource)
def initHSQLDB = new InitDevelopmentDatabase(performInit:true,db:db)
initHSQLDB.initHSQLDB()
assertEquals('CHAMPIONS_LEAGUE_FINAL_TICKET', db.queryForObject("select reward from rewards where channel='SPORTS'",String.class))
}
@Before
void initInstance(){
rewardService = new RewardService(eligibility:template, db:db)
json = new JsonBuilder()
json.portfolio {
channels(['SPORTS'])
}
BasicConfigurator.configure()
Logger.getRootLogger().setLevel(Level.INFO)
}
/*
* test if EligibilityService receive correct input
*/
@Test
void testInvokingEligibilityWithCorrectAccounID(){
eligibility.returnReplyBody(ExpressionBuilder.simpleExpression(RewardService.EligibilityOutput.CUSTOMER_ELIGIBLE.name()))
String expected = "account-12345"
eligibility.expectedBodiesReceived(expected)
rewardService.getRewards(expected, json.toString())
eligibility.assertIsSatisfied()
}
/*
* test correct reward
*/
@Test
void testEligibility(){
eligibility.returnReplyBody(ExpressionBuilder.simpleExpression(RewardService.EligibilityOutput.CUSTOMER_ELIGIBLE.name()))
Response res = rewardService.getRewards("account-12345", json.toString())
assertEquals(Response.ok().build().getStatus(), res.getStatus())
def slurper = new JsonSlurper()
def json = slurper.parseText(res.getEntity())
assertEquals('CHAMPIONS_LEAGUE_FINAL_TICKET',json.account.rewards[0])
}
@Test
void testChannelWithoutReward(){
json = new JsonBuilder()
json.portfolio {
channels(['KIDS']) //no rewards at this channel
}
eligibility.returnReplyBody(ExpressionBuilder.simpleExpression(RewardService.EligibilityOutput.CUSTOMER_ELIGIBLE.name()))
Response res = rewardService.getRewards("account-12345", json.toString())
assertEquals(Response.ok().build().getStatus(), res.getStatus())
def slurper = new JsonSlurper()
def json = slurper.parseText(res.getEntity())
assertEquals(0,json.account.rewards.size())
}
/*
* test negative response from EligibilityService
*/
@Test
void testIneligibility(){
eligibility.returnReplyBody(ExpressionBuilder.simpleExpression(RewardService.EligibilityOutput.CUSTOMER_INELIGIBLE.name()))
Response res = rewardService.getRewards("account-12345", json.toString())
assertEquals(Response.noContent().build().getStatus(), res.getStatus())
}
/*
* test tech failure response from EligibilityService
*/
@Test
void testTechnicalFailure(){
eligibility.whenAnyExchangeReceived(new Processor(){
void process(Exchange exchange) throws Exception{
//unchecked exception for technical errors
throw new RuntimeException("test technical failure")
}
});
Response res = rewardService.getRewards("account-12345", json.toString())
//503 Service Unavailable
assertEquals(503, res.getStatus())
}
@Test
void testInvalidAccount(){
eligibility.whenAnyExchangeReceived(new Processor(){
void process(Exchange exchange) throws Exception{
throw new Exception("Invalid account number")
}
});
Response res = rewardService.getRewards("account-12345", json.toString())
//400 Bad Request
assertEquals(400, res.getStatus())
def slurper = new JsonSlurper()
def json = slurper.parseText(res.getEntity())
assertNotNull(json?.account?.error)
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder(){
public void configure(){
from("direct:rewards").to("mock:eligibility");
}
}
}
}