1
2
3
4
5 package com.jcabi.github;
6
7 import jakarta.json.Json;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.text.ParseException;
13 import org.hamcrest.MatcherAssert;
14 import org.hamcrest.Matchers;
15 import org.junit.jupiter.api.Test;
16 import org.mockito.Mockito;
17
18
19
20
21
22
23 @SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
24 final class ReleaseAssetTest {
25
26 @Test
27 void fetchesUrl() throws IOException, MalformedURLException, URISyntaxException {
28 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
29
30 final String prop = "https://api.github.com/repos/octo/Hello/releases/assets/1";
31 Mockito.doReturn(
32 Json.createObjectBuilder()
33 .add("url", prop)
34 .build()
35 ).when(asset).json();
36 MatcherAssert.assertThat(
37 "Values are not equal",
38 new ReleaseAsset.Smart(asset).url(),
39 Matchers.is(new URI(prop).toURL())
40 );
41 }
42
43 @Test
44 void fetchesName() throws IOException {
45 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
46 final String prop = "assetname.ext";
47 Mockito.doReturn(
48 Json.createObjectBuilder()
49 .add("name", prop)
50 .build()
51 ).when(asset).json();
52 MatcherAssert.assertThat(
53 "Values are not equal",
54 new ReleaseAsset.Smart(asset).name(),
55 Matchers.is(prop)
56 );
57 }
58
59 @Test
60 void fetchesLabel() throws IOException {
61 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
62 final String prop = "short description";
63 Mockito.doReturn(
64 Json.createObjectBuilder()
65 .add("label", prop)
66 .build()
67 ).when(asset).json();
68 MatcherAssert.assertThat(
69 "Values are not equal",
70 new ReleaseAsset.Smart(asset).label(),
71 Matchers.is(prop)
72 );
73 }
74
75 @Test
76 void fetchesState() throws IOException {
77 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
78 final String prop = "uploaded";
79 Mockito.doReturn(
80 Json.createObjectBuilder()
81 .add("state", prop)
82 .build()
83 ).when(asset).json();
84 MatcherAssert.assertThat(
85 "Values are not equal",
86 new ReleaseAsset.Smart(asset).state(),
87 Matchers.is(prop)
88 );
89 }
90
91 @Test
92 void fetchesContentType() throws IOException {
93 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
94 final String prop = "application/zip";
95 Mockito.doReturn(
96 Json.createObjectBuilder()
97 .add("content_type", prop)
98 .build()
99 ).when(asset).json();
100 MatcherAssert.assertThat(
101 "Values are not equal",
102 new ReleaseAsset.Smart(asset).contentType(),
103 Matchers.is(prop)
104 );
105 }
106
107 @Test
108 void fetchesSize() throws IOException {
109 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
110 final int prop = 1024;
111 Mockito.doReturn(
112 Json.createObjectBuilder()
113 .add("size", prop)
114 .build()
115 ).when(asset).json();
116 MatcherAssert.assertThat(
117 "Values are not equal",
118 new ReleaseAsset.Smart(asset).size(),
119 Matchers.is(prop)
120 );
121 }
122
123 @Test
124 void fetchesDownloadCount() throws IOException {
125 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
126 final int prop = 42;
127 Mockito.doReturn(
128 Json.createObjectBuilder()
129 .add("download_count", prop)
130 .build()
131 ).when(asset).json();
132 MatcherAssert.assertThat(
133 "Values are not equal",
134 new ReleaseAsset.Smart(asset).downloadCount(),
135 Matchers.is(prop)
136 );
137 }
138
139 @Test
140 void fetchesCreatedAt() throws IOException, ParseException {
141 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
142 final String prop = "2013-02-27T19:35:32Z";
143 Mockito.doReturn(
144 Json.createObjectBuilder()
145 .add("created_at", prop)
146 .build()
147 ).when(asset).json();
148 MatcherAssert.assertThat(
149 "Values are not equal",
150 new ReleaseAsset.Smart(asset).createdAt(),
151 Matchers.equalTo(new GitHub.Time(prop).date())
152 );
153 }
154
155 @Test
156 void fetchesUpdatedAt() throws IOException, ParseException {
157 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
158 final String prop = "2013-02-27T19:35:32Z";
159 Mockito.doReturn(
160 Json.createObjectBuilder()
161 .add("updated_at", prop)
162 .build()
163 ).when(asset).json();
164 MatcherAssert.assertThat(
165 "Values are not equal",
166 new ReleaseAsset.Smart(asset).updatedAt(),
167 Matchers.equalTo(new GitHub.Time(prop).date())
168 );
169 }
170
171 @Test
172 void updatesName() throws IOException {
173 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
174 final String prop = "new_name";
175 new ReleaseAsset.Smart(asset).name(prop);
176 Mockito.verify(asset).patch(
177 Json.createObjectBuilder().add("name", prop).build()
178 );
179 }
180
181 @Test
182 void updatesLabel() throws IOException {
183 final ReleaseAsset asset = Mockito.mock(ReleaseAsset.class);
184 final String prop = "new_label";
185 new ReleaseAsset.Smart(asset).label(prop);
186 Mockito.verify(asset).patch(
187 Json.createObjectBuilder().add("label", prop).build()
188 );
189 }
190 }