forked from aslakknutsen/arquillian-showcase
/
DefaultStockServiceTestCase.java
212 lines (170 loc) · 6.51 KB
/
DefaultStockServiceTestCase.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.acme.spring.jpa.service.impl;
import com.acme.spring.jpa.Deployments;
import com.acme.spring.jpa.JpaTestHelper;
import com.acme.spring.jpa.domain.Stock;
import com.acme.spring.jpa.service.StockService;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.spring.integration.test.annotation.SpringConfiguration;
import org.jboss.arquillian.transaction.api.annotation.Transactional;
import org.jboss.shrinkwrap.api.Archive;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import static com.acme.spring.jpa.JpaTestHelper.retrieveAllStocks;
import static com.acme.spring.jpa.JpaTestHelper.runScript;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* <p>Tests the {@link DefaultStockService} class.</p>
*
* @author <a href="mailto:jmnarloch@gmail.com">Jakub Narloch</a>
*/
@RunWith(Arquillian.class)
@Transactional(manager = "txManager")
@SpringConfiguration("applicationContext.xml")
public class DefaultStockServiceTestCase {
/**
* <p>Creates the test deployment.</p>
*
* @return the test deployment
*/
@Deployment
public static Archive createTestArchive() {
return Deployments.createDeployment();
}
/**
* <p>Injected {@link DefaultStockService}.</p>
*/
@Autowired
private StockService stockService;
/**
* <p>{@link EntityManager} instance used by tests.</p>
*/
@PersistenceContext
private EntityManager entityManager;
/**
* <p>Tears down the test environment.</p>
*
* @throws Exception if any error occurs
*/
@After
public void tearDown() throws Exception {
// deletes all records from database
entityManager.createQuery("delete from Stock").executeUpdate();
}
/**
* <p>Tests the {@link DefaultStockService#save(Stock)} method.</p>
*/
@Test
public void testSave() {
Stock acme = createStock("Acme", "ACM", 123.21D, new Date());
Stock redhat = createStock("Red Hat", "RHC", 59.61D, new Date());
stockService.save(acme);
stockService.save(redhat);
assertTrue("The stock id hasn't been assigned.", acme.getId() > 0);
assertTrue("The stock id hasn't been assigned.", redhat.getId() > 0);
List<Stock> stocks = retrieveAllStocks(entityManager);
assertEquals("Incorrect number of created stocks, 2 were expected.", 2, stocks.size());
assertStock(acme, stocks.get(0));
assertStock(redhat, stocks.get(1));
}
/**
* <p>Tests the {@link DefaultStockService#update(Stock)} method.</p>
*/
@Test
public void testUpdate() throws Exception {
runScript(entityManager, "insert.sql");
List<Stock> stocks = JpaTestHelper.retrieveAllStocks(entityManager);
Stock acme = stocks.get(0);
acme.setSymbol("ACE");
stockService.update(acme);
stocks = retrieveAllStocks(entityManager);
assertEquals("The stock symbol hasn't been updated.", acme.getSymbol(), stocks.get(0).getSymbol());
}
/**
* <p>Tests the {@link DefaultStockService#get(long)} method.</p>
*/
@Test
public void testGet() throws Exception {
runScript(entityManager, "insert.sql");
Stock acme = createStock("Acme", "ACM", 123.21D, new Date());
Stock result = stockService.get(1L);
assertNotNull("Method returned null result.", result);
assertStock(acme, result);
}
/**
* <p>Tests the {@link DefaultStockService#getBySymbol(String)} method.</p>
*/
@Test
public void testGetBySymbol() throws Exception {
runScript(entityManager, "insert.sql");
Stock acme = createStock("Acme", "ACM", 123.21D, new Date());
Stock result = stockService.getBySymbol(acme.getSymbol());
assertNotNull("Method returned null result.", result);
assertStock(acme, result);
}
/**
* <p>Tests the {@link DefaultStockService#getAll()} method.</p>
*/
@Test
public void testGetAll() throws Exception {
runScript(entityManager, "insert.sql");
List<Stock> result = stockService.getAll();
assertNotNull("Method returned null result.", result);
assertEquals("Incorrect number of elements.", 2, result.size());
}
/**
* <p>Creates new stock instance</p>
*
* @param name the stock name
* @param symbol the stock symbol
* @param value the stock value
* @param date the stock date
*
* @return the created stock instance
*/
private static Stock createStock(String name, String symbol, double value, Date date) {
Stock result = new Stock();
result.setName(name);
result.setSymbol(symbol);
result.setValue(new BigDecimal(value));
result.setDate(date);
return result;
}
/**
* <p>Asserts that the actual stock's properties values are correct.</p>
*
* @param expected the expected stock object
* @param actual the tested stock object
*/
private static void assertStock(Stock expected, Stock actual) {
assertEquals("Stock has invalid name property.", expected.getName(), actual.getName());
assertEquals("Stock has invalid symbol property.", expected.getSymbol(), actual.getSymbol());
assertEquals("Stock has invalid value property.", expected.getValue().doubleValue(),
actual.getValue().doubleValue(), 0.01D);
}
}