##### Coverage Report - org.jbehave.core.io.google.LoadOdtFromGoogle

Classes in this File | Line Coverage | Branch Coverage | Complexity
LoadOdtFromGoogle |
69% | 23/33
50% | 1/2
| 1.9090909090909092;1.909
LoadOdtFromGoogle$GoogleAccessFailed |
100% | 2/2
N/A | N/A
| 1.9090909090909092;1.909
LoadOdtFromGoogle$GoogleDocumentNotFound |
0% | 0/2
N/A | N/A
| 1.9090909090909092;1.909
LoadOdtFromGoogle$GoogleMediaExportFailed |
0% | 0/2
N/A | N/A
| 1.9090909090909092;1.909

1 |  |
     package org.jbehave.core.io.google;
2 |  |
3 |  |
     import java.io.IOException;
4 |  |
     import java.io.InputStream;
5 |  |
     import java.net.MalformedURLException;
6 |  |
     import java.net.URL;
7 |  |
     import java.util.List;
8 |  |
9 |  |
     import org.jbehave.core.io.odf.LoadOdtFromURL;
10 |  |
11 |  |
     import com.google.gdata.client.DocumentQuery;
12 |  |
     import com.google.gdata.client.docs.DocsService;
13 |  |
     import com.google.gdata.data.MediaContent;
14 |  |
     import com.google.gdata.data.docs.DocumentListEntry;
15 |  |
     import com.google.gdata.data.docs.DocumentListFeed;
16 |  |
     import com.google.gdata.data.media.MediaSource;
17 |  |
     import com.google.gdata.util.AuthenticationException;
18 |  |
     import com.google.gdata.util.ServiceException;
19 |  |
20 |  |
     public class LoadOdtFromGoogle extends LoadOdtFromURL {
21 |  |
22 | 1 |
         private static final DocsService DEFAULT_DOCS_SERVICE = new DocsService("jbehave");
23 |  |
         private static final String DEFAULT_FEED_URI = "https://docs.google.com/feeds/default/private/full/";
24 |  |
         private final DocsService service;
25 |  |
         private final String feedURI;
26 |  |
27 |  |
         public LoadOdtFromGoogle(String username, String password) {
28 | 1 |
             this(username, password, DEFAULT_FEED_URI);
29 | 0 |
         }
30 |  |
31 |  |
         public LoadOdtFromGoogle(String username, String password, String feedURI) {
32 | 1 |
             this(username, password, feedURI, DEFAULT_DOCS_SERVICE);
33 | 0 |
         }
34 |  |
35 | 3 |
         public LoadOdtFromGoogle(String username, String password, String feedURI, DocsService service) {
36 | 3 |
             this.service = service;
37 | 3 |
             this.feedURI = feedURI;
38 |  |
             try {
39 | 3 |
                 service.setUserCredentials(username, password);
40 | 1 |
             } catch (AuthenticationException e) {
41 | 1 |
                 throw new GoogleAccessFailed(username, e);
42 | 2 |
             }
43 | 2 |
         }
44 |  |
45 |  |
         protected InputStream resourceAsStream(String title) throws IOException, MalformedURLException {
46 |  |
             try {
47 | 2 |
                 return documentAsStream(exportURL(title));
48 | 0 |
             } catch (ServiceException e) {
49 | 0 |
                 throw new IOException(e);
50 |  |
             }
51 |  |
         }
52 |  |
53 |  |
         private String exportURL(String title) throws IOException, ServiceException, MalformedURLException {
54 | 2 |
             DocumentQuery query = documentQuery(title);
55 | 2 |
             List<DocumentListEntry> entries = service.getFeed(query, DocumentListFeed.class).getEntries();
56 | 1 |
             if (entries.isEmpty()) {
57 | 0 |
                 throw new GoogleDocumentNotFound(title);
58 |  |
             }
59 | 1 |
             return ((MediaContent) entries.get(0).getContent()).getUri() + "&exportFormat=odt";
60 |  |
         }
61 |  |
62 |  |
         DocumentQuery documentQuery(String title) throws MalformedURLException {
63 | 1 |
             DocumentQuery query = new DocumentQuery(new URL(feedURI));
64 | 1 |
             query.setTitleQuery(title);
65 | 1 |
             query.setTitleExact(true);
66 | 1 |
             query.setMaxResults(1);
67 | 1 |
             return query;
68 |  |
         }
69 |  |
70 |  |
         private InputStream documentAsStream(String url) throws IOException, MalformedURLException {
71 |  |
             try {
72 | 1 |
                 MediaSource ms = service.getMedia(mediaContent(url));
73 | 1 |
                 return ms.getInputStream();
74 | 0 |
             } catch (ServiceException e) {
75 | 0 |
                 throw new GoogleMediaExportFailed(url, e);
76 |  |
             }
77 |  |
         }
78 |  |
79 |  |
         MediaContent mediaContent(String url) {
80 | 0 |
             MediaContent mc = new MediaContent();
81 | 0 |
             mc.setUri(url);
82 | 0 |
             return mc;
83 |  |
         }
84 |  |
85 |  |
         @SuppressWarnings("serial")
86 |  |
         public static class GoogleAccessFailed extends RuntimeException {
87 |  |
88 |  |
             public GoogleAccessFailed(String username, Throwable cause) {
89 | 1 |
                 super("Google access failed for user " + username, cause);
90 | 1 |
             }
91 |  |
92 |  |
         }
93 |  |
94 |  |
         @SuppressWarnings("serial")
95 |  |
         public static class GoogleDocumentNotFound extends RuntimeException {
96 |  |
97 |  |
             public GoogleDocumentNotFound(String title) {
98 | 0 |
                 super("Failed to find Google document from " + title);
99 | 0 |
             }
100 |  |
101 |  |
         }
102 |  |
103 |  |
         @SuppressWarnings("serial")
104 |  |
         public static class GoogleMediaExportFailed extends RuntimeException {
105 |  |
106 |  |
             public GoogleMediaExportFailed(String url, Throwable cause) {
107 | 0 |
                 super("Failed to export Google media from " + url, cause);
108 | 0 |
             }
109 |  |
110 |  |
         }
111 |  |
     }