Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imasahiro committed May 1, 2018
1 parent b063815 commit ec755f4
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 25 deletions.
Expand Up @@ -49,10 +49,10 @@
*
* <p>The route file could be retrieved as an {@link EndpointGroup} using the following code:
* <pre>{@code
* CentralDogmaEndpointGroup<JsonNode> endpointGroup = CentralDogmaEndpointGroup.ofJsonFile(
* CentralDogmaEndpointGroup<JsonNode> endpointGroup = CentralDogmaEndpointGroup.of(
* centralDogma, "myProject", "myRepo",
* CentralDogmaCodec.DEFAULT_JSON_CODEC,
* Query.ofJsonPath("/route.json")
* Query.ofJsonPath("/route.json"),
* EndpointListDecoder.JSON
* )
* endpointGroup.endpoints();
* }</pre>
Expand Down Expand Up @@ -89,8 +89,7 @@ public static <T> CentralDogmaEndpointGroup<T> ofWatcher(Watcher<T> watcher,
public static <T> CentralDogmaEndpointGroup<T> of(CentralDogma centralDogma,
String projectName, String repositoryName,
Query<T> query,
EndpointListDecoder<T> endpointCodec
) {
EndpointListDecoder<T> endpointCodec) {
return ofWatcher(centralDogma.fileWatcher(projectName, repositoryName, query), endpointCodec);
}

Expand Down
Expand Up @@ -19,12 +19,13 @@

import java.util.List;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;

import com.linecorp.armeria.client.Endpoint;

final class TextEndpointListDecoder implements EndpointListDecoder<String> {
private static final Splitter NEWLINE_SPLITTER = Splitter.on(System.getProperty("line.separator", "\n"))
private static final Splitter NEWLINE_SPLITTER = Splitter.on(CharMatcher.anyOf("\n\r"))
.omitEmptyStrings()
.trimResults();

Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright 2018 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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 com.linecorp.centraldogma.client.armeria;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;

import com.linecorp.armeria.client.Endpoint;

public class JsonEndpointListDecoderTest {
private static final ObjectMapper objectMapper = new ObjectMapper();

static List<String> HOST_AND_PORT_LIST = ImmutableList.of(
"centraldogma-sample001.com",
"centraldogma-sample001.com:1234",
"1.2.3.4",
"1.2.3.4:5678"
);

static List<Endpoint> ENDPOINT_LIST = ImmutableList.of(
Endpoint.of("centraldogma-sample001.com"),
Endpoint.of("centraldogma-sample001.com", 1234),
Endpoint.of("1.2.3.4"),
Endpoint.of("1.2.3.4", 5678)
);

@Test
public void decode() throws Exception {
EndpointListDecoder<JsonNode> decoder = EndpointListDecoder.JSON;
List<Endpoint> decoded = decoder.decode(
objectMapper.readTree(objectMapper.writeValueAsString(HOST_AND_PORT_LIST)));

assertThat(decoded).hasSize(4);
assertThat(decoded).isEqualTo(ENDPOINT_LIST);
}
}
@@ -0,0 +1,23 @@
package com.linecorp.centraldogma.client.armeria;

import static com.linecorp.centraldogma.client.armeria.JsonEndpointListDecoderTest.ENDPOINT_LIST;
import static com.linecorp.centraldogma.client.armeria.JsonEndpointListDecoderTest.HOST_AND_PORT_LIST;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.Test;

import com.linecorp.armeria.client.Endpoint;

public class TextEndpointListDecoderTest {
@Test
public void decode() {
EndpointListDecoder<String> decoder = EndpointListDecoder.TEXT;
List<Endpoint> decoded = decoder.decode(HOST_AND_PORT_LIST.stream().collect(Collectors.joining("\n")));

assertThat(decoded).hasSize(4);
assertThat(decoded).isEqualTo(ENDPOINT_LIST);
}
}

This file was deleted.

@@ -0,0 +1,110 @@
/*
* Copyright 2018 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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 com.linecorp.centraldogma.it;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import org.junit.Rule;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.client.Watcher;
import com.linecorp.centraldogma.client.armeria.CentralDogmaEndpointGroup;
import com.linecorp.centraldogma.client.armeria.EndpointListDecoder;
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.Query;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.testing.CentralDogmaRule;

public class CentralDogmaEndpointGroupTest {
private static List<String> HOST_AND_PORT_LIST = ImmutableList.of(
"centraldogma-sample001.com:1234",
"1.2.3.4:5678"
);
private static String HOST_AND_PORT_LIST_JSON;

static {
try {
HOST_AND_PORT_LIST_JSON = new ObjectMapper().writeValueAsString(HOST_AND_PORT_LIST);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static List<Endpoint> ENDPOINT_LIST = ImmutableList.of(
Endpoint.of("centraldogma-sample001.com", 1234),
Endpoint.of("1.2.3.4", 5678)
);

@Rule
public final CentralDogmaRule dogma = new CentralDogmaRule() {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("directory").join();
client.createRepository("directory", "my-service").join();
client.push("directory", "my-service",
Revision.HEAD, "commit",
Change.ofJsonUpsert("/endpoint.json", HOST_AND_PORT_LIST_JSON))
.join();
client.push("directory", "my-service",
Revision.HEAD, "commit",
Change.ofTextUpsert("/endpoint.txt",
HOST_AND_PORT_LIST.stream().collect(Collectors.joining("\n"))))
.join();
}
};

@Test
public void json() {
CentralDogmaEndpointGroup<JsonNode> endpointGroup = CentralDogmaEndpointGroup.ofWatcher(
dogma.client().fileWatcher("directory", "my-service",
Query.ofJson("/endpoint.json")),
EndpointListDecoder.JSON);
assertThat(endpointGroup.endpoints()).isEqualTo(ENDPOINT_LIST);
}

@Test(timeout = 10000)
public void text() throws Exception {
Watcher<String> watcher = dogma.client().fileWatcher("directory", "my-service",
Query.ofText("/endpoint.txt"));
CountDownLatch latch = new CountDownLatch(2);
watcher.watch(unused -> latch.countDown());
CentralDogmaEndpointGroup<String> endpointGroup = CentralDogmaEndpointGroup.ofWatcher(
watcher, EndpointListDecoder.TEXT);
assertThat(endpointGroup.endpoints()).isEqualTo(ENDPOINT_LIST);
assertThat(latch.getCount()).isOne();

dogma.client().push("directory", "my-service",
Revision.HEAD, "commit",
Change.ofTextUpsert("/endpoint.txt",
"foo.bar:1234"))
.join();

latch.await();
assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("foo.bar", 1234)));
}
}

0 comments on commit ec755f4

Please sign in to comment.