1
+ package org .commonwl .view .researchobject ;
2
+
3
+ import org .apache .commons .io .FileUtils ;
4
+ import org .apache .taverna .robundle .Bundle ;
5
+ import org .apache .taverna .robundle .Bundles ;
6
+ import org .apache .taverna .robundle .manifest .Manifest ;
7
+ import org .commonwl .view .github .GitHubService ;
8
+ import org .commonwl .view .github .GithubDetails ;
9
+ import org .eclipse .egit .github .core .*;
10
+ import org .junit .Rule ;
11
+ import org .junit .Test ;
12
+ import org .junit .rules .TemporaryFolder ;
13
+ import org .mockito .Mockito ;
14
+ import org .mockito .invocation .InvocationOnMock ;
15
+ import org .mockito .stubbing .Answer ;
16
+
17
+ import java .io .File ;
18
+ import java .util .ArrayList ;
19
+ import java .util .List ;
20
+
21
+ import static org .junit .Assert .*;
22
+ import static org .mockito .Matchers .anyObject ;
23
+ import static org .mockito .Mockito .when ;
24
+
25
+ public class ROBundleTest {
26
+
27
+ /**
28
+ * Use a temporary directory for testing
29
+ */
30
+ @ Rule
31
+ public TemporaryFolder roBundleFolder = new TemporaryFolder ();
32
+
33
+ /**
34
+ * Generate a Research Object bundle from lobstr-v1 and check it
35
+ */
36
+ @ Test
37
+ public void generateAndSaveROBundle () throws Exception {
38
+
39
+ // Get mock Github service
40
+ GitHubService mockGithubService = getMock ();
41
+
42
+ // Create new RO bundle
43
+ GithubDetails lobSTRv1Details = new GithubDetails ("common-workflow-language" , "workflows" ,
44
+ "933bf2a1a1cce32d88f88f136275535da9df0954" , "workflows/lobSTR" );
45
+ ROBundle bundle = new ROBundle (mockGithubService , lobSTRv1Details , "CWL Viewer" ,
46
+ "https://view.commonwl.org" , 5242880 );
47
+
48
+ // Check bundle exists
49
+ assertNotNull (bundle .getBundle ());
50
+
51
+ // Check basic manifest metadata
52
+ Manifest manifest = bundle .getBundle ().getManifest ();
53
+ assertEquals ("CWL Viewer" , manifest .getCreatedBy ().getName ());
54
+ assertEquals ("https://view.commonwl.org" , manifest .getCreatedBy ().getUri ().toString ());
55
+ assertEquals ("Mark Robinson" , manifest .getAuthoredBy ().get (0 ).getName ());
56
+ assertEquals (10 , manifest .getAggregates ().size ());
57
+
58
+ // Save and check it exists in the temporary folder
59
+ bundle .saveToFile (roBundleFolder .getRoot ().toPath ());
60
+ File [] fileList = roBundleFolder .getRoot ().listFiles ();
61
+ assertTrue (fileList .length == 1 );
62
+ for (File ro : fileList ) {
63
+ assertTrue (ro .getName ().endsWith (".zip" ));
64
+ Bundle savedBundle = Bundles .openBundle (ro .toPath ());
65
+ assertNotNull (savedBundle );
66
+ }
67
+
68
+ }
69
+
70
+ /**
71
+ * Get a mock Github service redirecting file downloads to file system
72
+ * and providing translation from the file system to Github API returns
73
+ * @return The constructed mock object
74
+ */
75
+ private GitHubService getMock () throws Exception {
76
+
77
+ GitHubService mockGithubService = Mockito .mock (GitHubService .class );
78
+ Answer fileAnswer = new Answer <String >() {
79
+ @ Override
80
+ public String answer (InvocationOnMock invocation ) throws Throwable {
81
+ Object [] args = invocation .getArguments ();
82
+ GithubDetails details = (GithubDetails ) args [0 ];
83
+ File workflowFile = new File ("src/test/resources/cwl/lobstr-v1/"
84
+ + details .getPath ().replace ("workflows/lobSTR/" , "" ));
85
+ return FileUtils .readFileToString (workflowFile );
86
+ }
87
+ };
88
+ when (mockGithubService .downloadFile (anyObject ())).thenAnswer (fileAnswer );
89
+ when (mockGithubService .downloadFile (anyObject (), anyObject ())).thenAnswer (fileAnswer );
90
+
91
+ Answer contentsAnswer = new Answer <List <RepositoryContents >>() {
92
+ @ Override
93
+ public List <RepositoryContents > answer (InvocationOnMock invocation ) throws Throwable {
94
+ File [] fileList = new File ("src/test/resources/cwl/lobstr-v1/" ).listFiles ();
95
+
96
+ // Add all files from lobstr-v1 directory
97
+ List <RepositoryContents > returnList = new ArrayList <>();
98
+ for (File thisFile : fileList ) {
99
+ if (thisFile .isFile ()) {
100
+ RepositoryContents singleFile = new RepositoryContents ();
101
+ singleFile .setType (GitHubService .TYPE_FILE );
102
+ singleFile .setName (thisFile .getName ());
103
+ singleFile .setPath ("workflows/lobSTR/" + thisFile .getName ());
104
+ returnList .add (singleFile );
105
+ }
106
+ }
107
+
108
+ return returnList ;
109
+ }
110
+ };
111
+ when (mockGithubService .getContents (anyObject ())).thenAnswer (contentsAnswer );
112
+
113
+ Answer commitsAnswer = new Answer <List <RepositoryCommit >>() {
114
+ @ Override
115
+ public List <RepositoryCommit > answer (InvocationOnMock invocation ) throws Throwable {
116
+ Object [] args = invocation .getArguments ();
117
+ GithubDetails details = (GithubDetails ) args [0 ];
118
+
119
+ // Make up a commit for the file and return it
120
+ List <RepositoryCommit > commitList = new ArrayList <>();
121
+ RepositoryCommit commit = new RepositoryCommit ();
122
+
123
+ User author = new User ();
124
+ author .setName ("Mark Robinson" );
125
+ author .setHtmlUrl ("https://github.com/MarkRobbo" );
126
+ commit .setAuthor (author );
127
+ commit .setSha ("933bf2a1a1cce32d88f88f136275535da9df0954" );
128
+ commit .setCommit (new Commit ().setAuthor (new CommitUser ().setName ("Mark Robinson" )));
129
+ commitList .add (commit );
130
+
131
+ return commitList ;
132
+ }
133
+ };
134
+ when (mockGithubService .getCommits (anyObject ())).thenAnswer (commitsAnswer );
135
+
136
+ return mockGithubService ;
137
+ }
138
+
139
+ }
0 commit comments