From ed14474b9f5dbd813b499ce97221ddb4876faa71 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 30 Jul 2018 13:48:48 +0200 Subject: [PATCH] GH-191 Made 'name' required in FunctionRegistration --- .../context/FunctionRegistration.java | 22 +++++++++++++++++-- .../catalog/InMemoryFunctionCatalog.java | 1 + ...ntextFunctionCatalogAutoConfiguration.java | 2 ++ .../catalog/InMemoryFunctionCatalogTests.java | 4 ++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionRegistration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionRegistration.java index 78e9cff4f..0ec2add66 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionRegistration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,6 @@ import reactor.core.publisher.Mono; /** * @author Dave Syer * @author Oleg Zhurakousky - * */ public class FunctionRegistration { @@ -50,11 +49,30 @@ public class FunctionRegistration { private FunctionType type; + /** + * @deprecated as of v1.0.0 in favor of {@link #FunctionRegistration(Object, String, String...)} + */ + @Deprecated public FunctionRegistration(T target) { Assert.notNull(target, "'target' must not be null"); this.target = target; } + /** + * Creates instance of FunctionRegistration. + * + * @param target instance of {@link Supplier}, {@link Function} or {@link Consumer} + * @param name initial name for this registration. + * @param names additional set of names for this registration. Additional names + * can be provided {@link #name(String)} or {@link #names(String...)} operations. + */ + public FunctionRegistration(T target, String name, String... additionalNames) { + Assert.notNull(target, "'target' must not be null"); + this.target = target; + this.names(name); + this.names(additionalNames); + } + public Map getProperties() { return properties; } diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java index 069722957..0a44a1b65 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java @@ -65,6 +65,7 @@ public class InMemoryFunctionCatalog @Override public void register(FunctionRegistration registration) { + Assert.notEmpty(registration.getNames(), "'registration' must contain at least one name before it is registered in catalog."); Class type = Object.class; if (registration.getTarget() instanceof Function) { type = Function.class; diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java index 2a89fd9db..df0467bec 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java @@ -82,6 +82,7 @@ import org.springframework.core.io.Resource; import org.springframework.core.type.StandardMethodMetadata; import org.springframework.core.type.classreading.MethodMetadataReadingVisitor; import org.springframework.stereotype.Component; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -128,6 +129,7 @@ public class ContextFunctionCatalogAutoConfiguration { @Override public void register(FunctionRegistration registration) { + Assert.notEmpty(registration.getNames(), "'registration' must contain at least one name before it is registered in catalog."); processor.register(registration); } diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalogTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalogTests.java index 9ad3b45f0..282f20c07 100644 --- a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalogTests.java +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalogTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 the original author or authors. + * Copyright 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,7 @@ public class InMemoryFunctionCatalogTests { @Test public void testFunctionRegistration() { TestFunction function = new TestFunction(); - FunctionRegistration registration = new FunctionRegistration<>(function) + FunctionRegistration registration = new FunctionRegistration<>(function, "foo") .type(FunctionType.of(TestFunction.class).getType()); InMemoryFunctionCatalog catalog = new InMemoryFunctionCatalog(); catalog.register(registration);