Updated listEventTypes implementation

- Changed listEventTypes from Set<EventType> to Set<String>
- Created new parser for EventTypes.
This commit is contained in:
vijaykiran 2011-11-09 12:25:54 +01:00
parent 1c23700b76
commit e89baba4ef
6 changed files with 84 additions and 113 deletions

View File

@ -1,99 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.domain;
import com.google.gson.annotations.SerializedName;
/**
* @author Vijay Kiran
*/
public class EventType implements Comparable<EventType> {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String name;
public Builder name(String name) {
this.name = name;
return this;
}
public EventType build() {
return new EventType(name);
}
}
// for deserialization
EventType() {
}
@SerializedName("name")
private String name;
public EventType(String name) {
this.name = name;
}
/**
* @return the name/name of the OS type
*/
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.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;
EventType other = (EventType) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "[name=" + name + "]";
}
@Override
public int compareTo(EventType arg0) {
return name.compareTo(arg0.getName());
}
}

View File

@ -24,12 +24,12 @@ import javax.ws.rs.core.MediaType;
import java.util.Set;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.EventType;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.functions.ParseEventTypesFromHttpResponse;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.ResponseParser;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
/**
@ -48,9 +48,9 @@ public interface EventAsyncClient {
*/
@GET
@QueryParams(keys = "command", values = "listEventTypes")
@SelectJson("eventtype")
@Consumes(MediaType.APPLICATION_JSON)
@ResponseParser(ParseEventTypesFromHttpResponse.class)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<EventType>> listEventTypes();
ListenableFuture<Set<String>> listEventTypes();
}

View File

@ -21,7 +21,6 @@ package org.jclouds.cloudstack.features;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudstack.domain.EventType;
import org.jclouds.concurrent.Timeout;
/**
@ -39,5 +38,5 @@ public interface EventClient {
*
* @return event types or null if not found
*/
Set<EventType> listEventTypes();
Set<String> listEventTypes();
}

View File

@ -0,0 +1,72 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Singleton;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.inject.Inject;
import com.google.inject.TypeLiteral;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.json.internal.GsonWrapper;
/**
* @author Vijay Kiran
*/
@Singleton
public class ParseEventTypesFromHttpResponse implements Function<HttpResponse, Set<String>> {
private final ParseFirstJsonValueNamed<Set<EventType>> parser;
private static class EventType {
private String name;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
}
@Inject
public ParseEventTypesFromHttpResponse(GsonWrapper gsonWrapper) {
this.parser = new ParseFirstJsonValueNamed<Set<EventType>>(checkNotNull(gsonWrapper, "gsonWrapper"),
new TypeLiteral<Set<EventType>>() {
}, "eventtype");
}
public Set<String> apply(HttpResponse response) {
checkNotNull(response, "response");
Set<EventType> toParse = parser.apply(response);
checkNotNull(toParse, "parsed result from %s", response);
Builder<String> builder = ImmutableSet.<String>builder();
for (EventType entry : toParse)
builder.add(entry.name);
return builder.build();
}
}

View File

@ -22,8 +22,8 @@ import java.io.IOException;
import java.lang.reflect.Method;
import com.google.inject.TypeLiteral;
import org.jclouds.cloudstack.functions.ParseEventTypesFromHttpResponse;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.Test;
@ -47,7 +47,7 @@ public class EventAsyncClientTest extends BaseCloudStackAsyncClientTest<EventAsy
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertResponseParserClassEquals(method, httpRequest, ParseEventTypesFromHttpResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);

View File

@ -22,28 +22,27 @@ import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.cloudstack.domain.EventType;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code EventClient}
*
*
* @author Vijay Kiran
*/
@Test(groups = "live", singleThreaded = true, testName = "EventClientLiveTest")
public class EventClientLiveTest extends BaseCloudStackClientLiveTest {
public void testlistEventTypes() throws Exception {
Set<EventType> response = client.getEventClient().listEventTypes();
Set<String> response = client.getEventClient().listEventTypes();
assert null != response;
assertTrue(response.size() >= 0);
for (EventType type : response) {
for (String type : response) {
checkEventType(type);
}
}
protected void checkEventType(EventType eventType) {
assert eventType.getName() != null : eventType;
protected void checkEventType(String eventType) {
assert eventType != null : eventType;
}
}