Skip to content

Commit

Permalink
(fix) compareTo
Browse files Browse the repository at this point in the history
compareTo previously yielded incorrect equal results causing lost paths.
  • Loading branch information
trajano committed Mar 7, 2018
1 parent 4401978 commit 4297efb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ public void apply(final Router router,
route.handler(jaxRsHandler).failureHandler(failureHandler);
}

/**
* Compare two arrays with the following rules. The longer the length, the
* higher it's sequence. If equal length them each entry is compared.
*/
private <T extends Comparable<T>> int compareArrays(final T[] a1,
final T[] a2) {

if (a1.length != a2.length) {
return a1.length - a2.length;
}

for (int i = 0; i < a1.length; ++i) {
if (!a1[i].equals(a2[i])) {
return a1[i].compareTo(a2[i]);
}
}

return 0;

}

/**
* Compares two JaxRsPath objects such that it is ordered by most specific and
* lowest level first. It sorts it in reverse by path. A path with produces is
Expand All @@ -140,13 +161,18 @@ public int compareTo(final JaxRsPath o) {
return c;
}

if (isNoProduces() && !o.isNoProduces()) {
return 1;
} else if (!isNoProduces() && o.isNoProduces()) {
return -1;
} else {
return 0;
final int producesComparison = compareArrays(produces, o.produces);
if (producesComparison != 0) {
return producesComparison;
}

final int consumesComparison = compareArrays(consumes, o.consumes);
if (consumesComparison != 0) {
return consumesComparison;
}

return method.compareTo(o.method);

}

@Override
Expand Down Expand Up @@ -231,6 +257,11 @@ private boolean isGet() {
return method == HttpMethod.GET;
}

private boolean isNoConsumes() {

return consumes.length == 0;
}

private boolean isNoProduces() {

return produces.length == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public void register(final Class<? extends Application> applicationClass,
final String[] consumes = Optional.ofNullable(m.getAnnotation(Consumes.class)).map(Consumes::value).orElse(new String[0]);
final String[] produces = Optional.ofNullable(m.getAnnotation(Produces.class)).map(Produces::value).orElse(new String[0]);

paths.add(new JaxRsPath(UriBuilder.fromPath(rootPath).path(classPath).path(path).toTemplate(), consumes, produces, getHttpMethod(m)));
final JaxRsPath newPath = new JaxRsPath(UriBuilder.fromPath(rootPath).path(classPath).path(path).toTemplate(), consumes, produces, getHttpMethod(m));
if (paths.contains(newPath)) {
throw new IllegalStateException("Duplicate insert " + newPath + " existing ");
}
paths.add(newPath);

});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
package net.trajano.ms.engine.jaxrs.test;

import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import net.trajano.ms.engine.jaxrs.PathsProvider;
import net.trajano.ms.engine.jaxrs.sample.SampleResource;
import java.util.Arrays;

import javax.ws.rs.core.MediaType;

import org.junit.Test;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.Router;
import net.trajano.ms.engine.jaxrs.JaxRsPath;
import net.trajano.ms.engine.jaxrs.JaxRsRouter;
import net.trajano.ms.engine.jaxrs.PathsProvider;
import net.trajano.ms.engine.jaxrs.sample.SampleApp;

import java.util.Arrays;
import net.trajano.ms.engine.jaxrs.sample.SampleResource;

public class JaxRsRouterTest {

@Test
public void equalityTests() {

final JaxRsPath p1 = new JaxRsPath("/hello", new String[] {
MediaType.APPLICATION_JSON
}, new String[] {
MediaType.APPLICATION_JSON
}, HttpMethod.POST);

final JaxRsPath p2 = new JaxRsPath("/hello", new String[0], new String[] {
MediaType.TEXT_PLAIN
}, HttpMethod.GET);

final JaxRsPath p3 = new JaxRsPath("/hello", new String[] {
MediaType.APPLICATION_FORM_URLENCODED
}, new String[] {
MediaType.TEXT_PLAIN
}, HttpMethod.POST);

assertFalse(p1.equals(p2));
assertFalse(p1.compareTo(p2) == 0);

assertFalse(p1.equals(p3));
assertFalse(p1.compareTo(p3) == 0);

assertFalse(p2.equals(p3));
assertFalse(p2.compareTo(p3) == 0);
assertFalse(p3.compareTo(p1) == 0);

}

@SuppressWarnings("unchecked")
@Test
public void testRegister() throws Exception {
Expand Down

0 comments on commit 4297efb

Please sign in to comment.