1
0
mirror of https://github.com/apache/jclouds.git synced 2025-03-01 05:49:06 +00:00

Fixed DatabagItem parser for search api.

The search api returns the databag item inside a 'raw_data'
object instead of returning it directly.
With this commit all live tests for the search api are passing
except the searchClients one for hosted and private chef due
to an existing issue in the target platform. Once it is fixed (by
opscode) the live test will pass.
This commit is contained in:
Ignasi Barrera 2012-10-01 15:09:36 +02:00
parent 4852e03237
commit 3b361b4ed1
3 changed files with 118 additions and 9 deletions
apis/chef/src
main/java/org/jclouds/chef/functions
test/java/org/jclouds/chef

@ -18,24 +18,64 @@
*/
package org.jclouds.chef.functions;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.chef.domain.DatabagItem;
import org.jclouds.chef.domain.SearchResult;
import org.jclouds.domain.JsonBall;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseJson;
import org.jclouds.json.Json;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.gson.annotations.SerializedName;
/**
* Parses the search result into a {@link DatabagItem} object.
* <p>
* When searching databags, the items are contained inside the <code>raw_data</code> list.
*
* @author Adrian Cole
*/
@Singleton
public class ParseSearchDatabagFromJson extends ParseSearchResultFromJson<DatabagItem> {
public class ParseSearchDatabagFromJson implements Function<HttpResponse, SearchResult<DatabagItem>> {
// TODO add generic json parser detector
private final ParseJson<Response> responseParser;
private final Json json;
@Inject
ParseSearchDatabagFromJson(ParseJson<Response<DatabagItem>> json) {
super(json);
}
static class Row {
@SerializedName("raw_data")
JsonBall rawData;
}
static class Response {
long start;
List<Row> rows;
}
@Inject
ParseSearchDatabagFromJson(ParseJson<Response> responseParser, Json json) {
this.responseParser = responseParser;
this.json = json;
}
@Override
public SearchResult<DatabagItem> apply(HttpResponse arg0) {
Response returnVal = responseParser.apply(arg0);
Iterable<DatabagItem> items =
Iterables.transform(returnVal.rows, new Function<Row, DatabagItem>() {
@Override
public DatabagItem apply(Row input) {
return json.fromJson(input.rawData.toString(), DatabagItem.class);
}
});
return new SearchResult<DatabagItem>(returnVal.start, items);
}
}

@ -0,0 +1,70 @@
/**
* 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.chef.functions;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.chef.ChefAsyncApi;
import org.jclouds.chef.config.ChefParserModule;
import org.jclouds.chef.domain.DatabagItem;
import org.jclouds.chef.domain.SearchResult;
import org.jclouds.http.HttpResponse;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.ApiVersion;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Ignasi Barrera
*/
@Test(groups = { "unit" })
public class ParseSearchDataBagItemFromJsonTest {
private ParseSearchDatabagFromJson handler;
@BeforeTest
protected void setUpInjector() throws IOException {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure()
{
bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefAsyncApi.VERSION);
}
}, new ChefParserModule(), new GsonModule());
handler = injector.getInstance(ParseSearchDatabagFromJson.class);
}
public void test1() {
String itemJson = "{\"my_key\":\"my_data\"}";
String searchJson = "{\"rows\":[{\"raw_data\": {\"id\":\"item1\",\"my_key\":\"my_data\"}}]}";
DatabagItem item = new DatabagItem("item1", itemJson);
SearchResult<DatabagItem> result = handler.apply(HttpResponse.builder()
.statusCode(200)
.message("ok")
.payload(searchJson).build());
assertEquals(result.size(), 1);
assertEquals(result.iterator().next(), item);
}
}

@ -78,7 +78,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
private ChefApi validatorClient;
// It may take a bit until the search index is populated
protected int maxWaitForIndexInMs = 30000;
protected int maxWaitForIndexInMs = 60000;
protected ChefApi chefApi;
@ -434,8 +434,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
assertNotNull(results);
}
// FIXME: The filters works fine, but deserialization of the DatabagItem fails
@Test(enabled = false, dependsOnMethods = {"testListSearchIndexes", "testDatabagItemExists"})
@Test(dependsOnMethods = {"testListSearchIndexes", "testDatabagItemExists"})
public void testSearchDatabagWithOptions() throws Exception {
RetryablePredicate<SearchOptions> waitForIndex =
new RetryablePredicate<SearchOptions>(new Predicate<SearchOptions>() {