Coverage Report - org.jbehave.core.io.rest.xwiki.UploadToXWiki
 
Classes in this File Line Coverage Branch Coverage Complexity
UploadToXWiki
59%
13/22
75%
3/4
2.667
UploadToXWiki$1
100%
1/1
N/A
2.667
UploadToXWiki$Page
100%
1/1
N/A
2.667
 
 1  
 package org.jbehave.core.io.rest.xwiki;
 2  
 
 3  
 import org.jbehave.core.io.rest.RESTClient.Type;
 4  
 import org.jbehave.core.io.rest.Resource;
 5  
 import org.jbehave.core.io.rest.UploadToREST;
 6  
 
 7  
 import com.google.gson.Gson;
 8  
 import com.thoughtworks.xstream.XStream;
 9  
 
 10  
 /**
 11  
  * Uploads resource to XWiki pages using the REST API
 12  
  */
 13  
 public class UploadToXWiki extends UploadToREST {
 14  
 
 15  
         public UploadToXWiki(Type type) {
 16  2
                 this(type, null, null);
 17  2
         }
 18  
 
 19  
         public UploadToXWiki(Type type, String username, String password) {
 20  2
                 super(type, username, password);
 21  2
         }
 22  
 
 23  
         protected String entity(Resource resource, Type type) {
 24  2
                 Page page = new Page();
 25  2
                 page.syntax = ( resource.hasSyntax() ? resource.getSyntax() : "xwiki/2.0");
 26  2
                 page.title = resource.getName();
 27  2
                 page.content = resource.getContent();
 28  2
                 page.parent = resource.getParentName();
 29  1
                 switch (type) {
 30  
                 case JSON:
 31  2
                         Gson gson = new Gson();
 32  2
                         String json = gson.toJson(page);
 33  2
                         return json;
 34  
                 case XML:
 35  0
                         page.xmlns = "http://www.xwiki.org";
 36  0
                         XStream xstream = new XStream();
 37  0
                         xstream.alias("page", Page.class);
 38  0
                         xstream.useAttributeFor(Page.class, "xmlns");
 39  0
             xstream.aliasField("xmlns", Page.class, "xmlns");
 40  0
             xstream.ignoreUnknownElements();
 41  0
                         String xml = xstream.toXML(page);
 42  0
                         return xml;
 43  
                 default:
 44  0
                         return resource.getContent();
 45  
                 }
 46  
         }
 47  
 
 48  
         @SuppressWarnings("unused")
 49  12
         private static class Page {
 50  
                 private String xmlns;
 51  
                 private String title;
 52  
                 private String syntax;
 53  
                 private String content;
 54  
                 private String parent;
 55  
         }
 56  
 
 57  
 }