rendered paste bodypackage com.bmd.bs400.facade;import java.io.IOException;import java.io.InputStream;import java.io.StringReader;import java.io.StringWriter;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import javax.xml.stream.XMLStreamReader;import org.codehaus.xfire.service.ServiceRegistry;import org.codehaus.xfire.spring.AbstractXFireSpringTest;import org.codehaus.xfire.util.STAXUtils;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.XMLOutputter;import org.jdom.xpath.XPath;/** * Unit test af JSR 181 RhFacade WebServicen * @author Jeppe Cramon - Cramon Consulting [email: jeppe@cramon.dk, tlf: 26202565] * @author cln */public abstract class RhFacadeWebServiceTestAbstract extends AbstractXFireSpringTest { private final static String serviceName ="rhFacade"; /** * VÊrdien for den auth token vi hentede som en del af setUp() metoden. * Skal bruges til kald til andre metoder i webservicen */ private String authTokenValue; /** Initer. * * Kaldes som det f¯rste i setUp(). */ abstract protected void init(); @Override public void setUp() throws Exception { super.setUp(); init();// Reader r = getResourceAsReader("log4j.properties");// for(int i = r.read(); i>0; i=r.read())// System.out.print((char)i); // Check servicen er registreret ServiceRegistry reg = (ServiceRegistry) getContext().getBean("xfire.serviceRegistry"); assertNotNull(serviceName + " webservicen er ikke registreret", reg.getService(serviceName)); // Udf¯r en login og gem auth token Document response = invokeService(serviceName, "loginTest.xml"); addNamespace("bmd", "http://facade.bs400.bmd.com" ); addNamespace("trf", "http://transferobjects.bs400.bmd.com" ); assertInvalid("//s:Fault", response ); assertValid( "//bmd:loginResponse", response ); assertValid( "//bmd:authToken", response ); XPath xpath = XPath.newInstance("//bmd:authToken"); xpath.addNamespace("bmd", "http://facade.bs400.bmd.com"); Element authTokenElement = (Element) xpath.selectSingleNode(response); authTokenValue = authTokenElement.getText(); assertNotNull("Kunne ikke f fat i AuthToken fra Responset", authTokenValue); } public void testWsLogin() throws Exception { Document response = invokeService(serviceName, "wsLoginTest.xml"); assertInvalid("//s:Fault", response ); assertValid( "//trf:authToken", response ); } public void testSkiftPassword() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("skiftPasswordTest.xml")); assertInvalid("//s:Fault", response ); assertValid( "//s:Body", response ); } public void testSkiftPasswordWithInvalidToken() throws Exception { authTokenValue="invalidtoken"; Document response = invokeService(serviceName, getPreparedXMLStreamReader("skiftPasswordTest.xml")); assertValid("//s:Fault", response ); } public void testSkiftPasswordWithInvalidPassword() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("skiftPasswordInvalidTest.xml")); assertValid("//s:Fault", response ); } public void testLogout() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("logoutTest.xml")); assertInvalid("//s:Fault", response ); assertValid( "//s:Body", response ); } public void testSkiftPasswordFailsAfterLogout() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("logoutTest.xml")); assertInvalid("//s:Fault", response ); assertValid( "//s:Body", response ); response = invokeService(serviceName, getPreparedXMLStreamReader("skiftPasswordTest.xml")); assertValid("//s:Fault", response ); } @SuppressWarnings("unchecked") public void testFindEmailAdresser() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("findEmailAdresserTest.xml")); assertInvalid("//s:Fault", response ); assertValid( "//bmd:findEmailAdresserResponse", response ); assertValid( "//bmd:emailAdresser", response ); // Check hver adresse Element x = getNode(response.getRootElement(), "//bmd:emailAdresser"); List<Element> children = x.getChildren(); assertEquals(2, children.size()); Set mailadresser = new HashSet(2); mailadresser.add(children.get(0).getText()); mailadresser.add(children.get(1).getText()); String ma = "claus.nielsen@bmd.bonnier.dk"; assertTrue("Forventede mailadresse " + ma, mailadresser.contains(ma)); ma = "cn@cn-consult.dk"; assertTrue("Forventede mailadresse " + ma, mailadresser.contains(ma)); } @SuppressWarnings("unchecked") public void testHentEmailAnvendelser() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("hentEmailAnvendelserTest.xml")); assertInvalid("//s:Fault", response ); assertValid( "//bmd:hentEmailAnvendelserResponse", response ); assertValid( "//bmd:emailAnvendelser", response ); // Check hver af anvendelserne XPath xpath = XPath.newInstance("//trf:KontaktadrAnvendelseTO"); xpath.addNamespace("trf", "http://transferobjects.bs400.bmd.com"); List<Element> nodes = xpath.selectNodes(response); assertEquals("Forventede liste med 4 anvendelser.", 4, nodes.size()); class Till { public String oensket; public String tilladt; } Map<String, Till> tilladelser = new HashMap<String, Till>(); for (Element emailAnvendelseElement : nodes) { Till till = new Till(); String anvKode = getNode(emailAnvendelseElement, "trf:anvendelseskode").getText(); till.oensket = getNode(emailAnvendelseElement, "trf:oensket").getText(); till.tilladt = getNode(emailAnvendelseElement, "trf:tilladt").getText(); tilladelser.put(anvKode, till); } // Tjek Kundeservice Till till = tilladelser.get("KS"); assertNotNull("Tilladelse KS ikke fundet.", till); assertEquals("true", till.oensket); assertEquals("true", till.tilladt); // Tjek Track & Trace till = tilladelser.get("TT"); assertNotNull("Tilladelse TT ikke fundet.", till); assertEquals("true", till.oensket); assertEquals("false", till.tilladt); // Tjek Tilbud og info till = tilladelser.get("TILBUD"); assertNotNull("Tilladelse TILBUD ikke fundet.", till); assertEquals("true", till.oensket); assertEquals("true", till.tilladt); // Tjek Nyhedsbrev ILL till = tilladelser.get("NB_ILL"); assertNotNull("Tilladelse NB_ILL ikke fundet.", till); assertEquals("true", till.oensket); assertEquals("false", till.tilladt); } public void testIsAnvendelseTilladt_ja() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("isAnvendelseTilladt_ja_Test.xml")); assertInvalid("//s:Fault", response ); assertValid( "//bmd:isAnvendelseTilladtResponse", response ); assertValid( "//bmd:tilladt", response ); Element tilladt = getNode(response.getRootElement(), "//bmd:tilladt"); assertEquals("Anvendelse TILBUD forventedes at vÊre tilladt.", "true", tilladt.getText()); } public void testIsAnvendelseTilladt_nej() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("isAnvendelseTilladt_nej_Test.xml")); assertInvalid("//s:Fault", response ); assertValid( "//bmd:isAnvendelseTilladtResponse", response ); assertValid( "//bmd:tilladt", response ); Element tilladt = getNode(response.getRootElement(), "//bmd:tilladt"); assertEquals("Anvendelse NB_NGM forventedes ikke at vÊre tilladt.", "false", tilladt.getText()); } /** Test registrerKontaktkupon(). * * Tester ikke at kuponen registreres - kun at kaldet ikke fejler. * * @throws Exception */ public void testRegistrerKontaktkupon() throws Exception { Document response = invokeService(serviceName, getPreparedXMLStreamReader("registrerKontaktkuponTest.xml")); assertInvalid("//s:Fault", response ); assertValid( "//bmd:registrerKontaktkuponResponse", response ); } protected Element getNode(Element parentElement, String path) throws JDOMException { XPath xpath = XPath.newInstance(path); xpath.addNamespace("bmd", "http://facade.bs400.bmd.com"); xpath.addNamespace("trf", "http://transferobjects.bs400.bmd.com"); return (Element) xpath.selectSingleNode(parentElement); } /** * Get the input document from the classpath * @param path The path to the input document * @return The document instance * @throws JDOMException In case of an JDOM error * @throws IOException In case of an I/O Error */ protected Document getInputDocument(String path) throws JDOMException, IOException { SAXBuilder reader = new SAXBuilder(); return (Document) reader.build( getResourceAsStream(path)); } /** * Get the input document from the classpath and set the authtoken so the document is ready for testing * @param path The path to the input document * @return The document instance * @throws JDOMException In case of an JDOM error * @throws IOException In case of an I/O Error */ protected Document getPreparedInputDocument(String path) throws JDOMException, IOException { Document doc = getInputDocument(path); XPath xpath = XPath.newInstance("//bmd:authToken"); xpath.addNamespace("bmd", "http://facade.bs400.bmd.com"); Element authTokenElement = (Element) xpath.selectSingleNode(doc); authTokenElement.setText(authTokenValue); return doc; } /** * Convert a JDOM document to an XMLStreamReader * @param document The document to convert * @return The XMLStreamReader which will read the XML from the document * @throws IOException If something goes wrong in the conversion */ protected XMLStreamReader convertDocumentToXmlStreamReader(Document document) throws IOException { XMLOutputter outputter = new XMLOutputter(); StringWriter writer = new StringWriter(); outputter.output(document, writer); return STAXUtils.createXMLStreamReader(new StringReader(writer.getBuffer().toString())); } /** * Get an XMLStreamReader for the input document on the classpath. * Also, this method sets the authtoken so the document is ready for testing * @param path The path to the input document * @return The XMLStreamReader instance, which reads from the input document after it has been prepared * @throws JDOMException In case of an JDOM error * @throws IOException In case of an I/O Error */ protected XMLStreamReader getPreparedXMLStreamReader(String path) throws IOException, JDOMException { return convertDocumentToXmlStreamReader(getPreparedInputDocument(path)); } protected InputStream getResourceAsStream(String resource) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream res = cl.getResourceAsStream(resource); return res; }}