1+ /*-
2+ * -\-\-
3+ * github-api
4+ * --
5+ * Copyright (C) 2016 - 2020 Spotify AB
6+ * --
7+ * Licensed under the Apache License, Version 2.0 (the "License");
8+ * you may not use this file except in compliance with the License.
9+ * You may obtain a copy of the License at
10+ *
11+ * http://www.apache.org/licenses/LICENSE-2.0
12+ *
13+ * Unless required by applicable law or agreed to in writing, software
14+ * distributed under the License is distributed on an "AS IS" BASIS,
15+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ * See the License for the specific language governing permissions and
17+ * limitations under the License.
18+ * -/-/-
19+ */
20+
21+ package com .spotify .github .v3 .clients ;
22+
23+ import com .google .common .io .Resources ;
24+ import com .spotify .github .jackson .Json ;
25+ import com .spotify .github .v3 .actions .workflowjobs .*;
26+ import org .junit .jupiter .api .BeforeEach ;
27+ import org .junit .jupiter .api .Test ;
28+ import org .mockito .ArgumentCaptor ;
29+
30+ import java .io .IOException ;
31+ import java .io .UncheckedIOException ;
32+ import java .util .concurrent .CompletableFuture ;
33+
34+ import static java .nio .charset .StandardCharsets .UTF_8 ;
35+ import static java .util .concurrent .CompletableFuture .completedFuture ;
36+ import static org .hamcrest .MatcherAssert .assertThat ;
37+ import static org .hamcrest .Matchers .notNullValue ;
38+ import static org .hamcrest .core .Is .is ;
39+ import static org .mockito .ArgumentMatchers .any ;
40+ import static org .mockito .ArgumentMatchers .eq ;
41+ import static org .mockito .Mockito .mock ;
42+ import static org .mockito .Mockito .when ;
43+
44+ public class WorkflowJobClientTest {
45+
46+ private static final String FIXTURES_PATH = "com/spotify/github/v3/actions/workflowjobs/" ;
47+ private GitHubClient github ;
48+ private WorkflowJobsClient workflowJobsClient ;
49+ private Json json ;
50+
51+ public static String loadResource (final String path ) {
52+ try {
53+ return Resources .toString (Resources .getResource (path ), UTF_8 );
54+ } catch (IOException e ) {
55+ throw new UncheckedIOException (e );
56+ }
57+ }
58+
59+ @ BeforeEach
60+ public void setUp () {
61+ github = mock (GitHubClient .class );
62+ workflowJobsClient = new WorkflowJobsClient (github , "someowner" , "somerepo" );
63+ json = Json .create ();
64+ when (github .json ()).thenReturn (json );
65+ }
66+
67+ @ Test
68+ public void getWorkflowRun () throws Exception {
69+ final WorkflowJobResponse workflowJobResponse =
70+ json .fromJson (
71+ loadResource (FIXTURES_PATH + "workflowjobs-get-workflowjob-response.json" ), WorkflowJobResponse .class );
72+ final CompletableFuture <WorkflowJobResponse > fixtureResponse = completedFuture (workflowJobResponse );
73+ when (github .request (any (), eq (WorkflowJobResponse .class ), any ())).thenReturn (fixtureResponse );
74+
75+ final CompletableFuture <WorkflowJobResponse > actualResponse =
76+ workflowJobsClient .getWorkflowJob (29679449 , null );
77+
78+ assertThat (actualResponse .get ().id (), is (399444496L ));
79+ assertThat (actualResponse .get ().status (), is (WorkflowJobStatus .completed ));
80+
81+ assertThat (actualResponse .get ().steps (), notNullValue ());
82+ assertThat (actualResponse .get ().steps ().size (), is (10 ));
83+ assertThat (actualResponse .get ().steps ().get (0 ).name (), is ("Set up job" ));
84+ assertThat (actualResponse .get ().steps ().get (1 ).name (), is ("Run actions/checkout@v2" ));
85+ }
86+
87+ @ Test
88+ public void listWorkflowJobs () throws Exception {
89+ final WorkflowJobsResponseList workflowJobsListResponse =
90+ json .fromJson (
91+ loadResource (FIXTURES_PATH + "workflowjobs-list-workflowjobs-response.json" ), WorkflowJobsResponseList .class );
92+ final CompletableFuture <WorkflowJobsResponseList > fixtureResponse = completedFuture (workflowJobsListResponse );
93+ when (github .request (any (), eq (WorkflowJobsResponseList .class ), any ())).thenReturn (fixtureResponse );
94+
95+ final CompletableFuture <WorkflowJobsResponseList > actualResponse =
96+ workflowJobsClient .listWorkflowJobs (159038 , null );
97+
98+ assertThat (actualResponse .get ().totalCount (), is (1 ));
99+ assertThat (actualResponse .get ().jobs ().size (), is (1 ));
100+
101+ assertThat (actualResponse .get ().jobs ().get (0 ).id (), is (399444496L ));
102+ assertThat (actualResponse .get ().jobs ().get (0 ).status (), is (WorkflowJobStatus .completed ));
103+
104+ assertThat (actualResponse .get ().jobs ().get (0 ).steps (), notNullValue ());
105+ assertThat (actualResponse .get ().jobs ().get (0 ).steps ().size (), is (10 ));
106+ assertThat (actualResponse .get ().jobs ().get (0 ).steps ().get (0 ).name (), is ("Set up job" ));
107+ assertThat (actualResponse .get ().jobs ().get (0 ).steps ().get (1 ).name (), is ("Run actions/checkout@v2" ));
108+ }
109+
110+
111+ @ Test
112+ public void listWorkflowRunsFiltered () throws Exception {
113+ ArgumentCaptor <String > pathCaptor = ArgumentCaptor .forClass (String .class );
114+
115+ final WorkflowJobsResponseList workflowJobsListResponse =
116+ json .fromJson (
117+ loadResource (FIXTURES_PATH + "workflowjobs-list-workflowjobs-response.json" ), WorkflowJobsResponseList .class );
118+ final CompletableFuture <WorkflowJobsResponseList > fixtureResponse = completedFuture (workflowJobsListResponse );
119+ when (github .request (pathCaptor .capture (), eq (WorkflowJobsResponseList .class ), any ())).thenReturn (fixtureResponse );
120+
121+ ListWorkflowJobsQueryParams params = ImmutableListWorkflowJobsQueryParams .builder ()
122+ .filter (ListWorkflowJobsQueryParams .Filter .latest )
123+ .build ();
124+
125+ final CompletableFuture <WorkflowJobsResponseList > actualResponse =
126+ workflowJobsClient .listWorkflowJobs (159038 , null );
127+
128+ assertThat (pathCaptor .getValue (), is ("/repos/someowner/somerepo/actions/runs/159038/jobs" ));
129+
130+ assertThat (actualResponse .get ().totalCount (), is (1 ));
131+ assertThat (actualResponse .get ().jobs ().size (), is (1 ));
132+
133+ assertThat (actualResponse .get ().jobs ().get (0 ).id (), is (399444496L ));
134+ assertThat (actualResponse .get ().jobs ().get (0 ).status (), is (WorkflowJobStatus .completed ));
135+
136+ assertThat (actualResponse .get ().jobs ().get (0 ).steps (), notNullValue ());
137+ assertThat (actualResponse .get ().jobs ().get (0 ).steps ().size (), is (10 ));
138+ assertThat (actualResponse .get ().jobs ().get (0 ).steps ().get (0 ).name (), is ("Set up job" ));
139+ assertThat (actualResponse .get ().jobs ().get (0 ).steps ().get (1 ).name (), is ("Run actions/checkout@v2" ));
140+ }
141+
142+ }
0 commit comments