diff --git a/spring-data-riak/src/main/java/org/springframework/data/riak/core/RiakTemplate.java b/spring-data-riak/src/main/java/org/springframework/data/riak/core/RiakTemplate.java index dc7988800..ef1cbcda7 100644 --- a/spring-data-riak/src/main/java/org/springframework/data/riak/core/RiakTemplate.java +++ b/spring-data-riak/src/main/java/org/springframework/data/riak/core/RiakTemplate.java @@ -548,11 +548,25 @@ public class RiakTemplate extends RestGatewaySupport implements KeyValueStoreOpe public T execute(MapReduceJob job, Class targetType) { RestTemplate restTemplate = getRestTemplate(); - ResponseEntity resp = restTemplate.postForEntity(mapReduceUri, + ResponseEntity resp = restTemplate.postForEntity(mapReduceUri, job.toJson(), - targetType); + List.class); if (resp.hasBody()) { - return resp.getBody(); + if (!targetType.isAssignableFrom(List.class)) { + List results = (List) resp.getBody(); + if (results.size() == 1) { + Object obj = results.get(0); + if (obj.getClass() != targetType) { + ConversionService conv = getConversionService(); + if (conv.canConvert(obj.getClass(), targetType)) { + return conv.convert(obj, targetType); + } + } else { + return (T) obj; + } + } + } + return (T) resp.getBody(); } return null; } diff --git a/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/RiakTemplateSpec.groovy b/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/RiakTemplateSpec.groovy index 4fcd71c5f..749c8022d 100644 --- a/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/RiakTemplateSpec.groovy +++ b/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/RiakTemplateSpec.groovy @@ -178,7 +178,7 @@ class RiakTemplateSpec extends Specification { } - def "Test Map/Reduce"() { + def "Test Map/Reduce returning Integer"() { given: MapReduceJob job = riak.createMapReduceJob() @@ -193,7 +193,29 @@ class RiakTemplateSpec extends Specification { addPhase(reducePhase) when: - def result = riak.execute(job, List) + def result = riak.execute(job, Integer) + + then: + 1 == result + + } + + def "Test Map/Reduce returning List"() { + + given: + MapReduceJob job = riak.createMapReduceJob() + def mapJs = new JavascriptMapReduceOperation("function(v){ return [1]; }") + def mapPhase = new RiakMapReducePhase("map", "javascript", mapJs) + + def reduceJs = new JavascriptMapReduceOperation("function(v){ return [v.length]; }") + def reducePhase = new RiakMapReducePhase("reduce", "javascript", reduceJs) + + job.addInputs(["test"]). + addPhase(mapPhase). + addPhase(reducePhase) + + when: + def result = riak.execute(job) then: 1 == result.size() diff --git a/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/TestObject.java b/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/TestObject.java deleted file mode 100644 index fb37ae547..000000000 --- a/spring-data-riak/src/test/groovy/org/springframework/data/riak/core/TestObject.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2010 by J. Brisbin - * - * Licensed 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.springframework.data.riak.core; - -/** - * @author J. Brisbin - */ -public class TestObject { - String test = "value"; - Integer integer = 12; - - public String getTest() { - return test; - } - - public void setTest(String test) { - this.test = test; - } - - public Integer getInteger() { - return integer; - } - - public void setInteger(Integer integer) { - this.integer = integer; - } -}