private static JSSnippet createSnippetFromXml(String xml) { try { Document document = DOMUtil.createDocument(xml); XResult unid = DOMUtil.evaluateXPath(document, "unid"); XResult js = DOMUtil.evaluateXPath(document.getDocumentElement(), "js"); XResult html = DOMUtil.evaluateXPath(document.getDocumentElement(), "html"); XResult css = DOMUtil.evaluateXPath(document.getDocumentElement(), "css"); XResult docHtml = DOMUtil.evaluateXPath(document.getDocumentElement(), "docHtml"); XResult theme = DOMUtil.evaluateXPath(document.getDocumentElement(), "theme"); XResult description = DOMUtil.evaluateXPath(document.getDocumentElement(), "description"); XResult tags = DOMUtil.evaluateXPath(document.getDocumentElement(), "tags"); XResult labels = DOMUtil.evaluateXPath(document.getDocumentElement(), "labels"); JSSnippet snippet = new JSSnippet(); if (unid != null) snippet.setUnid(unid.getStringValue()); if (js != null) snippet.setJs(js.getStringValue()); if (html != null) snippet.setHtml(html.getStringValue()); if (css != null) snippet.setCss(css.getStringValue()); if (docHtml != null) snippet.setDocHtml(docHtml.getStringValue()); Properties p = new Properties(); snippet.init(p); if (theme != null && theme.getStringValue() != null) snippet.setTheme(theme.getStringValue()); if (description != null && description.getStringValue() != null) snippet.setDescription(description.getStringValue()); if (tags != null && tags.getValues() != null) snippet.setTags(tags.getValues()); if (labels != null && labels.getValues() != null) snippet.setLabels(labels.getValues()); return snippet; } catch (Exception e) { return null; } }
/** * this methd is called for every operation on the registered endpoint a test class is thus paired * with its own test data */ @Override protected Object testRequest(String method, Args args, Object content) throws ClientServicesException { // used by testLoad() if (method.equals("get") && args.getServiceUrl().equals("communities/service/atom/communities/all")) { try { return DOMUtil.createDocument(this.getClass().getResourceAsStream("allCommunities.xml")); } catch (XMLException e) { throw new ClientServicesException(e); } } // used by testCreate() if (method.equals("post") && args.getServiceUrl().equals("communities/service/atom/communities/my")) { // TODO: we should thest the API usage against the API XSD try { Node creationData = (DOMUtil.createDocument((String) content)); NamespaceContext ctx = new XMLFieldExtractor(new HashMap<String, XPathExpression>()).getNamespaceContext(); assertEquals( DOMUtil.evaluateXPath(creationData, "/entry/content", ctx).getStringValue(), CommunitiesTest.TEST_COMMUNITY_DESCRIPTION); assertEquals( DOMUtil.evaluateXPath(creationData, "/entry/title", ctx).getStringValue(), CommunitiesTest.NEW_COMMUNITY); } catch (Throwable e) { e.printStackTrace(); throw new ClientServicesException(e); } // TODO: use the standard response we would get return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; } // this is here just to trap and detect the requests that are to be implemented for the test to // work throw new ClientServicesException( new IllegalArgumentException( "Testing service only, please implement " + method + " at " + args.getServiceUrl() + " for " + args.getParameters() + " for " + content + " with " + args.getHeaders())); }