1 /**
2 * Copyright (c) 2013-2023, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.github;
31
32 import com.google.common.base.Optional;
33 import javax.json.Json;
34 import org.hamcrest.MatcherAssert;
35 import org.hamcrest.Matchers;
36 import org.junit.Test;
37
38 /**
39 * Test case for {@link Statuses}.
40 * @author Chris Rebert (github@chrisrebert.com)
41 * @version $Id: b0803bd2250f58a46cc0f5c20664b8eae22aa1b8 $
42 * @since 0.24
43 */
44 public final class StatusesTest {
45 /**
46 * Name of state property in Status JSON object.
47 */
48 private static final String STATE_PROP = "state";
49 /**
50 * Name of description property in Status JSON object.
51 */
52 private static final String DESCRIPTION_PROP = "description";
53 /**
54 * Name of description property in Status JSON object.
55 */
56 private static final String TARGET_PROP = "target_url";
57 /**
58 * Name of context property in Status JSON object.
59 */
60 private static final String CONTEXT_PROP = "context";
61 /**
62 * Test status URL.
63 */
64 private static final String URL = "http://status.jcabi-github.invalid/42";
65 /**
66 * Test commit status context string.
67 */
68 private static final String CONTEXT = "jcabi/github/test";
69
70 /**
71 * StatusCreate can convert itself to JSON.
72 */
73 @Test
74 public void convertsToJsonWhenAllPresent() {
75 final String success = "Everything is not so awesome";
76 MatcherAssert.assertThat(
77 new Statuses.StatusCreate(Status.State.ERROR)
78 .withTargetUrl(Optional.of(StatusesTest.URL))
79 .withDescription(success)
80 .withContext(Optional.of(StatusesTest.CONTEXT))
81 .json().toString(),
82 Matchers.equalTo(
83 Json.createObjectBuilder()
84 .add(StatusesTest.STATE_PROP, "error")
85 .add(StatusesTest.DESCRIPTION_PROP, success)
86 .add(StatusesTest.CONTEXT_PROP, StatusesTest.CONTEXT)
87 .add(StatusesTest.TARGET_PROP, StatusesTest.URL)
88 .build().toString()
89 )
90 );
91 }
92
93 /**
94 * StatusCreate can convert itself to JSON when it has no URL.
95 */
96 @Test
97 public void convertsToJsonWhenUrlAbsent() {
98 final String success = "Living the dream!";
99 MatcherAssert.assertThat(
100 new Statuses.StatusCreate(Status.State.SUCCESS)
101 .withDescription(success)
102 .withContext(Optional.of(StatusesTest.CONTEXT))
103 .json().toString(),
104 Matchers.equalTo(
105 Json.createObjectBuilder()
106 .add(StatusesTest.STATE_PROP, "success")
107 .add(StatusesTest.DESCRIPTION_PROP, success)
108 .add(StatusesTest.CONTEXT_PROP, StatusesTest.CONTEXT)
109 .build().toString()
110 )
111 );
112 }
113
114 /**
115 * StatusCreate can convert itself to JSON when it has no description.
116 */
117 @Test
118 public void convertsToJsonWhenDescriptionAbsent() {
119 MatcherAssert.assertThat(
120 new Statuses.StatusCreate(Status.State.FAILURE)
121 .withTargetUrl(Optional.of(StatusesTest.URL))
122 .withContext(Optional.of(StatusesTest.CONTEXT))
123 .json().toString(),
124 Matchers.equalTo(
125 Json.createObjectBuilder()
126 .add(StatusesTest.STATE_PROP, "failure")
127 .add(StatusesTest.DESCRIPTION_PROP, "")
128 .add(StatusesTest.CONTEXT_PROP, StatusesTest.CONTEXT)
129 .add(StatusesTest.TARGET_PROP, StatusesTest.URL)
130 .build().toString()
131 )
132 );
133 }
134
135 /**
136 * StatusCreate can convert itself to JSON when it has no context.
137 */
138 @Test
139 public void convertsToJsonWhenContextAbsent() {
140 final String pending = "Kragle is drying...";
141 MatcherAssert.assertThat(
142 new Statuses.StatusCreate(Status.State.PENDING)
143 .withTargetUrl(Optional.of(StatusesTest.URL))
144 .withDescription(pending)
145 .json().toString(),
146 Matchers.equalTo(
147 Json.createObjectBuilder()
148 .add(StatusesTest.STATE_PROP, "pending")
149 .add(StatusesTest.DESCRIPTION_PROP, pending)
150 .add(StatusesTest.TARGET_PROP, StatusesTest.URL)
151 .build().toString()
152 )
153 );
154 }
155 }