Skip to content

Commit 7fb1c4d

Browse files
committed
Mockito sample
1 parent 5e8e142 commit 7fb1c4d

File tree

11 files changed

+482
-0
lines changed

11 files changed

+482
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Mockito testing containers Sample
2+
3+
This sample shows how you can use the JCICSX API to get data from BIT containers.
4+
This sample contains a `CurrencyConverter` class which gets an account balance, and a conversion rate from 2 separate channels, and returns a string representing the value of the account once converted using the rate.
5+
In `src/test/java/mockitotests`, there is a test class `CurrencyConverterTest`, which demonstrates how to use a mocking framework such as Mockito to test the logic of your application by mocking out the JCICSX calls. By mocking out the JCICSX calls, CICS is not called, and these tests can be run in an agnostic environment.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<groupId>com.ibm.cics</groupId>
5+
<artifactId>mockito-testing-containers-sample</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>javax.servlet</groupId>
12+
<artifactId>javax.servlet-api</artifactId>
13+
<version>3.1.0</version>
14+
<scope>provided</scope>
15+
</dependency>
16+
<dependency>
17+
<groupId>javax.annotation</groupId>
18+
<artifactId>jsr250-api</artifactId>
19+
<version>1.0</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
<dependency>
23+
<!-- <groupId>com.ibm.cics</groupId>
24+
<artifactId>com.ibm.cics.jcicsx</artifactId> -->
25+
<groupId>com.ibm.cics.exec-cics-http</groupId>
26+
<artifactId>jcicsx-http-client</artifactId>
27+
<version>0.0.8-SNAPSHOT</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>3.8.1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.mockito</groupId>
37+
<artifactId>mockito-all</artifactId>
38+
<version>1.10.19</version>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<pluginManagement>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-war-plugin</artifactId>
48+
<version>3.2.2</version>
49+
<configuration>
50+
<failOnMissingWebXml>false</failOnMissingWebXml>
51+
</configuration>
52+
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<configuration>
57+
<source>1.8</source>
58+
<target>1.8</target>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</pluginManagement>
63+
</build>
64+
65+
66+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sample;
2+
3+
import java.nio.ByteBuffer;
4+
5+
import com.ibm.cics.jcicsx.CICSConditionException;
6+
import com.ibm.cics.jcicsx.CICSContext;
7+
8+
public class CurrencyConverter {
9+
10+
private CICSContext task;
11+
12+
public CurrencyConverter(CICSContext task) {
13+
this.task = task;
14+
}
15+
16+
public String convertCurrency(String accountName) throws CICSConditionException, InvalidConversionRateException {
17+
double balance = getAccountBalance(accountName);
18+
double rate = getUSDGBPConversionRate();
19+
double convertedBalance = balance * rate;
20+
return String.format("%.2f", convertedBalance);
21+
}
22+
23+
private double getAccountBalance(String accountName) throws CICSConditionException {
24+
byte[] bytes = task.getChannel("ACCOUNTS").getBITContainer(accountName).get();
25+
return ByteBuffer.wrap(bytes).getDouble();
26+
}
27+
28+
private double getUSDGBPConversionRate() throws CICSConditionException, InvalidConversionRateException {
29+
byte[] bytes = task.getChannel("RATES").getBITContainer("USD-GBP").get();
30+
31+
double rate = ByteBuffer.wrap(bytes).getDouble();
32+
33+
if (rate < 0) {
34+
throw new InvalidConversionRateException();
35+
}
36+
37+
return rate;
38+
}
39+
40+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package sample;
2+
3+
public class InvalidConversionRateException extends Exception {
4+
5+
private static final long serialVersionUID = 1L;
6+
7+
@Override
8+
public String getMessage() {
9+
return "Conversion rate not valid";
10+
}
11+
12+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package sample;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import com.ibm.cics.jcicsx.CICSConditionException;
12+
import com.ibm.cics.jcicsx.CICSContext;
13+
14+
/**
15+
* A sample servlet to demonstrate how to use JCICSX to get data from BIT containers
16+
*/
17+
@WebServlet("/SampleServlet")
18+
public class SampleServlet extends HttpServlet {
19+
20+
private static final long serialVersionUID = 1L;
21+
22+
23+
/**
24+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
25+
*/
26+
@Override
27+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28+
response.setContentType("text/html");
29+
30+
response.getWriter().print("Hello world!");
31+
32+
String accountName = request.getParameter("accountname");
33+
34+
// Gets the current CICS Context for the environment we're running in
35+
CICSContext task = CICSContext.getCICSContext();
36+
37+
try {
38+
CurrencyConverter converter = new CurrencyConverter(task);
39+
String convertedCurrency = converter.convertCurrency(accountName);
40+
response.getWriter().println(convertedCurrency);
41+
} catch (CICSConditionException e) {
42+
response.getWriter().println("An exception has occured" +
43+
"\nRESP: " + e.getResp2() +
44+
"\nRESP2: " + e.getResp2() +
45+
"\nMessage: " + e.getMessage());
46+
} catch (InvalidConversionRateException e) {
47+
response.getWriter().println("An exception occured while trying to get the conversion rate");
48+
}
49+
}
50+
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
5+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
6+
version="3.1">
7+
<login-config>
8+
<auth-method>CLIENT-CERT</auth-method>
9+
</login-config>
10+
</web-app>
42.7 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Java Web Starter Application</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<link rel="stylesheet" href="style.css" />
7+
</head>
8+
<body>
9+
<table>
10+
<tr>
11+
<td style='width: 30%;'>
12+
<img class = "newappIcon" src='images/cics.png'>
13+
</td>
14+
<td>
15+
<!-- The message comes from /SampleServlet -->
16+
<h1 id = "message"></h1>
17+
18+
<p class='description'></p> Thanks for creating a <span class="blue">Liberty for Java Starter Application</span>.
19+
</td>
20+
</tr>
21+
</table>
22+
<!-- Call SampleServlet to get the message -->
23+
<script type="text/javascript" src="index.js"></script>
24+
</body>
25+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// index.js
2+
3+
// request message on server
4+
//Calls SampleServlet to get the message
5+
xhrGet("SampleServlet", function(responseText){
6+
// add to document
7+
var mytitle = document.getElementById('message');
8+
mytitle.innerHTML = responseText;
9+
10+
}, function(err){
11+
console.log(err);
12+
});
13+
14+
//utilities
15+
function createXHR(){
16+
if(typeof XMLHttpRequest != 'undefined'){
17+
return new XMLHttpRequest();
18+
}else{
19+
try{
20+
return new ActiveXObject('Msxml2.XMLHTTP');
21+
}catch(e){
22+
try{
23+
return new ActiveXObject('Microsoft.XMLHTTP');
24+
}catch(e){}
25+
}
26+
}
27+
return null;
28+
}
29+
function xhrGet(url, callback, errback){
30+
var xhr = new createXHR();
31+
xhr.open("GET", url, true);
32+
xhr.onreadystatechange = function(){
33+
if(xhr.readyState == 4){
34+
if(xhr.status == 200){
35+
callback(xhr.responseText);
36+
}else{
37+
errback('service not available');
38+
}
39+
}
40+
};
41+
xhr.timeout = 3000;
42+
xhr.ontimeout = errback;
43+
xhr.send();
44+
}
45+
function parseJson(str){
46+
return window.JSON ? JSON.parse(str) : eval('(' + str + ')');
47+
}
48+
function prettyJson(str){
49+
// If browser does not have JSON utilities, just print the raw string value.
50+
return window.JSON ? JSON.stringify(JSON.parse(str), null, ' ') : str;
51+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* style.css
2+
* This file provides css styles.
3+
*/
4+
5+
body,html {
6+
background-color: #3b4b54; width : 100%;
7+
height: 100%;
8+
margin: 0 auto;
9+
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
10+
color: #ffffff;
11+
}
12+
13+
a {
14+
text-decoration: none;
15+
color: #00aed1;
16+
}
17+
18+
a:hover {
19+
text-decoration: underline;
20+
}
21+
22+
.newappIcon {
23+
padding-top: 10%;
24+
display: block;
25+
margin: 0 auto;
26+
padding-bottom: 2em;
27+
max-width:200px;
28+
}
29+
30+
h1 {
31+
font-weight: bold;
32+
font-size: 2em;
33+
}
34+
35+
.leftHalf {
36+
float: left;
37+
background-color: #26343f;
38+
width: 45%;
39+
height: 100%;
40+
}
41+
42+
.rightHalf {
43+
float: right;
44+
width: 55%;
45+
background-color: #313f4a;
46+
height: 100%;
47+
overflow:auto;
48+
}
49+
50+
.description {
51+
padding-left: 50px;
52+
padding-right: 50px;
53+
text-align: center;
54+
font-size: 1.2em;
55+
}
56+
57+
.blue {
58+
color: #00aed1;
59+
}
60+
61+
62+
table {
63+
table-layout: fixed;
64+
width: 800px;
65+
margin: 0 auto;
66+
word-wrap: break-word;
67+
padding-top:10%;
68+
}
69+
70+
th {
71+
border-bottom: 1px solid #000;
72+
}
73+
74+
th, td {
75+
text-align: left;
76+
padding: 2px 20px;
77+
}
78+
79+
.env-var {
80+
text-align: right;
81+
border-right: 1px solid #000;
82+
width: 30%;
83+
}
84+
85+
pre {
86+
padding: 0;
87+
margin: 0;
88+
}

0 commit comments

Comments
 (0)