reverting BAEL-5657

This commit is contained in:
Ulisses Lima 2022-08-08 13:40:15 -03:00
parent 46aceb10ed
commit d23e389a00
4 changed files with 0 additions and 188 deletions

View File

@ -1,53 +0,0 @@
package com.baeldung.jackson.jsonurlreader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUrlReader {
public static void main(String[] args) throws StreamReadException, DatabindException, MalformedURLException, IOException {
String url = args[0];
JsonNode node = JsonUrlReader.get(url);
System.out.println(node.toPrettyString());
}
public static String stream(String url) throws IOException {
try (InputStream input = new URL(url).openStream()) {
InputStreamReader isr = new InputStreamReader(input, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(isr);
StringBuilder json = new StringBuilder();
int c;
while ((c = reader.read()) != -1) {
json.append((char) c);
}
return json.toString();
}
}
public static JsonNode get(String url) throws StreamReadException, DatabindException, MalformedURLException, IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(new URL(url));
return json;
}
public static <T> T get(String url, Class<T> type) throws StreamReadException, DatabindException, MalformedURLException, IOException {
ObjectMapper mapper = new ObjectMapper();
T entity = mapper.readValue(new URL(url), type);
return entity;
}
public static String getString(String url) throws StreamReadException, DatabindException, MalformedURLException, IOException {
return get(url).toPrettyString();
}
}

View File

@ -1,40 +0,0 @@
package com.baeldung.jackson.jsonurlreader.data;
public class Example {
private String name;
private Integer n;
private Boolean real;
public Example() {
}
public Example(String name, Integer n, Boolean real) {
this.name = name;
this.n = n;
this.real = real;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getN() {
return n;
}
public void setN(Integer n) {
this.n = n;
}
public Boolean getReal() {
return real;
}
public void setReal(Boolean real) {
this.real = real;
}
}

View File

@ -1,62 +0,0 @@
package com.baeldung.jackson.jsonurlreader;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import java.io.IOException;
import java.net.ServerSocket;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpStatusCode;
import com.baeldung.jackson.jsonurlreader.data.Example;
public class JsonMockServer {
protected static final String JSON_RESPONSE = "{ \"name\": \"A\", \"n\": 1, \"real\": true }";
protected static final Example OBJECT_RESPONSE = new Example("A", 1, true);
protected static String serviceUrl;
private static final String SERVER_ADDRESS = "127.0.0.1";
private static final String PATH = "/sample-json";
private static ClientAndServer mockServer;
private static int serverPort;
@BeforeAll
static void startServer() throws IOException {
serverPort = getFreePort();
serviceUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH;
mockServer = startClientAndServer(serverPort);
mockJsonRequest();
}
@AfterAll
static void stopServer() {
mockServer.stop();
}
@SuppressWarnings("resource")
private static void mockJsonRequest() {
new MockServerClient(SERVER_ADDRESS, serverPort)
.when(request()
.withPath(PATH)
.withMethod("GET"))
.respond(response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(JSON_RESPONSE)
);
}
private static int getFreePort() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}
}

View File

@ -1,33 +0,0 @@
package com.baeldung.jackson.jsonurlreader;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import com.baeldung.jackson.jsonurlreader.data.Example;
import com.fasterxml.jackson.databind.JsonNode;
public class JsonUrlReaderIntegrationTest extends JsonMockServer {
@Test
public void whenStreamUrl_thenJsonStringReturned() throws Exception {
String jsonNode = JsonUrlReader.stream(serviceUrl);
assertEquals(jsonNode, JSON_RESPONSE);
}
@Test
public void whenGetUrl_thenJsonNodeReturned() throws Exception {
JsonNode jsonNode = JsonUrlReader.get(serviceUrl);
assertEquals(jsonNode.get("name")
.textValue(), "A");
}
@Test
public void givenType_whenGetUrl_thenTypeReturned() throws Exception {
Example object = JsonUrlReader.get(serviceUrl, Example.class);
assertEquals(object.getName(), "A");
}
}