Skip to content

Commit 826b958

Browse files
committed
make sample work in Liberty. Rename SampleServlet to reduce confusion
1 parent f114101 commit 826b958

File tree

5 files changed

+97
-11
lines changed

5 files changed

+97
-11
lines changed

mockito-testing-containers-sample/src/main/java/sample/CurrencyConverter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,33 @@
1717

1818
public class CurrencyConverter {
1919

20+
public static final String USD2GBP_CONTAINER = "USD-GBP";
21+
public static final String RATES_CHANNEL = "RATES";
22+
public static final String ACCOUNT_CHANNEL = "ACCOUNTS";
23+
2024
private CICSContext task;
2125

2226
public CurrencyConverter(CICSContext task) {
2327
this.task = task;
2428
}
2529

2630
public String convertCurrency(String accountName) throws CICSConditionException, InvalidConversionRateException {
31+
if (accountName == null || accountName.length() == 0) {
32+
throw new IllegalArgumentException("Account name must be provided");
33+
}
2734
double balance = getAccountBalance(accountName);
2835
double rate = getUSDGBPConversionRate();
2936
double convertedBalance = balance * rate;
3037
return String.format("%.2f", convertedBalance);
3138
}
3239

3340
private double getAccountBalance(String accountName) throws CICSConditionException {
34-
byte[] bytes = task.getChannel("ACCOUNTS").getBITContainer(accountName).get();
41+
byte[] bytes = task.getChannel(ACCOUNT_CHANNEL).getBITContainer(accountName).get();
3542
return ByteBuffer.wrap(bytes).getDouble();
3643
}
37-
44+
3845
private double getUSDGBPConversionRate() throws CICSConditionException, InvalidConversionRateException {
39-
byte[] bytes = task.getChannel("RATES").getBITContainer("USD-GBP").get();
46+
byte[] bytes = task.getChannel(RATES_CHANNEL).getBITContainer(USD2GBP_CONTAINER).get();
4047

4148
double rate = ByteBuffer.wrap(bytes).getDouble();
4249

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package sample;
2+
3+
import java.io.IOException;
4+
import java.nio.ByteBuffer;
5+
6+
import javax.servlet.Filter;
7+
import javax.servlet.FilterChain;
8+
import javax.servlet.FilterConfig;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.ServletRequest;
11+
import javax.servlet.ServletResponse;
12+
import javax.servlet.annotation.WebFilter;
13+
14+
import com.ibm.cics.jcicsx.BITContainer;
15+
import com.ibm.cics.jcicsx.CICSConditionException;
16+
import com.ibm.cics.jcicsx.CICSContext;
17+
import com.ibm.cics.jcicsx.Channel;
18+
19+
/*
20+
* This servlet filter runs before the main servlet and sets up some sample data - an account balance and a conversion rate.
21+
*/
22+
@WebFilter("/*")
23+
public class CurrencyFilter implements Filter {
24+
25+
@Override
26+
public void init(FilterConfig filterConfig) throws ServletException {}
27+
28+
@Override
29+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
30+
String accountName = request.getParameter("acctname");
31+
if (accountName == null || accountName.length() == 0) {
32+
accountName = CurrencyServlet.getDefaultAccountName();
33+
}
34+
35+
CICSContext task = CICSContext.getCICSContext();
36+
try {
37+
seedAccountContainer(task, accountName);
38+
seedConversionContainer(task);
39+
} catch (CICSConditionException e) {
40+
throw new ServletException("Exception settings up CICS environment", e);
41+
}
42+
43+
chain.doFilter(request, response);
44+
45+
46+
}
47+
48+
private void seedAccountContainer(CICSContext task, String accountName) throws CICSConditionException {
49+
Channel accountChannel = task.getChannel(CurrencyConverter.ACCOUNT_CHANNEL);
50+
BITContainer accountContainer = accountChannel.getBITContainer(accountName);
51+
byte[] bytes = new byte[8];
52+
ByteBuffer.wrap(bytes).putDouble(53000);
53+
accountContainer.put(bytes);
54+
}
55+
56+
private void seedConversionContainer(CICSContext task) throws CICSConditionException {
57+
Channel conversionChannel = task.getChannel(CurrencyConverter.RATES_CHANNEL);
58+
BITContainer conversionContainer = conversionChannel.getBITContainer(CurrencyConverter.USD2GBP_CONTAINER);
59+
byte[] bytes = new byte[8];
60+
ByteBuffer.wrap(bytes).putDouble(0.79);
61+
conversionContainer.put(bytes);
62+
}
63+
64+
@Override
65+
public void destroy() {}
66+
67+
}

mockito-testing-containers-sample/src/main/java/sample/SampleServlet.java renamed to mockito-testing-containers-sample/src/main/java/sample/CurrencyServlet.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
/* */
1212

1313
import java.io.IOException;
14+
import java.util.logging.Level;
15+
import java.util.logging.Logger;
1416

1517
import javax.servlet.ServletException;
1618
import javax.servlet.annotation.WebServlet;
@@ -24,11 +26,16 @@
2426
/**
2527
* A sample servlet to demonstrate how to use JCICSX to get data from BIT containers
2628
*/
27-
@WebServlet("/SampleServlet")
28-
public class SampleServlet extends HttpServlet {
29+
@WebServlet("/CurrencyServlet")
30+
public class CurrencyServlet extends HttpServlet {
31+
32+
Logger log = Logger.getLogger(CurrencyServlet.class.getName());
2933

3034
private static final long serialVersionUID = 1L;
3135

36+
public static String getDefaultAccountName() {
37+
return "cicsuser";
38+
}
3239

3340
/**
3441
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
@@ -37,24 +44,29 @@ public class SampleServlet extends HttpServlet {
3744
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
3845
response.setContentType("text/html");
3946

40-
response.getWriter().print("Hello world!");
4147

42-
String accountName = request.getParameter("accountname");
48+
String accountName = request.getParameter("acctname");
49+
if (accountName == null) {
50+
accountName = getDefaultAccountName();
51+
}
52+
response.getWriter().print("Converting balance for user "+accountName+"<br/>");
4353

4454
// Gets the current CICS Context for the environment we're running in
4555
CICSContext task = CICSContext.getCICSContext();
4656

4757
try {
4858
CurrencyConverter converter = new CurrencyConverter(task);
4959
String convertedCurrency = converter.convertCurrency(accountName);
50-
response.getWriter().println(convertedCurrency);
60+
response.getWriter().append("$").println(convertedCurrency);
5161
} catch (CICSConditionException e) {
62+
log.log(Level.SEVERE, "Exception occurred converting currency", e);
5263
response.getWriter().println("An exception has occured" +
5364
"\nRESP: " + e.getResp2() +
5465
"\nRESP2: " + e.getResp2() +
5566
"\nMessage: " + e.getMessage());
5667
} catch (InvalidConversionRateException e) {
5768
response.getWriter().println("An exception occured while trying to get the conversion rate");
69+
log.log(Level.SEVERE, "Exception occurred getting currency conversion rate", e);
5870
}
5971
}
6072

mockito-testing-containers-sample/src/main/webapp/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h1 id = "message"></h1>
1919
</td>
2020
</tr>
2121
</table>
22-
<!-- Call SampleServlet to get the message -->
22+
<!-- Call CurrencyServlet to get the message -->
2323
<script type="text/javascript" src="index.js"></script>
2424
</body>
2525
</html>

mockito-testing-containers-sample/src/main/webapp/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// index.js
22

33
// request message on server
4-
//Calls SampleServlet to get the message
5-
xhrGet("SampleServlet", function(responseText){
4+
//Calls CurrencyServlet to get the message
5+
xhrGet("CurrencyServlet", function(responseText){
66
// add to document
77
var mytitle = document.getElementById('message');
88
mytitle.innerHTML = responseText;

0 commit comments

Comments
 (0)