Skip to content

Commit bdbfc57

Browse files
committed
Unit tests added
1 parent 25662bf commit bdbfc57

File tree

6 files changed

+264
-2
lines changed

6 files changed

+264
-2
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2023-2024 Tracehub.git
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to read
6+
* the Software only. Permissions is hereby NOT GRANTED to use, copy, modify,
7+
* merge, publish, distribute, sublicense, and/or sell copies of the Software.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
12+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15+
* SOFTWARE.
16+
*/
17+
18+
package git.tracehub.pmo.controller;
19+
20+
import git.tracehub.pmo.exception.ResourceAlreadyExistsException;
21+
import git.tracehub.pmo.exception.ResourceNotFoundException;
22+
import io.github.eocqrs.eokson.Jocument;
23+
import io.github.eocqrs.eokson.MutableJson;
24+
import org.cactoos.list.ListOf;
25+
import org.hamcrest.MatcherAssert;
26+
import org.hamcrest.core.IsEqual;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.Mockito;
30+
import org.mockito.junit.jupiter.MockitoExtension;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.ResponseEntity;
33+
import org.springframework.security.access.AccessDeniedException;
34+
import org.springframework.validation.BindingResult;
35+
import org.springframework.validation.FieldError;
36+
import org.springframework.web.bind.MethodArgumentNotValidException;
37+
38+
/**
39+
* Test suite for {@link AdviceController}.
40+
*
41+
* @since 0.0.0
42+
*/
43+
@ExtendWith(MockitoExtension.class)
44+
final class AdviceControllerTest {
45+
46+
/**
47+
* Controller.
48+
*/
49+
private final AdviceController controller = new AdviceController();
50+
51+
@Test
52+
void handlesMethodArgumentNotValidException() {
53+
final String message = "Message";
54+
final MethodArgumentNotValidException exception =
55+
Mockito.mock(MethodArgumentNotValidException.class);
56+
final BindingResult result = Mockito.mock(BindingResult.class);
57+
Mockito.when(exception.getBindingResult()).thenReturn(result);
58+
Mockito.when(result.getFieldErrors()).thenReturn(
59+
new ListOf<>(new FieldError("object", "field", message))
60+
);
61+
MatcherAssert.assertThat(
62+
"MethodArgumentNotValidException isn't handled",
63+
this.controller.handle(exception),
64+
new IsEqual<>(
65+
new ResponseEntity<>(
66+
new Jocument(
67+
new MutableJson()
68+
.with("message", message)
69+
).byteArray(),
70+
HttpStatus.BAD_REQUEST
71+
)
72+
)
73+
);
74+
}
75+
76+
@Test
77+
void handlesResourceAlreadyExistsException() {
78+
final String message = "Message";
79+
final ResourceAlreadyExistsException exception =
80+
new ResourceAlreadyExistsException(message);
81+
MatcherAssert.assertThat(
82+
"ResourceAlreadyExistsException isn't handled",
83+
this.controller.handle(exception),
84+
new IsEqual<>(
85+
new ResponseEntity<>(
86+
new Jocument(
87+
new MutableJson()
88+
.with("message", message)
89+
).byteArray(),
90+
HttpStatus.BAD_REQUEST
91+
)
92+
)
93+
);
94+
}
95+
96+
@Test
97+
void handlesResourceNotFoundException() {
98+
final String message = "Message";
99+
final ResourceNotFoundException exception =
100+
new ResourceNotFoundException(message);
101+
MatcherAssert.assertThat(
102+
"ResourceNotFoundException isn't handled",
103+
this.controller.handle(exception),
104+
new IsEqual<>(
105+
new ResponseEntity<>(
106+
new Jocument(
107+
new MutableJson()
108+
.with("message", message)
109+
).byteArray(),
110+
HttpStatus.NOT_FOUND
111+
)
112+
)
113+
);
114+
}
115+
116+
@Test
117+
void handlesAccessDeniedException() {
118+
final String message = "Message";
119+
final AccessDeniedException exception =
120+
new AccessDeniedException(message);
121+
MatcherAssert.assertThat(
122+
"AccessDeniedException isn't handled",
123+
this.controller.handle(exception),
124+
new IsEqual<>(
125+
new ResponseEntity<>(
126+
new Jocument(
127+
new MutableJson()
128+
.with("message", message)
129+
).byteArray(),
130+
HttpStatus.FORBIDDEN
131+
)
132+
)
133+
);
134+
}
135+
136+
@Test
137+
void handlesException() {
138+
final String message = "Message";
139+
final Exception exception = new Exception(message);
140+
MatcherAssert.assertThat(
141+
"Exception isn't handled",
142+
this.controller.handle(exception),
143+
new IsEqual<>(
144+
new ResponseEntity<>(
145+
new Jocument(
146+
new MutableJson()
147+
.with("message", message)
148+
).byteArray(),
149+
HttpStatus.INTERNAL_SERVER_ERROR
150+
)
151+
)
152+
);
153+
}
154+
155+
156+
157+
158+
}

src/test/java/git/tracehub/pmo/platforms/github/CreateLabelsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717

1818
package git.tracehub.pmo.platforms.github;
1919

20+
import com.jcabi.github.Labels;
2021
import com.jcabi.github.Repo;
2122
import com.jcabi.github.mock.MkGithub;
23+
import git.tracehub.pmo.platforms.ColorInHex;
2224
import git.tracehub.pmo.platforms.Label;
2325
import java.awt.Color;
2426
import java.io.IOException;
2527
import org.cactoos.list.ListOf;
2628
import org.hamcrest.MatcherAssert;
2729
import org.hamcrest.core.IsEqual;
30+
import org.junit.jupiter.api.Assertions;
2831
import org.junit.jupiter.api.Test;
32+
import org.mockito.Mockito;
2933

3034
/**
3135
* Test suite for {@link CreateLabels}.
@@ -47,4 +51,24 @@ void createsLabelSuccessfully() throws IOException {
4751
);
4852
}
4953

54+
@Test
55+
void throwsOnInvalidLabel() throws IOException {
56+
final Label label = new Label("new", Color.BLUE);
57+
final Repo repo = Mockito.mock(Repo.class);
58+
final Labels labels = Mockito.mock(Labels.class);
59+
Mockito.when(repo.labels()).thenReturn(labels);
60+
Mockito.doThrow(IOException.class).when(labels).create(
61+
label.getName(),
62+
new ColorInHex(label.getColor()).value()
63+
);
64+
Assertions.assertThrows(
65+
IOException.class,
66+
() -> new CreateLabels(
67+
repo,
68+
new ListOf<>(label)
69+
).exec(),
70+
"Exception is not thrown or valid"
71+
);
72+
}
73+
5074
}

src/test/java/git/tracehub/pmo/platforms/github/InviteCollaboratorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
package git.tracehub.pmo.platforms.github;
1919

20+
import com.jcabi.github.Collaborators;
2021
import com.jcabi.github.Repo;
2122
import com.jcabi.github.mock.MkGithub;
2223
import java.io.IOException;
2324
import org.hamcrest.MatcherAssert;
2425
import org.hamcrest.core.IsEqual;
26+
import org.junit.jupiter.api.Assertions;
2527
import org.junit.jupiter.api.Test;
28+
import org.mockito.Mockito;
2629

2730
/**
2831
* Test suite for {@link InviteCollaborator}.
@@ -44,4 +47,18 @@ void invitesCollaboratorSuccessfully() throws IOException {
4447
);
4548
}
4649

50+
@Test
51+
void throwsOnInvalidRepository() throws IOException {
52+
final String collaborator = "name";
53+
final Repo repo = Mockito.mock(Repo.class);
54+
final Collaborators collaborators = Mockito.mock(Collaborators.class);
55+
Mockito.when(repo.collaborators()).thenReturn(collaborators);
56+
Mockito.doThrow(IOException.class).when(collaborators).add(collaborator);
57+
Assertions.assertThrows(
58+
IOException.class,
59+
() -> new InviteCollaborator(repo, collaborator).exec(),
60+
"Exception is not thrown or valid"
61+
);
62+
}
63+
4764
}

src/test/java/git/tracehub/pmo/platforms/github/webhook/CreateWebhookTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.cactoos.list.ListOf;
2626
import org.hamcrest.MatcherAssert;
2727
import org.hamcrest.core.IsEqual;
28+
import org.junit.jupiter.api.Assertions;
2829
import org.junit.jupiter.api.Test;
2930

3031
/**
@@ -90,4 +91,21 @@ void doesNotCreateWebhookWhenAlreadyExists() throws IOException {
9091
container.stop();
9192
}
9293

94+
@Test
95+
void throwsOnInvalidHost() {
96+
final String url = "http://localhost:1000";
97+
final String location = "user/repo";
98+
Assertions.assertThrows(
99+
IOException.class,
100+
() -> new CreateWebhook(
101+
url,
102+
"token",
103+
location,
104+
"test/url",
105+
new ListOf<>("push")
106+
).exec(),
107+
"Exception is not thrown or valid"
108+
);
109+
}
110+
93111
}

src/test/java/git/tracehub/pmo/project/DefaultProjectsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,15 @@ void throwsOnCreatingInvalidProject() throws SQLException {
201201
).affirm();
202202
}
203203

204+
@Test
205+
@SuppressWarnings("JTCOP.RuleAssertionMessage")
206+
void throwsOnInvalidUser() throws SQLException {
207+
Mockito.when(this.set.next()).thenThrow(SQLException.class);
208+
new Assertion<>(
209+
"Exception is not thrown or valid",
210+
() -> this.projects.byUser("email"),
211+
new Throws<>(SQLException.class)
212+
).affirm();
213+
}
214+
204215
}

src/test/java/git/tracehub/pmo/secret/EncryptedSecretsTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717

1818
package git.tracehub.pmo.secret;
1919

20+
import java.io.IOException;
2021
import java.util.List;
2122
import java.util.UUID;
23+
import org.cactoos.Scalar;
2224
import org.cactoos.list.ListOf;
2325
import org.hamcrest.MatcherAssert;
2426
import org.hamcrest.Matchers;
2527
import org.hamcrest.core.IsEqual;
2628
import org.jasypt.util.text.TextEncryptor;
2729
import org.junit.jupiter.api.Test;
2830
import org.junit.jupiter.api.extension.ExtendWith;
31+
import org.llorllale.cactoos.matchers.Assertion;
32+
import org.llorllale.cactoos.matchers.Throws;
2933
import org.mockito.InjectMocks;
3034
import org.mockito.Mock;
3135
import org.mockito.Mockito;
@@ -113,7 +117,8 @@ void createsSecret() {
113117
"unique key",
114118
"unique value"
115119
);
116-
Mockito.when(this.origin.create(Mockito.any())).thenReturn(expected);
120+
Mockito.when(this.origin.create(Mockito.any(Scalar.class)))
121+
.thenReturn(expected);
117122
final Secret secret = this.secrets.create(() -> expected);
118123
MatcherAssert.assertThat(
119124
"Secret %s isn't created".formatted(secret),
@@ -134,7 +139,8 @@ void updatesSecret() {
134139
"key",
135140
"value"
136141
);
137-
Mockito.when(this.origin.update(Mockito.any())).thenReturn(expected);
142+
Mockito.when(this.origin.update(Mockito.any(Scalar.class)))
143+
.thenReturn(expected);
138144
final Secret secret = this.secrets.update(() -> expected);
139145
MatcherAssert.assertThat(
140146
"Secret %s isn't updated".formatted(secret),
@@ -164,4 +170,32 @@ void existsSecret() {
164170
);
165171
}
166172

173+
@Test
174+
@SuppressWarnings("JTCOP.RuleAssertionMessage")
175+
void throwsOnInvalidSecretForCreating() {
176+
new Assertion<>(
177+
"Exception is not thrown",
178+
() -> this.secrets.create(
179+
() -> {
180+
throw new IOException();
181+
}
182+
),
183+
new Throws<>(IOException.class)
184+
).affirm();
185+
}
186+
187+
@Test
188+
@SuppressWarnings("JTCOP.RuleAssertionMessage")
189+
void throwsOnInvalidSecretForUpdating() {
190+
new Assertion<>(
191+
"Exception is not thrown",
192+
() -> this.secrets.update(
193+
() -> {
194+
throw new IOException();
195+
}
196+
),
197+
new Throws<>(IOException.class)
198+
).affirm();
199+
}
200+
167201
}

0 commit comments

Comments
 (0)