1 package ejava.projects.eleague.ejb;
2
3 import java.io.InputStream;
4
5 import static junit.framework.TestCase.*;
6
7 import javax.annotation.PostConstruct;
8 import javax.annotation.Resource;
9 import javax.ejb.EJBException;
10 import javax.ejb.Stateless;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import ejava.projects.eleague.dto.Club;
16 import ejava.projects.eleague.dto.Contact;
17 import ejava.projects.eleague.dto.ContactRoleType;
18 import ejava.projects.eleague.dto.Division;
19 import ejava.projects.eleague.dto.ELeague;
20 import ejava.projects.eleague.dto.LeagueMetadata;
21 import ejava.projects.eleague.dto.Season;
22 import ejava.projects.eleague.dto.Team;
23 import ejava.projects.eleague.dto.TeamSeason;
24 import ejava.projects.eleague.dto.Venue;
25 import ejava.projects.eleague.xml.ELeagueParser;
26
27
28
29
30
31
32 @Stateless
33 public class ParserTestEJB implements ParserTestRemote {
34 private static final Logger log = LoggerFactory.getLogger(ParserTestEJB.class);
35
36 @Resource(name="vals/xmlFile")
37 private String xmlFile;
38
39 @PostConstruct
40 public void init() {
41 log.debug("*** ParserTestEJB ***");
42 log.debug("xmlFile=" + xmlFile);
43 }
44
45 public void ingest() throws Exception {
46 log.info("ingest");
47
48 InputStream is = null;
49
50 int clubs=0;
51 int contacts=0;
52 int md=0;
53 int seasons=0;
54 try {
55 log.trace("getting input file:" + xmlFile);
56 is = this.getClass().getResourceAsStream(xmlFile);
57 if (is == null) {
58 throw new Exception(xmlFile + " was not found");
59 }
60
61 log.trace("creating parser");
62 ELeagueParser parser = new ELeagueParser(ELeague.class, is);
63
64 log.trace("starting parse loop");
65 Object object=null;
66 do {
67 object = parser.getObject(
68 "contact", "league-metadata", "club", "season");
69 if (object instanceof Club) {
70 log.debug("found Club");
71 testClub((Club)object);
72 clubs += 1;
73 }
74 else if (object instanceof Contact) {
75 log.debug("found Contact");
76 testContact((Contact)object);
77 contacts += 1;
78 }
79 else if (object instanceof LeagueMetadata) {
80 log.debug("found LeagueMetadata");
81 testLeagueMetadata((LeagueMetadata)object);
82 md += 1;
83 }
84 else if (object instanceof Season) {
85 log.debug("found Season");
86 testSeason((Season)object);
87 seasons += 1;
88 }
89 else if (object != null) {
90 fail("object of unknown type:" + object);
91 }
92 } while (object != null);
93 }
94 catch (Throwable ex) {
95 log.error("error parsing doc",ex);
96 throw new EJBException("error parsing doc:" + ex);
97 }
98 finally {
99 if (is != null) is.close();
100 }
101 assertTrue("no clubs found", clubs > 0);
102 assertTrue("no contacts found", contacts > 0);
103 assertTrue("no metadata found", md > 0);
104 assertTrue("no season found", seasons > 0);
105 log.debug(clubs + " clubs found");
106 log.debug(contacts + " contacts found");
107 log.debug(md + " metadata found");
108 log.debug(seasons + " seasons found");
109 }
110
111 void testSeason(Season season) {
112 log.debug("{}", new StringBuilder()
113 .append("season=")
114 .append(season.getId())
115 .append(", refId=")
116 .append(season.getRefid())
117 .append(", contests=")
118 .append(season.getContest().size())
119 .append(", divisions=")
120 .append(season.getDivision().size())
121 .append(", starts=")
122 .append(season.getStarts())
123 .append(", ends=")
124 .append(season.getEnds())
125 );
126 assertNotNull("season id null", season.getId());
127 assertNotNull("season name null", season.getName());
128 assertNotNull("season refId null", season.getRefid());
129 assertNotNull("season contest list null", season.getContest());
130 assertNotNull("season ends null", season.getEnds());
131 assertNotNull("season refId null", season.getRefid());
132 assertNotNull("season starts null", season.getStarts());
133
134 for (Division d : season.getDivision()) {
135 testDivision(d);
136 }
137 }
138
139 void testDivision(Division d) {
140 log.debug("{}", new StringBuilder()
141 .append("division=")
142 .append(d.getId())
143 .append(", group=")
144 .append(d.getGroup())
145 .append(", level=")
146 .append(d.getLevel())
147 .append("refId=")
148 .append(d.getRefid())
149 .append(", contact=")
150 .append(d.getContact())
151 .append(", ranking=")
152 .append(d.getRanking())
153 .append(", teams=")
154 .append(d.getTeamSeason().size())
155 );
156 assertNotNull("division id null", d.getId());
157
158 assertNotNull("division group null", d.getGroup());
159 assertNotNull("division level null", d.getLevel());
160 assertNotNull("division ranking null", d.getRanking());
161 assertNotNull("division refId null", d.getRefid());
162 assertNotNull("division teamSeason null", d.getTeamSeason());
163
164 for (TeamSeason ts : d.getTeamSeason()) {
165 testTeamSeason(ts);
166 }
167 }
168
169 void testTeamSeason(TeamSeason ts) {
170 log.debug("{}", new StringBuilder()
171 .append("teamSeason=")
172 .append(ts.getId())
173 .append(", refId=")
174 .append(ts.getRefid())
175 .append(", teamName=")
176 .append(((Team)ts.getTeam()).getName())
177 );
178 assertNotNull("team season id null", ts.getId());
179 assertNotNull("team season refId null", ts.getRefid());
180 assertNotNull("team season team null", ts.getTeam());
181
182 testTeam((Team)ts.getTeam());
183 }
184
185 void testLeagueMetadata(LeagueMetadata md) {
186 log.debug("{}", new StringBuilder()
187 .append("league=")
188 .append(md.getName())
189 .append(", contactName=")
190 .append(((Contact)md.getContact()).getName()));
191 assertNotNull("leage name null", md.getName());
192 assertNotNull("league contact null", md.getContact());
193
194 testContact((Contact)md.getContact());
195 }
196
197 void testContact(Contact contact) {
198 log.debug("{}", new StringBuilder()
199 .append("contact=")
200 .append(contact.getId())
201 .append(", name=")
202 .append(contact.getName())
203 .append(", login=")
204 .append(contact.getLogin())
205 .append(", refId=")
206 .append(contact.getRefid()));
207 assertNotNull("contact ID null", contact.getId());
208 assertNotNull("contact name null", contact.getName());
209 assertNotNull("contact refId null", contact.getRefid());
210 }
211
212 void testClub(Club club) {
213 log.debug("{}", new StringBuilder()
214 .append("club=")
215 .append(club.getId())
216 .append(", name=")
217 .append(club.getName())
218 .append(", refId=")
219 .append(club.getRefid())
220 .append(", teams=")
221 .append(club.getTeam().size())
222 .append(", venues=")
223 .append(club.getVenue().size()));
224
225 assertNotNull("club id null", club.getId());
226 assertNotNull("club contact null", club.getContact());
227 assertNotNull("club name null", club.getName());
228 assertNotNull("club refId null", club.getRefid());
229 assertNotNull("club team list null", club.getTeam());
230 assertNotNull("club venue list null", club.getVenue());
231
232 for (Team team : club.getTeam()) {
233 testTeam(team);
234 }
235 for (Venue venue : club.getVenue()) {
236 testVenue(venue);
237 }
238 }
239
240 private void testVenue(Venue venue) {
241 log.debug("{}", new StringBuilder()
242 .append("venue=")
243 .append(venue.getId())
244 .append(", name=")
245 .append(venue.getName())
246 .append(", ADC=")
247 .append(venue.getAdcPage())
248 .append(", address={")
249 .append(venue.getStreet1())
250 .append(" ")
251 .append(venue.getStreet2()==null ? "" : venue.getStreet2())
252 .append("; ")
253 .append(venue.getCity())
254 .append(", ")
255 .append(venue.getState())
256 .append(" ")
257 .append(venue.getZip())
258 .append("}"));
259 assertNotNull("venue ADC null", venue.getAdcPage());
260 assertNotNull("venue city null", venue.getCity());
261 assertNotNull("venue directions null", venue.getDirections());
262 assertNotNull("venue id null", venue.getId());
263 assertNotNull("venue name null", venue.getName());
264 assertNotNull("venue refId null", venue.getRefid());
265 assertNotNull("venue state null", venue.getState());
266 assertNotNull("venue street1 null", venue.getStreet1());
267
268 assertNotNull("venue zip null", venue.getZip());
269 }
270
271 void testTeam(Team team) {
272 log.debug("{}", new StringBuilder()
273 .append("team=")
274 .append(team.getId())
275 .append(", name=")
276 .append(team.getName())
277 .append(", refId=")
278 .append(team.getRefid())
279 .append(", contacts=")
280 .append(team.getContactRole().size()));
281 assertNotNull("team id null", team.getId());
282 assertNotNull("team name null", team.getName());
283 assertNotNull("team refId null", team.getRefid());
284 assertNotNull("team contactRole null", team.getContactRole());
285
286 for (ContactRoleType role : team.getContactRole()) {
287 testContactRole(role);
288 }
289 }
290
291 void testContactRole(ContactRoleType role) {
292 log.debug("{}", new StringBuilder()
293 .append("contactRole=")
294 .append(role.getId())
295 .append(", role=")
296 .append(role.getRole())
297 .append(", contactName=")
298 .append(((Contact)role.getContact()).getName()));
299 assertNotNull("contactRole id null", role.getId());
300 assertNotNull("contactRole role null", role.getRole());
301 assertNotNull("contactRole contact null", role.getContact());
302
303 testContact((Contact)role.getContact());
304 }
305 }