Refactor deployer app so that it starts empty

Functions are namespaced under the "app name", e.g.
/sample/uppercase is the "uppercase" function in the "sample" app.

Also added a README to get started quickly.
This commit is contained in:
Dave Syer
2017-07-03 16:58:05 +01:00
parent 42a50861c0
commit 0419fbc2ac
5 changed files with 207 additions and 65 deletions

View File

@@ -15,13 +15,17 @@
*/
package org.springframework.cloud.function.deployer;
import java.net.URI;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.SocketUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,19 +34,33 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*
*/
@Ignore
// TODO: Salvage some stuff from this project
public class FunctionExtractingFunctionCatalogIntegrationTests {
private static ConfigurableApplicationContext context;
private static int port;
@BeforeClass
public static void open() {
public static void open() throws Exception {
port = SocketUtils.findAvailableTcpPort();
// System.setProperty("debug", "true");
context = new ApplicationRunner().start("--server.port=" + port,
"--spring.cloud.stream.enabled=false");
deploy("sample", "maven://com.example:function-sample:1.0.0.BUILD-SNAPSHOT");
}
private static void deploy(String name, String path) throws Exception {
ResponseEntity<String> result = new TestRestTemplate().postForEntity(
"http://localhost:" + port + "/admin/" + name + "?path=" + path, "",
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private static String undeploy(String name) throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.delete(new URI("http://localhost:" + port + "/admin/" + name)).build(),
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
return result.getBody();
}
@AfterClass
@@ -52,18 +70,49 @@ public class FunctionExtractingFunctionCatalogIntegrationTests {
}
}
@Test
public void listing() {
assertThat(new TestRestTemplate()
.getForObject("http://localhost:" + port + "/admin", String.class))
.startsWith("{").contains("sample");
}
@Test
public void words() {
assertThat(new TestRestTemplate()
.getForObject("http://localhost:" + port + "/words", String.class))
.isEqualTo("{\"value\":\"foo\"}{\"value\":\"bar\"}");
.getForObject("http://localhost:" + port + "/sample/words", String.class))
.isEqualTo("[{\"value\":\"foo\"},{\"value\":\"bar\"}]");
}
@Test
public void uppercase() {
assertThat(new TestRestTemplate().postForObject(
"http://localhost:" + port + "/uppercase", "{\"value\":\"foo\"}",
String.class)).isEqualTo("{\"value\":\"FOO\"}");
"http://localhost:" + port + "/sample/uppercase", "{\"value\":\"foo\"}",
String.class)).isEqualTo("[{\"value\":\"FOO\"}]");
}
@Test
public void another() throws Exception {
deploy("strings", "maven://com.example:function-sample:1.0.0.BUILD-SNAPSHOT");
assertThat(new TestRestTemplate().getForObject(
"http://localhost:" + port + "/strings/words", String.class))
.isEqualTo("[\"foo\",\"bar\"]");
}
@Test
public void cycle() throws Exception {
String undeploy = undeploy("sample");
assertThat(undeploy.contains("\"name\":\"sample\""));
assertThat(undeploy.contains(
"\"path\":\"maven://com.example:function-sample-pojo:1.0.0.BUILD-SNAPSHOT\""));
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.get(new URI("http://localhost:" + port + "/sample/words")).build(),
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
deploy("sample", "maven://com.example:function-sample-pojo:1.0.0.BUILD-SNAPSHOT");
assertThat(new TestRestTemplate().postForObject(
"http://localhost:" + port + "/sample/uppercase", "{\"value\":\"foo\"}",
String.class)).isEqualTo("[{\"value\":\"FOO\"}]");
}
}

View File

@@ -15,21 +15,13 @@
*/
package org.springframework.cloud.function.deployer;
import java.util.Arrays;
import java.util.Collections;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.loader.thin.ArchiveUtils;
import org.springframework.boot.loader.tools.LogbackInitializer;
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
@@ -55,8 +47,10 @@ public class FunctionExtractingFunctionCatalogTests {
@Before
public void init() throws Exception {
if (id == null) {
id = deploy("maven://com.example:function-sample:1.0.0.BUILD-SNAPSHOT");
deploy("sample", "maven://com.example:function-sample:1.0.0.BUILD-SNAPSHOT");
// "--debug");
id = deploy("pojos",
"maven://com.example:function-sample-pojo:1.0.0.BUILD-SNAPSHOT");
}
}
@@ -64,38 +58,63 @@ public class FunctionExtractingFunctionCatalogTests {
public static void close() {
if (id != null) {
deployer.undeploy("sample");
deployer.undeploy("pojos");
}
}
@Test
public void listFunctions() throws Exception {
assertThat(deployer.getFunctionNames()).contains("sample/uppercase",
"pojos/uppercase");
}
@Test
public void nameFunction() throws Exception {
assertThat(deployer.getName(deployer.lookupFunction("sample/uppercase")))
.isEqualTo("sample/uppercase");
}
@Test
public void deployAndExtractFunctions() throws Exception {
// This one can only work if you change the boot classpath to contain reactor-core
// and reactive-streams
expected.expect(ClassCastException.class);
@SuppressWarnings("unchecked")
Flux<String> result = (Flux<String>) deployer.lookupFunction("uppercase")
Flux<String> result = (Flux<String>) deployer.lookupFunction("pojos/uppercase")
.apply(Flux.just("foo"));
assertThat(result.blockFirst()).isEqualTo("FOO");
}
@Test
public void listConsumers() throws Exception {
assertThat(deployer.getConsumerNames()).isEmpty();
}
@Test
public void deployAndExtractConsumers() throws Exception {
assertThat(deployer.lookupConsumer("sink")).isNull();
assertThat(deployer.lookupConsumer("pojos/sink")).isNull();
}
@Test
public void listSuppliers() throws Exception {
assertThat(deployer.getSupplierNames()).contains("sample/words", "pojos/words");
}
@Test
public void nameSupplier() throws Exception {
assertThat(deployer.getName(deployer.lookupSupplier("sample/words")))
.isEqualTo("sample/words");
}
@Test
public void deployAndExtractSuppliers() throws Exception {
assertThat(deployer.lookupSupplier("words")).isNotNull();
assertThat(deployer.lookupSupplier("sample/words")).isNotNull();
assertThat(deployer.lookupSupplier("pojos/words")).isNotNull();
}
private static String deploy(String jarName, String... args) throws Exception {
Resource resource = new FileSystemResource(
ArchiveUtils.getArchiveRoot(ArchiveUtils.getArchive(jarName)));
AppDefinition definition = new AppDefinition(resource.getFilename(),
Collections.emptyMap());
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource,
Collections.emptyMap(), Arrays.asList(args));
String deployed = deployer.deploy("sample", request);
private static String deploy(String name, String path, String... args)
throws Exception {
String deployed = deployer.deploy(name, path, args);
return deployed;
}