Coverage Report - org.jbehave.core.io.rest.redmine.IndexFromRedmine
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexFromRedmine
91%
34/37
91%
11/12
1.545
IndexFromRedmine$1
100%
1/1
N/A
1.545
IndexFromRedmine$WikiPage
100%
1/1
N/A
1.545
 
 1  
 package org.jbehave.core.io.rest.redmine;
 2  
 
 3  
 import static java.text.MessageFormat.format;
 4  
 import static org.apache.commons.lang.StringUtils.join;
 5  
 
 6  
 import java.util.ArrayList;
 7  
 import java.util.Collection;
 8  
 import java.util.HashMap;
 9  
 import java.util.List;
 10  
 import java.util.Map;
 11  
 
 12  
 import org.jbehave.core.io.rest.RESTClient;
 13  
 import org.jbehave.core.io.rest.RESTClient.Type;
 14  
 import org.jbehave.core.io.rest.Resource;
 15  
 import org.jbehave.core.io.rest.ResourceIndexer;
 16  
 
 17  
 import com.google.gson.Gson;
 18  
 import com.google.gson.JsonParser;
 19  
 import com.google.gson.reflect.TypeToken;
 20  
 
 21  
 /**
 22  
  * Indexes resources from Redmine using the REST API
 23  
  */
 24  
 public class IndexFromRedmine implements ResourceIndexer {
 25  
 
 26  
     private static final String INDEX_URI = "{0}/index.json";
 27  
     private static final String PAGE_URI = "{0}/{1}";
 28  
 
 29  
     private RESTClient client;
 30  
 
 31  
     public IndexFromRedmine() {
 32  1
         this(null, null);
 33  1
     }
 34  
 
 35  1
     public IndexFromRedmine(String username, String password) {
 36  1
         this.client = new RESTClient(Type.JSON, username, password);
 37  1
     }
 38  
 
 39  
     public Map<String, Resource> indexResources(String rootURI) {
 40  0
         return indexResources(rootURI, entity(uri(rootURI)));
 41  
     }
 42  
 
 43  
     public Map<String, Resource> indexResources(String rootURI, String entity) {
 44  1
         Map<String, Resource> index = createIndex(rootURI, parse(entity));
 45  1
         addBreadcrumbs(index);
 46  1
         return index;
 47  
     }
 48  
 
 49  
     private void addBreadcrumbs(Map<String, Resource> index) {
 50  1
         for (Resource resource : index.values()) {
 51  4
             List<String> breadcrumbs = new ArrayList<String>();
 52  4
             collectBreadcrumbs(breadcrumbs, resource, index);
 53  4
             if (!breadcrumbs.isEmpty()) {
 54  2
                 resource.setBreadcrumbs(join(breadcrumbs, "/"));
 55  
             }
 56  4
         }
 57  1
     }
 58  
 
 59  
     private void collectBreadcrumbs(List<String> breadcrumbs, Resource resource, Map<String, Resource> index) {
 60  6
         if (resource.hasParent()) {
 61  2
             String parentName = resource.getParentName();
 62  2
             breadcrumbs.add(0, parentName);
 63  2
             Resource parent = index.get(parentName);
 64  2
             if (parent != null) {
 65  2
                 collectBreadcrumbs(breadcrumbs, parent, index);
 66  
             }
 67  
         }
 68  6
     }
 69  
 
 70  
     private Map<String, Resource> createIndex(String rootURI, Collection<WikiPage> pages) {
 71  1
         Map<String, Resource> index = new HashMap<String, Resource>();
 72  1
         for (WikiPage page : pages) {
 73  4
             String name = page.title;
 74  4
             String parentName = (page.parent != null ? page.parent.title : null);
 75  4
             String uri = format(PAGE_URI, rootURI, name);
 76  4
             Resource resource = new Resource(uri, name, parentName);
 77  4
             index.put(name, resource);
 78  4
         }
 79  1
         return index;
 80  
     }
 81  
 
 82  
     private Collection<WikiPage> parse(String entity) {
 83  1
         Gson gson = new Gson();
 84  1
         return gson.<Collection<WikiPage>> fromJson(jsonMember(entity, "wiki_pages"),
 85  1
                 new TypeToken<Collection<WikiPage>>() {
 86  
                 }.getType());
 87  
     }
 88  
 
 89  
     private String jsonMember(String entity, String memberName) {
 90  1
         return new JsonParser().parse(entity).getAsJsonObject().get(memberName).toString();
 91  
     }
 92  
 
 93  
     private String entity(String uri) {
 94  0
         return client.get(uri);
 95  
     }
 96  
 
 97  
     private String uri(String rootPath) {
 98  0
         return format(INDEX_URI, rootPath);
 99  
     }
 100  
 
 101  18
     private static class WikiPage {
 102  
         private String title;
 103  
         private WikiPage parent;
 104  
     }
 105  
 
 106  
 }