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 6ec81a08c KNOX-2958 - Fixed API samples for certain services (#814)
6ec81a08c is described below

commit 6ec81a08c0173fad1f25ecc53bd2a8cf3eec6f21
Author: Sandor Molnar <[email protected]>
AuthorDate: Tue Oct 31 12:46:49 2023 +0100

    KNOX-2958 - Fixed API samples for certain services (#814)
    
    Additionally, a general improvement is implemented that adds the missing 
slash at the beginning of the path element if it was missing from the service 
definition sample.
---
 .../resources/services/cm-api/1.0.0/service.xml    |  4 +--
 .../resources/services/oozie/5.0.0/service.xml     |  4 +--
 .../resources/services/webhdfs/2.4.0/service.xml   |  6 ++--
 .../resources/services/yarn-rm/2.5.0/service.xml   |  4 +--
 .../gateway/service/metadata/ServiceModel.java     |  3 +-
 .../gateway/service/metadata/ServiceModelTest.java | 37 ++++++++++++++++++++++
 6 files changed, 48 insertions(+), 10 deletions(-)

diff --git 
a/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml
 
b/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml
index bf776307e..5ce63bd47 100644
--- 
a/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml
+++ 
b/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml
@@ -30,12 +30,12 @@
             <sample>
                 <description>Fetch all CM-managed clusters</description>
                 <method>GET</method>
-                <path>clusters</path>
+                <path>/v41/clusters</path>
             </sample>
             <sample>
                 <description>Fetches HDFS service details from cluster named 
'c1'</description>
                 <method>GET</method>
-                <path>clusters/c1/services/HDFS</path>
+                <path>/v41/clusters/c1/services/HDFS</path>
             </sample>
             <sample>
                 <description>You can checkout CM's API (v41) document 
here</description>
diff --git 
a/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml
 
b/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml
index e43d2ad6f..6b940c33a 100644
--- 
a/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml
+++ 
b/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml
@@ -25,12 +25,12 @@
             <sample>
                 <description>Fetch the supported Oozie protocol versions by 
the server</description>
                 <method>GET</method>
-                <path>oozie/versions</path>
+                <path>/oozie/versions</path>
             </sample>
              <sample>
                 <description>Fetch the system status</description>
                 <method>GET</method>
-                <path>oozie/v1/admin/status</path>
+                <path>/oozie/v1/admin/status</path>
             </sample>
             <sample>
                 <description>You may check out Apache Oozie's REST API 
documentation here</description>
diff --git 
a/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml
 
b/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml
index 48a7d416f..b1dde9b42 100644
--- 
a/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml
+++ 
b/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml
@@ -25,17 +25,17 @@
             <sample>
                 <description>List all files under 'testPath'</description>
                 <method>GET</method>
-                <path>v1/testPath?op=LISTSTATUS</path>
+                <path>/v1/testPath?op=LISTSTATUS</path>
             </sample>
             <sample>
                 <description>Rename a File/Directory under </description>
                 <method>PUT</method>
-                
<path>v1/testPath/testFile?op=RENAME&amp;destination=testPath/renamedFile</path>
+                
<path>/v1/testPath/testFile?op=RENAME&amp;destination=testPath/renamedFile</path>
             </sample>
             <sample>
                 <description>Get Home Directory</description>
                 <method>GET</method>
-                <path>v1/?op=GETHOMEDIRECTORY</path>
+                <path>/v1/?op=GETHOMEDIRECTORY</path>
             </sample>
             <sample>
                 <description>You may check out Apache WebHDFS's REST API 
documentation here</description>
diff --git 
a/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml
 
b/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml
index 76bbb185e..07436c1ed 100644
--- 
a/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml
+++ 
b/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml
@@ -25,12 +25,12 @@
             <sample>
                 <description>Fetch cluster info</description>
                 <method>GET</method>
-                <path>ws/v1/cluster/info</path>
+                <path>/ws/v1/cluster/info</path>
             </sample>
             <sample>
                 <description>Fetch cluster metrics</description>
                 <method>GET</method>
-                <path>ws/v1/cluster/metrics</path>
+                <path>/ws/v1/cluster/metrics</path>
             </sample>
             <sample>
                 <description>You may check out Apache YARN Resource Manager's 
REST API documentation here</description>
diff --git 
a/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java
 
b/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java
index 3d9c57adf..d4b08d088 100644
--- 
a/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java
+++ 
b/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java
@@ -178,7 +178,8 @@ public class ServiceModel implements 
Comparable<ServiceModel> {
           resolvedSample.setValue(sample.getValue());
         } else {
           final String method = StringUtils.isBlank(sample.getMethod()) ? 
"GET" : sample.getMethod();
-          resolvedSample.setValue(String.format(Locale.ROOT, 
CURL_SAMPLE_TEMPLATE, method, getServiceUrl(), sample.getPath()));
+          final String path = sample.getPath().startsWith("/") ? 
sample.getPath() : ("/" + sample.getPath());
+          resolvedSample.setValue(String.format(Locale.ROOT, 
CURL_SAMPLE_TEMPLATE, method, getServiceUrl(), path));
         }
         samples.add(resolvedSample);
       });
diff --git 
a/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java
 
b/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java
index 95ff5e6f3..4058ee2f6 100644
--- 
a/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java
+++ 
b/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java
@@ -21,12 +21,17 @@ import static 
org.apache.knox.gateway.service.metadata.ServiceModel.HIVE_SERVICE
 import static 
org.apache.knox.gateway.service.metadata.ServiceModel.HIVE_SERVICE_URL_TEMPLATE;
 import static 
org.apache.knox.gateway.service.metadata.ServiceModel.SERVICE_URL_TEMPLATE;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 
+import java.util.Arrays;
+import java.util.List;
 import java.util.Locale;
 
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.knox.gateway.service.definition.Metadata;
+import org.apache.knox.gateway.service.definition.Sample;
 import org.apache.knox.gateway.topology.Service;
 import org.apache.knox.gateway.topology.Version;
 import org.easymock.EasyMock;
@@ -231,4 +236,36 @@ public class ServiceModelTest {
     assertEquals(String.format(Locale.ROOT, SERVICE_URL_TEMPLATE, 
SERVER_SCHEME, SERVER_NAME, SERVER_PORT, gatewayPath, topologyName,
         context.replace("{{SCHEME}}", "https").replace("{{HOST}}", 
"localhost").replace("{{PORT}}", "5555")), serviceModel.getServiceUrl());
   }
+
+  @Test
+  public void shouldReturnProperSampleValueEvenIfPathDoesNotStartWithSlash() 
throws Exception {
+    final ServiceModel serviceModel = new ServiceModel();
+    final String gatewayPath = "gateway";
+    final String topologyName = "sandbox";
+    serviceModel.setGatewayPath(gatewayPath);
+    serviceModel.setTopologyName(topologyName);
+    serviceModel.setRequest(setUpHttpRequestMock());
+
+    final Metadata metadata = EasyMock.createNiceMock(Metadata.class);
+    final Sample sampleStartsWithSlash = new Sample();
+    sampleStartsWithSlash.setDescription("sampleStartsWithSlash");
+    sampleStartsWithSlash.setMethod("PUT");
+    sampleStartsWithSlash.setPath("/sampleStartsWithSlashPath/operation");
+    final Sample sampleStartsWithoutSlash = new Sample();
+    sampleStartsWithoutSlash.setDescription("sampleStartsWithoutSlash");
+    
sampleStartsWithoutSlash.setPath("sampleStartsWithoutSlashPath/operation"); // 
note the missing starting slash
+    
EasyMock.expect(metadata.getSamples()).andReturn(Arrays.asList(sampleStartsWithSlash,
 sampleStartsWithoutSlash)).anyTimes();
+    serviceModel.setServiceMetadata(metadata);
+
+    final String context = "/testContext";
+    EasyMock.expect(metadata.getContext()).andReturn(context).anyTimes();
+    EasyMock.replay(metadata);
+    final List<Sample> samples = serviceModel.getSamples();
+    assertNotNull(samples);
+    assertFalse(samples.isEmpty());
+    assertEquals(2, samples.size());
+
+    assertEquals(samples.get(0).getValue(), "curl -iv -X PUT 
\"https://localhost:8443/gateway/sandbox/testContext/sampleStartsWithSlashPath/operation\"";);
+    assertEquals(samples.get(1).getValue(), "curl -iv -X GET 
\"https://localhost:8443/gateway/sandbox/testContext/sampleStartsWithoutSlashPath/operation\"";);
+  }
 }

Reply via email to