Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OSJC-169] changed KubernetesResource#equals #hashCode to constant behav #18

Merged
merged 1 commit into from May 27, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,14 +31,14 @@
*
* @author Jeff Cantrill
*/
public class KubernetesResource implements IResource, ResourcePropertyKeys{
public abstract class KubernetesResource implements IResource, ResourcePropertyKeys {

private ModelNode node;
private IClient client;
private Map<Class<? extends ICapability>, ICapability> capabilities = new HashMap<Class<? extends ICapability>, ICapability>();
private Map<String, String []> propertyKeys;

public KubernetesResource(ModelNode node, IClient client, Map<String, String []> propertyKeys){
protected KubernetesResource(ModelNode node, IClient client, Map<String, String []> propertyKeys){
this.node = node;
this.client = client;
this.propertyKeys = propertyKeys;
Expand Down Expand Up @@ -105,11 +105,11 @@ public void refresh(){
this.node = ModelNode.fromJSONString(client.get(getKind(), getName(), getNamespace()).toString());
}

// TODO Pretty certain this should be protected
@Override
public ResourceKind getKind(){
if(node.has("kind")){
return ResourceKind.valueOf(node.get("kind").asString());
ModelNode kindNode = get(ResourcePropertyKeys.KIND);
if(kindNode.isDefined()){
return ResourceKind.valueOf(kindNode.asString());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will throw for unknown/new kinds that we are not aware of. We should either get away from enums or provide a lenient #valueOf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adietish this was one of my thoughts to KubernetesResource not being abstract for the cases of when we do have kinds we dont understand about and we decide to move away from enum

}
return null;
}
Expand All @@ -128,7 +128,6 @@ public String getName(){
return asString(NAME);
}

@Override
public void setName(String name) {
set(NAME, name);
}
Expand All @@ -142,14 +141,13 @@ public String getNamespace(){
return node.asString();
}

@Override
public void setNamespace(String namespace){
set(NAMESPACE, namespace);
}

@Override
public void addLabel(String key, String value) {
ModelNode labels = node.get("labels");
ModelNode labels = node.get(ResourcePropertyKeys.LABELS);
labels.get(key).set(value);
}

Expand All @@ -161,20 +159,25 @@ public Map<String, String> getLabels() {

/*---------- utility methods ------*/
protected ModelNode get(String key){
String [] property = propertyKeys.get(key);
return node.get(property);
return node.get(getPath(key));
}

protected void set(String key, int value) {
String [] property = propertyKeys.get(key);
node.get(property).set(value);
node.get(getPath(key)).set(value);
}

protected void set(String key, String value){
String [] property = propertyKeys.get(key);
node.get(property).set(value);
node.get(getPath(key)).set(value);
}

private String[] getPath(String key) {
String [] property = propertyKeys.get(key);
if (property == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adietish we should consider throwing here/providing an indication that the path doesnt exist. If the path doesnt exist for a given property then it needs to be added to the map IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcantrill isnt the property map a mapping so that if a special mapping is missing it indicates that the key is to be used as is? What is the purpose of these property-keys?

throw new IllegalArgumentException(String.format("key %s is not known to the resource %s", key, getName().isEmpty()? getClass().getSimpleName() : getName()));
}
return property;
}

protected Map<String, String> asMap(String property){
return JBossDmrExtentions.asMap(this.node, propertyKeys, property);
}
Expand Down Expand Up @@ -203,26 +206,52 @@ public String toPrettyString(){

@Override
public int hashCode() {
String namespace = getNamespace();
String name = getName();
ResourceKind kind = getKind();
final int prime = 31;
int result = 1;
result = prime * result + ((node == null) ? 0 : node.hashCode());
return result;
return prime * (namespace.hashCode() + name.hashCode() + kind.hashCode());
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
else if (obj == null)
return false;
KubernetesResource other = (KubernetesResource) obj;
if (node == null) {
if (other.node != null)
return false;
} else if (!node.equals(other.node))
else if (getClass() != obj.getClass())
return false;
else {
KubernetesResource other = (KubernetesResource) obj;
if (getKind() != null){
if (getKind() != other.getKind()) {
return false;
}
} else {
if (other.getKind() != null) {
return false;
}
}
if (getNamespace() != null) {
if(!getNamespace().equals(other.getNamespace())) {
return false;
}
} else {
if (other.getNamespace() != null) {
return false;
}
}
if (getName() != null) {
if(!getName().equals(other.getName())) {
return false;
}
} else {
if (other.getName() != null) {
return false;
}
}

}
return true;
}

Expand Down
Expand Up @@ -23,6 +23,7 @@ public interface KubernetesApiModelProperties extends ResourcePropertyKeys{
put(ANNOTATIONS, new String [] {"metadata", "annotations"});
put(APIVERSION, new String [] {"apiVersion"});
put(CREATION_TIMESTAMP, new String [] {"metadata","creationTimestamp"});
put(KIND, new String[] { "kind" });
put(LABELS, new String [] {"metadata","labels"});
put(NAME , new String [] {"metadata","name"});
put(NAMESPACE, new String [] {"metadata","namespace"});
Expand Down Expand Up @@ -57,6 +58,7 @@ public interface KubernetesApiModelProperties extends ResourcePropertyKeys{
put(ANNOTATIONS, new String [] {"annotations"});
put(APIVERSION, new String [] {"apiVersion"});
put(CREATION_TIMESTAMP, new String [] {"creationTimestamp"});
put(KIND, new String[] { "kind" });
put(LABELS, new String [] {"labels"});
put(NAME , new String [] {"id"});
put(NAMESPACE, new String [] {"namespace"});
Expand Down
Expand Up @@ -60,7 +60,9 @@ public interface OpenShiftApiModelProperties extends ResourcePropertyKeys{
put(DEPLOYMENTCONFIG_STRATEGY, new String[]{"template","strategy","type"});

put(IMAGESTREAM_DOCKER_IMAGE_REPO, new String[]{"status","dockerImageRepository"});


put(KIND, new String[] { "kind" });

put(PROJECT_DISPLAY_NAME, new String[]{"displayName"});

put(ROUTE_HOST, new String[] { "host" });
Expand Down Expand Up @@ -116,6 +118,8 @@ public interface OpenShiftApiModelProperties extends ResourcePropertyKeys{
put(DEPLOYMENTCONFIG_REPLICA_SELECTOR, new String[]{"spec","selector"});
put(DEPLOYMENTCONFIG_TRIGGERS, new String[]{"spec","triggers"});
put(DEPLOYMENTCONFIG_STRATEGY, new String[]{"spec","strategy","type"});

put(KIND, new String[]{"kind"});

put(IMAGESTREAM_DOCKER_IMAGE_REPO, new String[]{"status","dockerImageRepository"});

Expand Down
Expand Up @@ -20,6 +20,8 @@ public interface ResourcePropertyKeys extends BuildConfigPropertyKeys{
static final String LABELS = "labels";
static final String NAME = "name";
static final String NAMESPACE = "namespace";
static final String DISPLAYNAME = "displayName";
static final String KIND = "kind";

static final String REPLICATION_CONTROLLER_CONTAINERS = "replicationcontroller.containers";
static final String REPLICATION_CONTROLLER_REPLICA_COUNT = "replicationcontroller.replicacount";
Expand Down
Expand Up @@ -30,7 +30,7 @@ public static Map<String, String> asMap(ModelNode root, Map<String, String []> p
if(propertyKeys != null){
String [] path = propertyKeys.get(property);
ModelNode node = root.get(path);
if( ModelType.UNDEFINED == node.getType())
if( !node.isDefined())
return map;
for (String key : node.keys()) {
map.put(key, node.get(key).asString());
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/com/openshift/restclient/model/IResource.java
Expand Up @@ -20,7 +20,7 @@
*
* @author Jeff Cantrill
*/
public interface IResource extends ICapable{
public interface IResource extends ICapable {

/**
* Retrieves the list of capabilities supported by this resource
Expand Down Expand Up @@ -53,24 +53,12 @@ public interface IResource extends ICapable{
*/
String getName();

/**
* Sets the identifier for this resource
* @param name
*/
void setName(String name);

/**
* Returns the scope of this resource
* @return
*/
String getNamespace();

/**
* Sets the scope of this resource
* @param namespace
*/
void setNamespace(String namespace);

/**
* Retrieves the labels associated with the resource
* @return
Expand Down
Expand Up @@ -8,7 +8,7 @@
******************************************************************************/
package com.openshift.internal.restclient;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import java.net.MalformedURLException;
import java.util.List;
Expand All @@ -18,15 +18,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openshift.internal.restclient.ResourceFactory;
import com.openshift.internal.restclient.model.Project;
import com.openshift.internal.restclient.model.Service;
import com.openshift.internal.restclient.model.template.Template;
import com.openshift.restclient.IClient;
import com.openshift.restclient.IResourceFactory;
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.model.IProject;
import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.IService;
import com.openshift.restclient.model.template.ITemplate;
import com.openshift.restclient.utils.Samples;

Expand All @@ -52,8 +51,9 @@ public void setup () {

@Test
public void testListTemplates(){
ITemplate template = null;
IProject project = null;
Template template = null;
Project project = null;

try {
project = factory.create(VERSION, ResourceKind.Project);
project.setName(helper.generateNamespace());
Expand All @@ -68,7 +68,7 @@ public void testListTemplates(){
for (ITemplate t : list) {
LOG.debug(t.toString());
}
}finally {
} finally {
cleanUpResource(client, template);
cleanUpResource(client, project);
}
Expand All @@ -79,14 +79,14 @@ public void testResourceLifeCycle() throws MalformedURLException {


IProject project = factory.create(VERSION, ResourceKind.Project);
project.setName(helper.generateNamespace());
((Project) project).setName(helper.generateNamespace());
LOG.debug(String.format("Stubbing project: %s", project));

IProject other = factory.create(VERSION, ResourceKind.Project);
other.setName(helper.generateNamespace());
((Project) other).setName(helper.generateNamespace());
LOG.debug(String.format("Stubbing project: %s", project));

IService service = factory.create(VERSION, ResourceKind.Service);
Service service = factory.create(VERSION, ResourceKind.Service);
service.setNamespace(project.getName()); //this will be the project's namespace
service.setName("some-service");
service.setContainerPort(6767);
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.capability.CapabilityVisitor;
import com.openshift.restclient.capability.server.ITemplateProcessing;
import com.openshift.restclient.model.IConfig;
import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.template.ITemplate;
import com.openshift.restclient.utils.Samples;
Expand All @@ -52,7 +51,7 @@ public void setup () throws MalformedURLException{
public void testProcessAndApplyTemplate() throws Exception{
final Collection<IResource> results = new ArrayList<IResource>();
ModelNode node = ModelNode.fromJSONString(Samples.V1BETA3_TEMPLATE.getContentAsString());
final ITemplate template = new Template(node, client, ResourcePropertiesRegistry.getInstance().get(VERSION, ResourceKind.Template));
final Template template = new Template(node, client, ResourcePropertiesRegistry.getInstance().get(VERSION, ResourceKind.Template));
template.setNamespace(COMMON);
try {
client.accept(new CapabilityVisitor<ITemplateProcessing, Object>() {
Expand All @@ -73,7 +72,7 @@ public Object visit(ITemplateProcessing capability) {
return null;
}
}, new Object());
}finally {
} finally {
IntegrationTestHelper.cleanUpResource(client, template);
for (IResource resource : results) {
IntegrationTestHelper.cleanUpResource(client, resource);
Expand Down