Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/main/java/com/openshift/client/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,17 +228,23 @@ private Map<String, Link> 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);
}
}
}
}
return links;
}

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<LinkParameter> requiredParams =
createLinkParameters(valueNode.get(PROPERTY_REQUIRED_PARAMS));
final List<LinkParameter> optionalParams =
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/openshift/client/HttpMethodTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}

}
3 changes: 2 additions & 1 deletion src/test/java/com/openshift/client/utils/Samples.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@
@Suite.SuiteClasses({
ConfigurationTest.class,
HttpClientTest.class,
HttpMethodTest.class,
RestServicePropertiesTest.class,
RestServiceTest.class,
OpenShiftJsonDTOFactoryTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String, Link> links = response.getData();
assertThat(links.size()).isEqualTo(0);
}

}
16 changes: 16 additions & 0 deletions src/test/resources/samples/links-unknown-verb.json
Original file line number Diff line number Diff line change
@@ -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"
}