|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2013-2026 Yegor Bugayenko |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | +package com.jcabi.github; |
| 6 | + |
| 7 | +import com.google.common.base.Optional; |
| 8 | +import jakarta.json.Json; |
| 9 | +import jakarta.json.JsonObject; |
| 10 | +import jakarta.json.JsonValue; |
| 11 | +import java.io.IOException; |
| 12 | +import org.hamcrest.MatcherAssert; |
| 13 | +import org.hamcrest.Matchers; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.mockito.Mockito; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test case for {@link Event.Smart}. |
| 19 | + * @since 1.7.0 |
| 20 | + */ |
| 21 | +final class EventTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void readsCommitIdWhenPresent() throws IOException { |
| 25 | + final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51"; |
| 26 | + MatcherAssert.assertThat( |
| 27 | + "Commit SHA is not equal", |
| 28 | + new Event.Smart( |
| 29 | + EventTest.eventWithJson( |
| 30 | + Json.createObjectBuilder() |
| 31 | + .add("commit_id", sha) |
| 32 | + .build() |
| 33 | + ) |
| 34 | + ).commitId(), |
| 35 | + Matchers.equalTo(Optional.of(sha)) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void commitIdIsAbsentWhenJsonNull() throws IOException { |
| 41 | + MatcherAssert.assertThat( |
| 42 | + "Commit ID should be absent when JSON value is null", |
| 43 | + new Event.Smart( |
| 44 | + EventTest.eventWithJson( |
| 45 | + Json.createObjectBuilder() |
| 46 | + .add("commit_id", JsonValue.NULL) |
| 47 | + .build() |
| 48 | + ) |
| 49 | + ).commitId(), |
| 50 | + Matchers.equalTo(Optional.<String>absent()) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void commitIdIsAbsentWhenMissing() throws IOException { |
| 56 | + MatcherAssert.assertThat( |
| 57 | + "Commit ID should be absent when key is missing", |
| 58 | + new Event.Smart( |
| 59 | + EventTest.eventWithJson( |
| 60 | + Json.createObjectBuilder().build() |
| 61 | + ) |
| 62 | + ).commitId(), |
| 63 | + Matchers.equalTo(Optional.<String>absent()) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Build an Event whose json() method returns the given object. |
| 69 | + * @param obj JSON object to expose via Event.json() |
| 70 | + * @return Mocked Event |
| 71 | + * @throws IOException If stubbing fails |
| 72 | + */ |
| 73 | + private static Event eventWithJson(final JsonObject obj) |
| 74 | + throws IOException { |
| 75 | + final Event event = Mockito.mock(Event.class); |
| 76 | + Mockito.doReturn(obj).when(event).json(); |
| 77 | + return event; |
| 78 | + } |
| 79 | +} |
0 commit comments