diff --git a/src/main/java/com/openshift/client/HttpMethod.java b/src/main/java/com/openshift/client/HttpMethod.java index c635a7c2..89c65419 100644 --- a/src/main/java/com/openshift/client/HttpMethod.java +++ b/src/main/java/com/openshift/client/HttpMethod.java @@ -14,5 +14,22 @@ * @author Andre Dietisheim */ public enum HttpMethod { - GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS + GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS; + + /** + * hasValue determines if enum can safely convert string to enum value + * + * @param value the value to inspect + * @return true if the value can be safely converted to the enum; false otherwise + */ + public static boolean hasValue(String value) { + HttpMethod[] enumConstants = HttpMethod.class.getEnumConstants(); + for (int i = 0; i < enumConstants.length; i++) { + HttpMethod httpMethod = enumConstants[i]; + if(httpMethod.name().equalsIgnoreCase(value)){ + return true; + } + } + return false; + } } diff --git a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java index 46498f3d..a5e75567 100755 --- a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java +++ b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java @@ -69,6 +69,7 @@ import org.slf4j.LoggerFactory; import com.openshift.client.ApplicationScale; +import com.openshift.client.HttpMethod; import com.openshift.client.IGearProfile; import com.openshift.client.Messages; import com.openshift.client.OpenShiftException; @@ -227,7 +228,10 @@ private Map createLinks(final ModelNode linksNode) throws OpenShif final String linkName = linkNode.asProperty().getName(); final ModelNode valueNode = linkNode.asProperty().getValue(); if (valueNode.isDefined()) { - links.put(linkName, createLink(valueNode)); + Link link = createLink(valueNode); + if(link != null){ + links.put(linkName, link); + } } } } @@ -235,9 +239,12 @@ private Map createLinks(final ModelNode linksNode) throws OpenShif } private Link createLink(final ModelNode valueNode) { + final String method = valueNode.get(PROPERTY_METHOD).asString(); + if(!HttpMethod.hasValue(method)){ + return null; + } final String rel = getAsString(valueNode, PROPERTY_REL); final String href = valueNode.get(PROPERTY_HREF).asString(); - final String method = valueNode.get(PROPERTY_METHOD).asString(); final List requiredParams = createLinkParameters(valueNode.get(PROPERTY_REQUIRED_PARAMS)); final List optionalParams = diff --git a/src/test/java/com/openshift/client/HttpMethodTest.java b/src/test/java/com/openshift/client/HttpMethodTest.java new file mode 100644 index 00000000..55272f0c --- /dev/null +++ b/src/test/java/com/openshift/client/HttpMethodTest.java @@ -0,0 +1,19 @@ +package com.openshift.client; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class HttpMethodTest { + + @Test + public void hasValueForKnownValueShouldReturnTrue() { + assertTrue(HttpMethod.hasValue("POST")); + assertTrue(HttpMethod.hasValue("pOst")); + } + @Test + public void hasValueForUnKnownValueShouldReturnFalse() { + assertFalse(HttpMethod.hasValue("afdadsfads")); + } + +} diff --git a/src/test/java/com/openshift/client/utils/Samples.java b/src/test/java/com/openshift/client/utils/Samples.java index b08dde71..150c830e 100644 --- a/src/test/java/com/openshift/client/utils/Samples.java +++ b/src/test/java/com/openshift/client/utils/Samples.java @@ -78,7 +78,8 @@ public enum Samples { GET_CARTRIDGES("get-cartridges.json"), // 1.2 // links - LINKS_UNKNOWN_LINKPARAMETERTYPE("links-unknown-linkparametertype.json"); // 1.2 + LINKS_UNKNOWN_LINKPARAMETERTYPE("links-unknown-linkparametertype.json"), // 1.2 + LINKS_UNKNOWN_VERB("links-unknown-verb.json"); private static final String SAMPLES_FOLDER = "/samples/"; diff --git a/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java b/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java index 7f860b87..a6fbe835 100755 --- a/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java +++ b/src/test/java/com/openshift/internal/client/OpenShiftTestSuite.java @@ -13,6 +13,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; +import com.openshift.client.HttpMethodTest; import com.openshift.internal.client.httpclient.HttpClientTest; import com.openshift.internal.client.httpclient.request.FormUrlEncodedMediaTypeTest; import com.openshift.internal.client.httpclient.request.JsonMediaTypeTest; @@ -23,6 +24,7 @@ @Suite.SuiteClasses({ ConfigurationTest.class, HttpClientTest.class, + HttpMethodTest.class, RestServicePropertiesTest.class, RestServiceTest.class, OpenShiftJsonDTOFactoryTest.class, diff --git a/src/test/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactoryTest.java b/src/test/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactoryTest.java index ab95fb49..dc2acccf 100755 --- a/src/test/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactoryTest.java +++ b/src/test/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactoryTest.java @@ -428,7 +428,7 @@ public void shouldUnmarshallMultipleValidOptionInResponseBody() throws Throwable } @Test - public void shouldUnmarshallLinksUnknwonLinkParameterType() throws Throwable { + public void shouldUnmarshallLinksUnknownLinkParameterType() throws Throwable { // pre-conditions String content = Samples.LINKS_UNKNOWN_LINKPARAMETERTYPE.getContentAsString(); assertNotNull(content); @@ -449,5 +449,15 @@ public void shouldUnmarshallLinksUnknwonLinkParameterType() throws Throwable { assertThat(linkParameter.getDescription()).isEqualTo("post1Required1Description"); assertThat(linkParameter.getType()).isNotNull().isEqualTo(new LinkParameterType("unknown")); } + + @Test + public void shouldSkipLinksWithUnknownVerbsWithoutError() { + String content = Samples.LINKS_UNKNOWN_VERB.getContentAsString(); + assertNotNull(content); + + RestResponse response = factory.get(content); + final Map links = response.getData(); + assertThat(links.size()).isEqualTo(0); + } } diff --git a/src/test/resources/samples/links-unknown-verb.json b/src/test/resources/samples/links-unknown-verb.json new file mode 100644 index 00000000..87e55e43 --- /dev/null +++ b/src/test/resources/samples/links-unknown-verb.json @@ -0,0 +1,16 @@ +{ + "data":{ + "ALINK":{ + "href":"https://openshift.redhat.com/broker/rest/post1", + "method":"MadeUpMethodName", + "optional_params":[ + + ], + "rel":"Post1", + "required_params":[ + ] + } + }, + "type":"links", + "version":"1.2" +} \ No newline at end of file