google-nomulus/javatests/google/registry/request/RouterTest.java
mcilwain 03c782f38e Replace ExceptionRule with ExpectedException
This is in preparation for running the automatic refactoring script that
will replace all ExpectedExceptions with use of JUnit 4.13's assertThrows/
expectThrows.

Note that I have recorded the callsites of assertions about EppExceptions
being marshallable and will edit those specific assertions back in after
running the automatic refactoring script (which do not understand these).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178812403
2017-12-13 12:43:45 -05:00

178 lines
6.2 KiB
Java

// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 google.registry.request;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.request.auth.Auth.AUTH_INTERNAL_ONLY;
import com.google.common.base.Function;
import java.util.Optional;
import java.util.concurrent.Callable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link Router}. */
@RunWith(JUnit4.class)
public final class RouterTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
////////////////////////////////////////////////////////////////////////////////////////////////
public interface Empty {}
@Test
public void testRoute_noRoutes_throws() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("No routes found for class: google.registry.request.RouterTest.Empty");
Router.create(Empty.class);
}
////////////////////////////////////////////////////////////////////////////////////////////////
@Action(path = "/sloth", auth = AUTH_INTERNAL_ONLY)
public static final class SlothTask implements Runnable {
@Override
public void run() {}
}
public interface SlothComponent {
SlothTask slothTask();
}
@Test
public void testRoute_pathMatch_returnsRoute() throws Exception {
Optional<Route> route = Router.create(SlothComponent.class).route("/sloth");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/sloth");
assertThat(route.get().instantiator()).isInstanceOf(Function.class);
}
@Test
public void testRoute_pathMismatch_returnsAbsent() throws Exception {
assertThat(Router.create(SlothComponent.class).route("/doge")).isEmpty();
}
@Test
public void testRoute_pathIsAPrefix_notAllowedByDefault() throws Exception {
assertThat(Router.create(SlothComponent.class).route("/sloth/extra")).isEmpty();
}
////////////////////////////////////////////////////////////////////////////////////////////////
@Action(path = "/prefix", isPrefix = true, auth = AUTH_INTERNAL_ONLY)
public static final class PrefixTask implements Runnable {
@Override
public void run() {}
}
public interface PrefixComponent {
PrefixTask prefixTask();
}
@Test
public void testRoute_prefixMatches_returnsRoute() throws Exception {
assertThat(Router.create(PrefixComponent.class).route("/prefix")).isPresent();
assertThat(Router.create(PrefixComponent.class).route("/prefix/extra")).isPresent();
}
@Test
public void testRoute_prefixDoesNotMatch_returnsAbsent() throws Exception {
assertThat(Router.create(PrefixComponent.class).route("")).isEmpty();
assertThat(Router.create(PrefixComponent.class).route("/")).isEmpty();
assertThat(Router.create(PrefixComponent.class).route("/ulysses")).isEmpty();
assertThat(Router.create(PrefixComponent.class).route("/man/of/sadness")).isEmpty();
}
////////////////////////////////////////////////////////////////////////////////////////////////
@Action(path = "/prefix/long", isPrefix = true, auth = AUTH_INTERNAL_ONLY)
public static final class LongTask implements Runnable {
@Override
public void run() {}
}
public interface LongPathComponent {
PrefixTask prefixTask();
LongTask longTask();
}
@Test
public void testRoute_prefixAndLongPathMatch_returnsLongerPath() throws Exception {
Optional<Route> route = Router.create(LongPathComponent.class).route("/prefix/long");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/prefix/long");
}
@Test
public void testRoute_prefixAndLongerPrefixMatch_returnsLongerPrefix() throws Exception {
Optional<Route> route = Router.create(LongPathComponent.class).route("/prefix/longer");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/prefix/long");
}
@Test
public void testRoute_onlyShortPrefixMatches_returnsShortPrefix() throws Exception {
Optional<Route> route = Router.create(LongPathComponent.class).route("/prefix/cat");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/prefix");
}
////////////////////////////////////////////////////////////////////////////////////////////////
public interface WeirdMethodsComponent {
SlothTask hasAnArgumentWhichIsIgnored(boolean lol);
Callable<?> notARunnableWhichIsIgnored();
}
@Test
public void testRoute_methodsInComponentAreIgnored_throws() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"No routes found for class: google.registry.request.RouterTest.WeirdMethodsComponent");
Router.create(WeirdMethodsComponent.class);
}
////////////////////////////////////////////////////////////////////////////////////////////////
@Action(path = "/samePathAsOtherTask", auth = AUTH_INTERNAL_ONLY)
public static final class DuplicateTask1 implements Runnable {
@Override
public void run() {}
}
@Action(path = "/samePathAsOtherTask", auth = AUTH_INTERNAL_ONLY)
public static final class DuplicateTask2 implements Runnable {
@Override
public void run() {}
}
public interface DuplicateComponent {
DuplicateTask1 duplicateTask1();
DuplicateTask2 duplicateTask2();
}
@Test
public void testCreate_twoTasksWithSameMethodAndPath_resultsInError() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Multiple entries with same key");
Router.create(DuplicateComponent.class);
}
}