Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.jsonschema2pojo.ContentResolverTest require internet connection (… #705

Merged
merged 2 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,10 @@ public interface Annotator {
*
* @param field
* the field that contains data that will be serialized
* @param clazz
* the owner of the field (class to which the field belongs)
* @param propertyName
* the name of the JSON property that this field represents
* @param propertyNode
* the schema node defining this property
*/
void dateField(JFieldVar field, JsonNode node);
void dateField(JFieldVar field, JsonNode propertyNode);

void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class MediaRule implements Rule<JType, JType> {
* Constructs a new media rule.
* </p>
*
* @param ruleFactory the rule factory that created this rule.
* @since 0.4.2
*/
protected MediaRule() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,6 +24,10 @@
import java.io.OutputStream;
import java.net.URI;

import org.apache.commons.io.IOUtils;
import org.jsonschema2pojo.util.LocalHttpServerBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -39,18 +43,35 @@ public void wrongProtocolCausesIllegalArgumentException() {
URI uriWithUnrecognisedProtocol = URI.create("foobar://schema/address.json");
resolver.resolve(uriWithUnrecognisedProtocol);
}


private static LocalHttpServerBuilder.Server server = null;

@BeforeClass
public static void beforeClass() throws Exception{
server = LocalHttpServerBuilder.createServer(
LocalHttpServerBuilder.context("/address", "application/json", "utf-8",
IOUtils.toByteArray(ContentResolverTest.class.getResourceAsStream("/schema/address.json")))
);
server.startInRange(1024, 100);
}

@AfterClass
public static void afterClass() throws Exception{
if(server!=null)
server.close();
}

@Test(expected=IllegalArgumentException.class)
public void brokenLinkCausesIllegalArgumentException() {

URI brokenHttpUri = URI.create("http://json-schema.org/address123123213");
URI brokenHttpUri = URI.create("http://localhost:" + server.getPort() + "/sserdda");
resolver.resolve(brokenHttpUri);
}

@Test
public void httpLinkIsResolvedToContent() {

URI httpUri = URI.create("http://json-schema.org/address");
URI httpUri = URI.create("http://localhost:" + server.getPort() + "/address");
JsonNode uriContent = resolver.resolve(httpUri);

assertThat(uriContent.path("description").asText().length(), is(greaterThan(0)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright © 2010-2014 Nokia
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.util;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.commons.io.IOUtils;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;


/**
* @author {@link "https://github.com/s13o" "s13o"}
* @since 3/16/2017
*/
public class LocalHttpServerBuilder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it doesn't seem like one endpoint is worth bringing in a library, but the project will have to lug this implementation around. We should use some kind of library for this. This entire PR should be somewhere in the 20 line range.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree, particularly as this would be a test dependency only. Let's use wiremock 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np


public interface Server extends Closeable {

/**
* Start the Server on first available port from pointed range
* @param from the first port to test for availability
* @param count max number to increment the port value
* @return port of the server
*/
int startInRange(int from, int count);

/**
*
* @return actual port or '-1' if the server has not been started
*/
int getPort();
}

private static class Srv implements Server {
private final List<Context> contexts = new ArrayList<Context>();
private HttpServer server;

private Srv(Context... context) {
if (context == null || context.length == 0)
throw new IllegalArgumentException("No context provided");
Collections.addAll(contexts, context);
}

@Override
public int getPort() {
if (server != null)
return server.getAddress().getPort();
return -1;
}

@Override
public void close() throws IOException {
if (server != null)
server.stop(0);
}

private int getFreePort(int from, int count) {
for (int port = from; port < from + count; port++) {
try {
new ServerSocket(port & 0xFFFF).close();
return port;
} catch (IOException e) {
}
}
throw new IllegalArgumentException(String.format("No free ports from %s to %s", from, from + count));
}

@Override
public int startInRange(int from, int count) {
final int port = getFreePort(from, count);
try {
server = HttpServer.create(new InetSocketAddress(port), 0);
for (Context c : contexts) {
server.createContext(c.context, new ContextHandler(c));
}
server.setExecutor(Executors.newCachedThreadPool());
server.start();
return port;
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
}

public static class Context {
private final String context;
private final String contentType;
private final String encoding;
private final byte[] content;

private Context(String context, String contentType, String encoding, byte[] content) {
this.context = context;
this.contentType = contentType;
this.encoding = encoding;
this.content = content;
}
}

private static class ContextHandler implements HttpHandler {
private final Context context;

private ContextHandler(Context context) {
this.context = context;
}

@Override
public void handle(HttpExchange exchange) throws IOException {
try {
final String in = IOUtils.toString(exchange.getRequestBody(), context.encoding);
exchange.getResponseHeaders().set("Content-Type", context.contentType);
exchange.sendResponseHeaders(200, context.content.length);
exchange.getResponseBody().write(context.content);
} catch (Exception e) {
exchange.getResponseHeaders().set("Content-Type", "plain/text");
final byte[] out = e.toString().getBytes(context.encoding);
exchange.sendResponseHeaders(500, out.length);
exchange.getResponseBody().write(out);
} finally {
exchange.close();
}
}
}

public static Context context(String context, String contentType, String encoding, byte[] content) {
return new Context(context, contentType, encoding, content);
}

public static Server createServer(Context... context) {
return new Srv(context);
}

}