Search in sources :

Example 1 with Resource

use of com.hotels.styx.api.Resource in project styx by ExpediaGroup.

the class FileBackedRegistryTest method announcesInitialStateWhenStarts.

@Test
public void announcesInitialStateWhenStarts() throws IOException {
    Resource configurationFile = mockResource("/styx/config", new ByteArrayInputStream(originalContent));
    registry = new FileBackedRegistry<>(configurationFile, bytes -> List.of(backendService), any -> true);
    registry.addListener(listener);
    await(registry.reload());
    verify(listener).onChange(eq(changeSet().added(backendService).build()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Registry(com.hotels.styx.api.extension.service.spi.Registry) ReloadResult(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StyxFutures.await(com.hotels.styx.common.StyxFutures.await) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ReloadResult.unchanged(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.unchanged) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(com.hotels.styx.api.Resource) JustATestException(com.hotels.styx.support.JustATestException) Matchers.eq(org.mockito.Matchers.eq) BackendService(com.hotels.styx.api.extension.service.BackendService) ReloadResult.reloaded(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.reloaded) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.mock(org.mockito.Mockito.mock) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(com.hotels.styx.api.Resource) Test(org.junit.jupiter.api.Test)

Example 2 with Resource

use of com.hotels.styx.api.Resource in project styx by ExpediaGroup.

the class FileBackedBackendServicesRegistryTest method yamlBackendReaderPropagatesExceptionWhenFailsToReadFromByteStream.

@Test
public void yamlBackendReaderPropagatesExceptionWhenFailsToReadFromByteStream() {
    Resource resource = newResource("classpath:/backends/origins-with-invalid-path.yml");
    assertThrows(RuntimeException.class, () -> new YAMLBackendServicesReader().read(bytes(resource.inputStream(), true)));
}
Also used : YAMLBackendServicesReader(com.hotels.styx.proxy.backends.file.FileBackedBackendServicesRegistry.YAMLBackendServicesReader) ResourceFactory.newResource(com.hotels.styx.common.io.ResourceFactory.newResource) Resource(com.hotels.styx.api.Resource) Test(org.junit.jupiter.api.Test)

Example 3 with Resource

use of com.hotels.styx.api.Resource in project styx by ExpediaGroup.

the class FileBackedBackendServicesRegistryTest method yamlBackendReaderReadsBackendServicesFromByteStream.

@Test
public void yamlBackendReaderReadsBackendServicesFromByteStream() throws IOException {
    Resource resource = newResource("classpath:/backends/origins.yml");
    Iterable<BackendService> backendServices = new YAMLBackendServicesReader().read(bytes(resource.inputStream(), true));
    assertThat(iterableToList(backendServices).size(), is(3));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) YAMLBackendServicesReader(com.hotels.styx.proxy.backends.file.FileBackedBackendServicesRegistry.YAMLBackendServicesReader) ResourceFactory.newResource(com.hotels.styx.common.io.ResourceFactory.newResource) Resource(com.hotels.styx.api.Resource) Test(org.junit.jupiter.api.Test)

Example 4 with Resource

use of com.hotels.styx.api.Resource in project styx by ExpediaGroup.

the class FileBackedBackendServicesRegistryTest method duplicatePathPrefixesCausesStartUpFailure.

@Test
public void duplicatePathPrefixesCausesStartUpFailure() throws InterruptedException, TimeoutException, IOException {
    String configWithDupe = "" + "---\n" + "- id: \"first\"\n" + "  path: \"/testpath/\"\n" + "  origins:\n" + "  - id: \"l1\"\n" + "    host: \"localhost:60000\"\n" + "- id: \"second\"\n" + "  path: \"/testpath/\"\n" + "  origins:\n" + "  - id: \"l2\"\n" + "    host: \"localhost:60001\"";
    Resource stubResource = mock(Resource.class);
    when(stubResource.inputStream()).thenReturn(toInputStream(configWithDupe));
    FileBackedBackendServicesRegistry registry = new FileBackedBackendServicesRegistry(stubResource, FileMonitor.DISABLED);
    CompletableFuture<Void> status = registry.start();
    assertThat(failureCause(status), is(instanceOf(ServiceFailureException.class)));
}
Also used : ResourceFactory.newResource(com.hotels.styx.common.io.ResourceFactory.newResource) Resource(com.hotels.styx.api.Resource) Test(org.junit.jupiter.api.Test)

Example 5 with Resource

use of com.hotels.styx.api.Resource in project styx by ExpediaGroup.

the class FileBackedBackendServicesRegistryTest method duplicatePathPrefixesCausesReloadFailure.

@Test
public void duplicatePathPrefixesCausesReloadFailure() throws ExecutionException, InterruptedException, TimeoutException, IOException {
    String configWithDupe = "" + "---\n" + "- id: \"first\"\n" + "  path: \"/testpath/\"\n" + "  origins:\n" + "  - id: \"l1\"\n" + "    host: \"localhost:60000\"\n" + "- id: \"second\"\n" + "  path: \"/testpath/\"\n" + "  origins:\n" + "  - id: \"l2\"\n" + "    host: \"localhost:60001\"";
    Resource stubResource = mock(Resource.class);
    when(stubResource.inputStream()).thenReturn(toInputStream(configWithDupe));
    FileBackedBackendServicesRegistry registry = new FileBackedBackendServicesRegistry(stubResource, FileMonitor.DISABLED);
    ReloadResult result = registry.reload().get(10, SECONDS);
    assertThat(result.outcome(), is(FAILED));
    assertThat(log.log(), hasItem(loggingEvent(ERROR, "Duplicate path '/testpath/' used for applications: 'first', 'second'")));
    assertThat(result.message(), is("timestamp=NA, md5-hash=3cf21842c8dee594b646ea903fc09490, Reload failure."));
}
Also used : ResourceFactory.newResource(com.hotels.styx.common.io.ResourceFactory.newResource) Resource(com.hotels.styx.api.Resource) ReloadResult(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.hotels.styx.api.Resource)13 Test (org.junit.jupiter.api.Test)12 BackendService (com.hotels.styx.api.extension.service.BackendService)6 ReloadResult (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult)6 Registry (com.hotels.styx.api.extension.service.spi.Registry)5 ReloadResult.reloaded (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.reloaded)5 ReloadResult.unchanged (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.unchanged)5 StyxFutures.await (com.hotels.styx.common.StyxFutures.await)5 JustATestException (com.hotels.styx.support.JustATestException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)5 List (java.util.List)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)5 Matchers.is (org.hamcrest.Matchers.is)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Matchers.eq (org.mockito.Matchers.eq)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.verify (org.mockito.Mockito.verify)5