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
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.jboss.dmr.ModelNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openshift.internal.restclient.model.KubernetesResource;
import com.openshift.restclient.IClient;
import com.openshift.restclient.IOpenShiftWatchListener;
import com.openshift.restclient.IOpenShiftWatchListener.ChangeType;
import com.openshift.restclient.IResourceFactory;
import com.openshift.restclient.IWatcher;
import com.openshift.restclient.OpenShiftException;
Expand Down Expand Up @@ -99,10 +101,11 @@ public void onWebSocketError(Throwable err) {
public void onWebSocketText(String message) {
LOGGER.debug(message);
KubernetesResource payload = factory.create(message);
IOpenShiftWatchListener.ChangeType event = IOpenShiftWatchListener.ChangeType.valueOf(payload.getNode().get("type").asString());
IResource resource = factory.create(payload.getNode().get("object").toJSONString(true));
ModelNode node = payload.getNode();
IOpenShiftWatchListener.ChangeType event = new ChangeType(node.get("type").asString());
IResource resource = factory.create(node.get("object").toJSONString(true));
if(StringUtils.isEmpty(resource.getKind())) {
LOGGER.error("Unable to determine resource kind from: " + payload.getNode().get("object").toJSONString(false));
LOGGER.error("Unable to determine resource kind from: " + node.get("object").toJSONString(false));
}
listener.received(resource, event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,49 @@ public interface IOpenShiftWatchListener {
*/
void received(IResource resource, ChangeType change);

enum ChangeType{
public class ChangeType {

ADDED,
MODIFIED,
DELETED
public static final ChangeType ADDED = new ChangeType("ADDED");
public static final ChangeType MODIFIED = new ChangeType("MODIFIED");
public static final ChangeType DELETED = new ChangeType("DELETED");

private String value;

public ChangeType(String value) {
if (value != null) {
value = value.toUpperCase();
}
this.value = value;
}

public String getValue() {
return value;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ChangeType other = (ChangeType) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!getValue().equals(other.getValue()))
return false;
return true;
}
}

void error(Throwable err);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2015 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.openshift.internal.restclient;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import com.openshift.restclient.IOpenShiftWatchListener.ChangeType;

/**
* @author Andre Dietisheim
*/
public class WatchClientTest {

@Test
public void changeTypeShouldEqualSameChangeType() {
assertThat(ChangeType.ADDED,
equalTo(ChangeType.ADDED));
}

@Test
public void changeTypeShouldNotEqualDifferentChangeType() {
assertThat(ChangeType.ADDED,
not(equalTo(ChangeType.DELETED)));
}

@Test
public void changeTypeShouldEqualSameChangeTypeInLowercase() {
assertThat(ChangeType.ADDED,
equalTo(new ChangeType(ChangeType.ADDED.getValue().toLowerCase())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,30 @@
package com.openshift.restclient;

import static com.openshift.internal.restclient.IntegrationTestHelper.cleanUpResource;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openshift.internal.restclient.IntegrationTestHelper;
import com.openshift.restclient.IClient;
import com.openshift.restclient.IOpenShiftWatchListener;
import com.openshift.restclient.IOpenShiftWatchListener.ChangeType;
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.IService;

public class ClientWatchIntegrationTest {
public class WatchClientIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(ClientWatchIntegrationTest.class);
private static final Logger LOG = LoggerFactory.getLogger(WatchClientIntegrationTest.class);

private IntegrationTestHelper helper = new IntegrationTestHelper();
private IClient client;
Expand Down