Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
add mapPayload(Fn<A,B>) to Response
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwawi committed Nov 29, 2015
1 parent 32aa1ae commit 0eeaa1e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apollo-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>slf4j-test</artifactId>
Expand Down
17 changes: 17 additions & 0 deletions apollo-api/src/main/java/com/spotify/apollo/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -70,9 +71,25 @@ public interface Response<T> {
* headers, etc., are copied over. To clear out the payload, one can pass in {@code null}.
*
* @param newPayload the new payload
* @return A response containing the new payload
*/
<P> Response<P> withPayload(@Nullable P newPayload);

/**
* Maps the contained payload if it exists, otherwise does nothing except changing the type
* of the response.
*
* @param mapFunction The function for mapping the payload
* @param <P> The type of the mapped payload
* @return A response with a converted payload
*/
default <P> Response<P> mapPayload(Function<? super T, ? extends P> mapFunction) {
//noinspection unchecked
return !payload().isPresent()
? (Response<P>) this
: withPayload(mapFunction.apply(payload().get()));
}

/**
* Returns a typed 200 OK {@link Response}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* -\-\-
* Spotify Apollo API Interfaces
* --
* Copyright (C) 2013 - 2015 Spotify AB
* --
* 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 com.spotify.apollo;

import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

@RunWith(Theories.class)
public class ResponsePayloadMapTest {

@DataPoints
public static Response<String>[] responses() {
Response<String> response = Response.<String>forStatus(Status.IM_A_TEAPOT)
.withHeader("foo", "bar");

//noinspection unchecked
return new Response[] {
response,
response.withPayload("hello")

};
}

@Theory
public void mapsPayload(Response<String> input) throws Exception {
Response<Integer> intResponse = input.mapPayload(String::length);

if (input.payload().isPresent()) {
assertThat(intResponse.payload().get().intValue(), is(5));
} else {
assertFalse(intResponse.payload().isPresent());
}
}

@Theory
public void preserveStatusAndHeaderOnMap(Response<String> input) throws Exception {
Response<Integer> intResponse = input.mapPayload(String::length);

assertThat(intResponse.status(), is(Status.IM_A_TEAPOT));
assertThat(intResponse.headers(), hasEntry("foo", "bar"));
}
}

0 comments on commit 0eeaa1e

Please sign in to comment.