Coverage Report - org.jbehave.core.io.rest.redmine.IndexFromRedmine
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexFromRedmine
94%
18/19
100%
4/4
1.286
IndexFromRedmine$1
100%
1/1
N/A
1.286
IndexFromRedmine$Page
100%
1/1
N/A
1.286
 
 1  
 package org.jbehave.core.io.rest.redmine;
 2  
 
 3  
 import static java.text.MessageFormat.format;
 4  
 
 5  
 import java.util.Collection;
 6  
 import java.util.HashMap;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.jbehave.core.io.rest.IndexWithBreadcrumbs;
 10  
 import org.jbehave.core.io.rest.RESTClient;
 11  
 import org.jbehave.core.io.rest.RESTClient.Type;
 12  
 import org.jbehave.core.io.rest.Resource;
 13  
 import org.jbehave.core.io.rest.ResourceNameResolver;
 14  
 
 15  
 import com.google.gson.Gson;
 16  
 import com.google.gson.JsonParser;
 17  
 import com.google.gson.reflect.TypeToken;
 18  
 
 19  
 /**
 20  
  * Indexes resources from Redmine using the REST API
 21  
  */
 22  
 public class IndexFromRedmine extends IndexWithBreadcrumbs {
 23  
 
 24  
     private static final String INDEX_URI = "{0}/index.json";
 25  
     private static final String PAGE_URI = "{0}/{1}";
 26  
 
 27  
     public IndexFromRedmine() {
 28  1
         this(null, null);
 29  1
     }
 30  
 
 31  
     public IndexFromRedmine(String username, String password) {
 32  1
         this(username, password, new ToLowerCase());
 33  1
     }
 34  
 
 35  
     public IndexFromRedmine(String username, String password, ResourceNameResolver nameResolver) {
 36  1
         super(new RESTClient(Type.JSON, username, password), nameResolver);
 37  1
     }
 38  
 
 39  
     protected Map<String, Resource> createIndexFromEntity(String rootURI, String entity) {
 40  1
             Collection<Page> pages = parse(entity);
 41  1
         Map<String, Resource> index = new HashMap<String, Resource>();
 42  1
         for (Page page : pages) {
 43  4
             String parentName = (page.parent != null ? resolveName(page.parent.title) : null);
 44  4
             String uri = format(PAGE_URI, rootURI, page.title);
 45  4
             Resource resource = new Resource(uri, resolveName(page.title), parentName);
 46  4
             index.put(resource.getName(), resource);
 47  4
         }
 48  1
         return index;
 49  
     }
 50  
 
 51  
         protected String uri(String rootPath) {
 52  0
                 return format(INDEX_URI, rootPath);
 53  
         }
 54  
 
 55  
     private Collection<Page> parse(String entity) {
 56  1
         Gson gson = new Gson();
 57  1
         return gson.<Collection<Page>> fromJson(jsonMember(entity, "wiki_pages"),
 58  1
                 new TypeToken<Collection<Page>>() {
 59  
                 }.getType());
 60  
     }
 61  
 
 62  
     private String jsonMember(String entity, String memberName) {
 63  1
         return new JsonParser().parse(entity).getAsJsonObject().get(memberName).toString();
 64  
     }
 65  
 
 66  22
     private static class Page {
 67  
         private String title;
 68  
         private Page parent;
 69  
     }
 70  
 
 71  
 }