Skip to content

Commit 2307c49

Browse files
committed
Added WorkflowJobsClient tests
1 parent cf35a7e commit 2307c49

File tree

3 files changed

+363
-0
lines changed

3 files changed

+363
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"id": 399444496,
3+
"run_id": 29679449,
4+
"run_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/29679449",
5+
"node_id": "MDEyOldvcmtmbG93IEpvYjM5OTQ0NDQ5Ng==",
6+
"head_sha": "f83a356604ae3c5d03e1b46ef4d1ca77d64a90b0",
7+
"url": "https://api.github.com/repos/octo-org/octo-repo/actions/jobs/399444496",
8+
"html_url": "https://github.com/octo-org/octo-repo/runs/29679449/jobs/399444496",
9+
"status": "completed",
10+
"conclusion": "success",
11+
"started_at": "2020-01-20T17:42:40Z",
12+
"completed_at": "2020-01-20T17:44:39Z",
13+
"name": "build",
14+
"steps": [
15+
{
16+
"name": "Set up job",
17+
"status": "completed",
18+
"conclusion": "success",
19+
"number": 1,
20+
"started_at": "2020-01-20T09:42:40.000-08:00",
21+
"completed_at": "2020-01-20T09:42:41.000-08:00"
22+
},
23+
{
24+
"name": "Run actions/checkout@v2",
25+
"status": "completed",
26+
"conclusion": "success",
27+
"number": 2,
28+
"started_at": "2020-01-20T09:42:41.000-08:00",
29+
"completed_at": "2020-01-20T09:42:45.000-08:00"
30+
},
31+
{
32+
"name": "Set up Ruby",
33+
"status": "completed",
34+
"conclusion": "success",
35+
"number": 3,
36+
"started_at": "2020-01-20T09:42:45.000-08:00",
37+
"completed_at": "2020-01-20T09:42:45.000-08:00"
38+
},
39+
{
40+
"name": "Run actions/cache@v3",
41+
"status": "completed",
42+
"conclusion": "success",
43+
"number": 4,
44+
"started_at": "2020-01-20T09:42:45.000-08:00",
45+
"completed_at": "2020-01-20T09:42:48.000-08:00"
46+
},
47+
{
48+
"name": "Install Bundler",
49+
"status": "completed",
50+
"conclusion": "success",
51+
"number": 5,
52+
"started_at": "2020-01-20T09:42:48.000-08:00",
53+
"completed_at": "2020-01-20T09:42:52.000-08:00"
54+
},
55+
{
56+
"name": "Install Gems",
57+
"status": "completed",
58+
"conclusion": "success",
59+
"number": 6,
60+
"started_at": "2020-01-20T09:42:52.000-08:00",
61+
"completed_at": "2020-01-20T09:42:53.000-08:00"
62+
},
63+
{
64+
"name": "Run Tests",
65+
"status": "completed",
66+
"conclusion": "success",
67+
"number": 7,
68+
"started_at": "2020-01-20T09:42:53.000-08:00",
69+
"completed_at": "2020-01-20T09:42:59.000-08:00"
70+
},
71+
{
72+
"name": "Deploy to Heroku",
73+
"status": "completed",
74+
"conclusion": "success",
75+
"number": 8,
76+
"started_at": "2020-01-20T09:42:59.000-08:00",
77+
"completed_at": "2020-01-20T09:44:39.000-08:00"
78+
},
79+
{
80+
"name": "Post actions/cache@v3",
81+
"status": "completed",
82+
"conclusion": "success",
83+
"number": 16,
84+
"started_at": "2020-01-20T09:44:39.000-08:00",
85+
"completed_at": "2020-01-20T09:44:39.000-08:00"
86+
},
87+
{
88+
"name": "Complete job",
89+
"status": "completed",
90+
"conclusion": "success",
91+
"number": 17,
92+
"started_at": "2020-01-20T09:44:39.000-08:00",
93+
"completed_at": "2020-01-20T09:44:39.000-08:00"
94+
}
95+
],
96+
"check_run_url": "https://api.github.com/repos/octo-org/octo-repo/check-runs/399444496",
97+
"labels": [
98+
"self-hosted",
99+
"foo",
100+
"bar"
101+
],
102+
"runner_id": 1,
103+
"runner_name": "my runner",
104+
"runner_group_id": 2,
105+
"runner_group_name": "my runner group",
106+
"workflow_name": "CI",
107+
"head_branch": "main"
108+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"total_count": 1,
3+
"jobs": [
4+
{
5+
"id": 399444496,
6+
"run_id": 29679449,
7+
"run_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/29679449",
8+
"node_id": "MDEyOldvcmtmbG93IEpvYjM5OTQ0NDQ5Ng==",
9+
"head_sha": "f83a356604ae3c5d03e1b46ef4d1ca77d64a90b0",
10+
"url": "https://api.github.com/repos/octo-org/octo-repo/actions/jobs/399444496",
11+
"html_url": "https://github.com/octo-org/octo-repo/runs/29679449/jobs/399444496",
12+
"status": "completed",
13+
"conclusion": "success",
14+
"started_at": "2020-01-20T17:42:40Z",
15+
"completed_at": "2020-01-20T17:44:39Z",
16+
"name": "build",
17+
"steps": [
18+
{
19+
"name": "Set up job",
20+
"status": "completed",
21+
"conclusion": "success",
22+
"number": 1,
23+
"started_at": "2020-01-20T09:42:40.000-08:00",
24+
"completed_at": "2020-01-20T09:42:41.000-08:00"
25+
},
26+
{
27+
"name": "Run actions/checkout@v2",
28+
"status": "completed",
29+
"conclusion": "success",
30+
"number": 2,
31+
"started_at": "2020-01-20T09:42:41.000-08:00",
32+
"completed_at": "2020-01-20T09:42:45.000-08:00"
33+
},
34+
{
35+
"name": "Set up Ruby",
36+
"status": "completed",
37+
"conclusion": "success",
38+
"number": 3,
39+
"started_at": "2020-01-20T09:42:45.000-08:00",
40+
"completed_at": "2020-01-20T09:42:45.000-08:00"
41+
},
42+
{
43+
"name": "Run actions/cache@v3",
44+
"status": "completed",
45+
"conclusion": "success",
46+
"number": 4,
47+
"started_at": "2020-01-20T09:42:45.000-08:00",
48+
"completed_at": "2020-01-20T09:42:48.000-08:00"
49+
},
50+
{
51+
"name": "Install Bundler",
52+
"status": "completed",
53+
"conclusion": "success",
54+
"number": 5,
55+
"started_at": "2020-01-20T09:42:48.000-08:00",
56+
"completed_at": "2020-01-20T09:42:52.000-08:00"
57+
},
58+
{
59+
"name": "Install Gems",
60+
"status": "completed",
61+
"conclusion": "success",
62+
"number": 6,
63+
"started_at": "2020-01-20T09:42:52.000-08:00",
64+
"completed_at": "2020-01-20T09:42:53.000-08:00"
65+
},
66+
{
67+
"name": "Run Tests",
68+
"status": "completed",
69+
"conclusion": "success",
70+
"number": 7,
71+
"started_at": "2020-01-20T09:42:53.000-08:00",
72+
"completed_at": "2020-01-20T09:42:59.000-08:00"
73+
},
74+
{
75+
"name": "Deploy to Heroku",
76+
"status": "completed",
77+
"conclusion": "success",
78+
"number": 8,
79+
"started_at": "2020-01-20T09:42:59.000-08:00",
80+
"completed_at": "2020-01-20T09:44:39.000-08:00"
81+
},
82+
{
83+
"name": "Post actions/cache@v3",
84+
"status": "completed",
85+
"conclusion": "success",
86+
"number": 16,
87+
"started_at": "2020-01-20T09:44:39.000-08:00",
88+
"completed_at": "2020-01-20T09:44:39.000-08:00"
89+
},
90+
{
91+
"name": "Complete job",
92+
"status": "completed",
93+
"conclusion": "success",
94+
"number": 17,
95+
"started_at": "2020-01-20T09:44:39.000-08:00",
96+
"completed_at": "2020-01-20T09:44:39.000-08:00"
97+
}
98+
],
99+
"check_run_url": "https://api.github.com/repos/octo-org/octo-repo/check-runs/399444496",
100+
"labels": [
101+
"self-hosted",
102+
"foo",
103+
"bar"
104+
],
105+
"runner_id": 1,
106+
"runner_name": "my runner",
107+
"runner_group_id": 2,
108+
"runner_group_name": "my runner group",
109+
"workflow_name": "CI",
110+
"head_branch": "main"
111+
}
112+
]
113+
}

0 commit comments

Comments
 (0)