Search in sources :

Example 6 with Resource

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

the class FileBackedRegistryTest method announcesNoMeaningfulChangesWhenNoSemanticChanges.

@Test
public void announcesNoMeaningfulChangesWhenNoSemanticChanges() throws Exception {
    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()));
    when(configurationFile.inputStream()).thenReturn(new ByteArrayInputStream(newContent));
    ReloadResult result = registry.reload().get();
    assertThat(result, is(unchanged("timestamp=NA, md5-hash=24996b9d53b21a60c35dcb7ca3fb331a, No semantic changes.")));
    // Still only one invocation, because reload didn't introduce any changes to configuration
    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) ReloadResult(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult) Test(org.junit.jupiter.api.Test)

Example 7 with Resource

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

the class FileBackedRegistryTest method mockResource.

private Resource mockResource(String path, ByteArrayInputStream content1, ByteArrayInputStream... contents) throws IOException {
    Resource configuration = mock(Resource.class);
    when(configuration.absolutePath()).thenReturn(path);
    when(configuration.inputStream()).thenReturn(content1, contents);
    return configuration;
}
Also used : Resource(com.hotels.styx.api.Resource)

Example 8 with Resource

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

the class FileBackedRegistryTest method announcesNoMeaningfulChangesWhenFileDidNotChange.

@Test
public void announcesNoMeaningfulChangesWhenFileDidNotChange() throws Exception {
    Resource configurationFile = mockResource("/styx/config", new ByteArrayInputStream(originalContent), 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()));
    ReloadResult result = registry.reload().get();
    assertThat(result, is(unchanged("timestamp=NA, md5-hash=c346e70114eff08dceb13562f9abaa48, Identical file content.")));
    // Still only one invocation, because reload didn't introduce any changes to configuration
    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) ReloadResult(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult) Test(org.junit.jupiter.api.Test)

Example 9 with Resource

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

the class FileBackedRegistryTest method announcesChanges.

@Test
public void announcesChanges() throws Exception {
    BackendService backendService1 = new BackendService.Builder().id("x").path("/x").build();
    BackendService backendService2 = new BackendService.Builder().id("x").path("/y").build();
    Resource configurationFile = mockResource("/styx/config", new ByteArrayInputStream(originalContent), new ByteArrayInputStream(newContent));
    registry = new FileBackedRegistry<>(configurationFile, bytes -> {
        if (new String(bytes).equals(new String(originalContent))) {
            return List.of(backendService1);
        } else {
            return List.of(backendService2);
        }
    }, any -> true);
    registry.addListener(listener);
    verify(listener).onChange(eq(changeSet().build()));
    await(registry.reload());
    verify(listener).onChange(eq(changeSet().added(backendService1).build()));
    ReloadResult result = registry.reload().get();
    assertThat(result, is(reloaded("timestamp=NA, md5-hash=24996b9d53b21a60c35dcb7ca3fb331a, File reloaded.")));
    assertThat(backendService1.equals(backendService2), is(false));
    verify(listener).onChange(eq(changeSet().updated(backendService2).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) BackendService(com.hotels.styx.api.extension.service.BackendService) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(com.hotels.styx.api.Resource) ReloadResult(com.hotels.styx.api.extension.service.spi.Registry.ReloadResult) Test(org.junit.jupiter.api.Test)

Example 10 with Resource

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

the class FileBackedRegistryTest method completesWithExceptionWhenErrorsDuringReload.

@Test
public void completesWithExceptionWhenErrorsDuringReload() throws Exception {
    Resource configurationFile = mockResource("/styx/config", new ByteArrayInputStream(originalContent), new ByteArrayInputStream(newContent));
    registry = new FileBackedRegistry<>(configurationFile, bytes -> {
        if (new String(bytes).equals(new String(originalContent))) {
            return List.of(backendService);
        } else {
            throw new JustATestException();
        }
    }, any -> true);
    registry.addListener(listener);
    ReloadResult outcome = await(registry.reload());
    assertThat(outcome, is(reloaded("timestamp=NA, md5-hash=c346e70114eff08dceb13562f9abaa48, File reloaded.")));
    verify(listener).onChange(eq(changeSet().added(backendService).build()));
    outcome = await(registry.reload());
    assertThat(outcome.outcome(), is(Registry.Outcome.FAILED));
    assertThat(outcome.message(), is("timestamp=NA, md5-hash=24996b9d53b21a60c35dcb7ca3fb331a, Reload failure."));
    assertThat(outcome.cause().get(), instanceOf(RuntimeException.class));
    // Noting changed
    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) JustATestException(com.hotels.styx.support.JustATestException) ByteArrayInputStream(java.io.ByteArrayInputStream) 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