This is an automated email from the ASF dual-hosted git repository.

smolnar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 56ab602  KNOX-2154 - Allow KNOX service during topology generation 
without URLs and parameters (#226)
56ab602 is described below

commit 56ab602d8f0e9ec92bfa083cc14badce8169468c
Author: Sandor Molnar <[email protected]>
AuthorDate: Fri Dec 20 09:09:56 2019 +0100

    KNOX-2154 - Allow KNOX service during topology generation without URLs and 
parameters (#226)
---
 .../topology/simple/SimpleDescriptorHandler.java   | 19 ++++----
 .../simple/SimpleDescriptorHandlerTest.java        | 52 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 8 deletions(-)

diff --git 
a/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java
 
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java
index 34f2f5b..70bf3a2 100644
--- 
a/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java
+++ 
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java
@@ -47,6 +47,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.TreeSet;
 
 /**
  * Processes simple topology descriptors, producing full topology files, which 
can subsequently be deployed to the
@@ -84,6 +85,8 @@ public class SimpleDescriptorHandler {
 
     private static Map<String, ServiceDiscovery> discoveryInstances = new 
HashMap<>();
 
+    private static final Set<String> ALLOWED_SERVICES_WITHOUT_URLS_AND_PARAMS 
= Collections.singleton("KNOX");
+
     public static Map<String, File> handle(GatewayConfig config, File desc) 
throws IOException {
         return handle(config, desc, NO_GATEWAY_SERVICES);
     }
@@ -107,7 +110,7 @@ public class SimpleDescriptorHandler {
     public static Map<String, File> handle(GatewayConfig config, 
SimpleDescriptor desc, File srcDirectory, File destDirectory, 
Service...gatewayServices) {
 
         List<String> declaredServiceNames = new ArrayList<>();
-        List<String> validServiceNames = new ArrayList<>();
+        Set<String> validServiceNames = new TreeSet<>();
         Map<String, String> serviceVersions = new HashMap<>();
         Map<String, Map<String, String>> serviceParams = new HashMap<>();
         Map<String, List<String>> serviceURLs = new HashMap<>();
@@ -169,11 +172,13 @@ public class SimpleDescriptorHandler {
                 // Don't add the service if the only params are discovery-only 
params
                 if (hasNonDiscoveryParams) {
                     serviceParams.put(serviceName, descService.getParams());
-                    if (!validServiceNames.contains(serviceName)) {
-                        validServiceNames.add(serviceName);
-                    }
+                    validServiceNames.add(serviceName);
                 }
             }
+
+            if 
(ALLOWED_SERVICES_WITHOUT_URLS_AND_PARAMS.contains(serviceName)) {
+              validServiceNames.add(serviceName);
+            }
         }
 
         // Provision the query param encryption password here, rather than 
relying on the random password generated
@@ -359,7 +364,7 @@ public class SimpleDescriptorHandler {
                                                       final File destDirectory,
                                                       final 
ServiceDiscovery.Cluster cluster,
                                                       final List<String> 
declaredServiceNames,
-                                                      final List<String> 
validServiceNames,
+                                                      final Set<String> 
validServiceNames,
                                                       final Map<String, 
String> serviceVersions,
                                                       final Map<String, 
List<String>> serviceURLs,
                                                       final Map<String, 
Map<String, String>> serviceParams) {
@@ -441,7 +446,7 @@ public class SimpleDescriptorHandler {
 
             // Services
             // Sort the service names to write the services alphabetically
-            List<String> serviceNames = new ArrayList<>(validServiceNames);
+            Set<String> serviceNames = new TreeSet<>(validServiceNames);
 
             // Add any declared services, which were not validated, but which 
have ZK-based HA provider config
             for (String haServiceName : haServiceParams.keySet()) {
@@ -454,8 +459,6 @@ public class SimpleDescriptorHandler {
                 }
             }
 
-            Collections.sort(serviceNames);
-
             // Write the service declarations
             for (String serviceName : serviceNames) {
                 sw.write("\n");
diff --git 
a/gateway-server/src/test/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandlerTest.java
 
b/gateway-server/src/test/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandlerTest.java
index 62c6e61..f5aca6a 100644
--- 
a/gateway-server/src/test/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandlerTest.java
+++ 
b/gateway-server/src/test/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandlerTest.java
@@ -50,6 +50,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasXPath;
 import static org.hamcrest.Matchers.is;
@@ -755,6 +756,57 @@ public class SimpleDescriptorHandlerTest {
         }
     }
 
+    @Test
+    public void shouldAllowKnoxServiceWithoutUrlsAndParams() throws Exception {
+      final String clusterName = "testCluster";
+      File providerConfig =  File.createTempFile("testKnoxProvider", ".xml");
+      FileUtils.write(providerConfig, TEST_PROVIDER_CONFIG, 
StandardCharsets.UTF_8);
+
+      final File discoveryConfig = 
File.createTempFile("testKnoxDiscoveryConfig", ".properties");
+      final Properties discoveryProperties = new Properties();
+      discoveryProperties.setProperty(clusterName + ".name", clusterName);
+
+      try (OutputStream outputStream = 
Files.newOutputStream(discoveryConfig.toPath())){
+          discoveryProperties.store(outputStream, null);
+      }
+
+      File topologyFile = null;
+      try {
+        final File destDir = new 
File(System.getProperty("java.io.tmpdir")).getCanonicalFile();
+        final GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
+
+        final SimpleDescriptor testDescriptor = 
EasyMock.createNiceMock(SimpleDescriptor.class);
+        
EasyMock.expect(testDescriptor.getName()).andReturn("testKnoxDescriptor").anyTimes();
+        
EasyMock.expect(testDescriptor.getProviderConfig()).andReturn(providerConfig.getAbsolutePath()).anyTimes();
+        
EasyMock.expect(testDescriptor.getDiscoveryAddress()).andReturn(discoveryConfig.getAbsolutePath()).anyTimes();
+        
EasyMock.expect(testDescriptor.getDiscoveryType()).andReturn("PROPERTIES_FILE").anyTimes();
+        
EasyMock.expect(testDescriptor.getDiscoveryUser()).andReturn(null).anyTimes();
+        
EasyMock.expect(testDescriptor.getClusterName()).andReturn(clusterName).anyTimes();
+
+        final SimpleDescriptor.Service knoxService = 
EasyMock.createNiceMock(SimpleDescriptor.Service.class);
+        EasyMock.expect(knoxService.getName()).andReturn("KNOX").anyTimes();
+        EasyMock.expect(knoxService.getURLs()).andReturn(null).anyTimes();
+        EasyMock.expect(knoxService.getParams()).andReturn(null).anyTimes();
+        List<SimpleDescriptor.Service> serviceMocks = 
Collections.singletonList(knoxService);
+        
EasyMock.expect(testDescriptor.getServices()).andReturn(serviceMocks).anyTimes();
+        EasyMock.replay(gc, knoxService, testDescriptor);
+
+        final Map<String, File> handleResult = 
SimpleDescriptorHandler.handle(gc, testDescriptor, destDir, destDir);
+        topologyFile = 
handleResult.get(SimpleDescriptorHandler.RESULT_TOPOLOGY);
+        assertTrue(topologyFile.exists());
+        assertTrue(new 
TopologyValidator(topologyFile.getAbsolutePath()).validateTopology());
+
+        final Document topologyXml = XmlUtils.readXml(topologyFile);
+        assertThat(topologyXml, hasXPath("/topology/service/role", 
is(equalTo("KNOX"))));
+      } finally {
+        providerConfig.delete();
+        discoveryConfig.delete();
+        if (topologyFile != null) {
+          topologyFile.delete();
+        }
+      }
+    }
+
     private File writeProviderConfig(String path, String content) throws 
IOException {
         File f = new File(path);
         FileUtils.write(f, content, StandardCharsets.UTF_8);

Reply via email to