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 java.nio.charset.StandardCharsets; 33 import javax.json.Json; 34 import javax.json.JsonArray; 35 import javax.json.JsonObject; 36 import org.apache.commons.io.IOUtils; 37 import org.hamcrest.MatcherAssert; 38 import org.hamcrest.Matchers; 39 import org.junit.Test; 40 41 /** 42 * Test case for {@link SmartJsonTest}. 43 * @author Yegor Bugayenko (yegor256@gmail.com) 44 * @version $Id: 95a32e26b894fb6ba1a19e114ff1b8c73567131a $ 45 * @checkstyle MultipleStringLiterals (500 lines) 46 */ 47 public final class SmartJsonTest { 48 49 /** 50 * SmartJson can fetch data from JSON. 51 * @throws Exception If some problem inside 52 */ 53 @Test 54 public void fetchesStringFromJson() throws Exception { 55 MatcherAssert.assertThat( 56 new SmartJson( 57 SmartJsonTest.json("{\"first\": \"a\"}") 58 ).text("first"), 59 Matchers.equalTo("a") 60 ); 61 } 62 63 /** 64 * SmartJson can fetch number from JSON. 65 * @throws Exception If some problem inside 66 */ 67 @Test 68 public void fetchesNumberFromJson() throws Exception { 69 MatcherAssert.assertThat( 70 new SmartJson( 71 SmartJsonTest.json("{\"second\": 1}") 72 ).number("second"), 73 Matchers.equalTo(1) 74 ); 75 } 76 77 /** 78 * SmartJson can fetch an array from JSON. 79 * @throws Exception If some problem inside 80 */ 81 @Test 82 public void fetchesArrayFromJson() throws Exception { 83 MatcherAssert.assertThat( 84 new SmartJson( 85 SmartJsonTest.json("{\"arr\": [1, 2]}") 86 ).value("arr", JsonArray.class), 87 Matchers.hasSize(2) 88 ); 89 } 90 91 /** 92 * SmartJson can fetch an object from JSON. 93 * @throws Exception If some problem inside 94 */ 95 @Test 96 public void fetchesObjectFromJson() throws Exception { 97 MatcherAssert.assertThat( 98 new SmartJson( 99 SmartJsonTest.json("{\"o\": {\"foo\": [1]}}") 100 ).value("o", JsonObject.class).getJsonArray("foo"), 101 Matchers.hasSize(1) 102 ); 103 } 104 105 /** 106 * SmartJson can check for not null keys. 107 * @throws Exception If some problem inside 108 */ 109 @Test 110 public void checksNotNullKeyNotPresent() throws Exception { 111 MatcherAssert.assertThat( 112 new SmartJson( 113 SmartJsonTest.json("{\"first\": \"a\"}") 114 ).hasNotNull("second"), 115 Matchers.equalTo(false) 116 ); 117 } 118 119 /** 120 * SmartJson can check for not null keys. 121 * @throws Exception If some problem inside 122 */ 123 @Test 124 public void checksNotNullKeyPresentAndNull() throws Exception { 125 MatcherAssert.assertThat( 126 new SmartJson( 127 SmartJsonTest.json("{\"first\": null}") 128 ).hasNotNull("first"), 129 Matchers.equalTo(false) 130 ); 131 } 132 133 /** 134 * SmartJson can check for not null keys. 135 * @throws Exception If some problem inside 136 */ 137 @Test 138 public void checksNotNullKeyPresentAndNotNull() throws Exception { 139 MatcherAssert.assertThat( 140 new SmartJson( 141 SmartJsonTest.json("{\"first\": \"a\"}") 142 ).hasNotNull("first"), 143 Matchers.equalTo(true) 144 ); 145 } 146 147 /** 148 * Make a readable with this JSON content. 149 * @param txt JSON content 150 * @return Readable 151 */ 152 private static JsonReadable json(final String txt) { 153 return () -> Json.createReader( 154 IOUtils.toInputStream(txt, StandardCharsets.UTF_8) 155 ).readObject(); 156 } 157 158 }