AgeraCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { if (getRawType(returnType) != Supplier.class) { return null; } if (!(returnType instanceof ParameterizedType)) { throw new IllegalStateException("Supplier return type must be parameterized" + " as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>"); } Type innerType = getParameterUpperBound(0, (ParameterizedType) returnType); if (getRawType(innerType) != Result.class) { throw new IllegalStateException("Supplier return type must be parameterized" + " as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>"); } Type innerTypeOfInnerType = getParameterUpperBound(0, (ParameterizedType) innerType); if (getRawType(innerTypeOfInnerType) != Response.class) { return new BodyCallAdapter(innerTypeOfInnerType); } if (!(innerTypeOfInnerType instanceof ParameterizedType)) { throw new IllegalStateException("Response must be parameterized" + " as Response<Foo> or Response<? extends Foo>"); } Type responseType = getParameterUpperBound(0, (ParameterizedType) innerTypeOfInnerType); return new ResponseCallAdapter(responseType); } private  AgeraCallAdapterFactory(); static AgeraCallAdapterFactory create(); @Override CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit);  }
@Test public void nonAgeraTypeReturnsNull() { CallAdapter<?, ?> adapter = factory.get(String.class, NO_ANNOTATIONS, retrofit); assertThat(adapter, nullValue()); }
@Test public void responseTypes() { Type oBodyClass = new TypeToken<Supplier<Result<String>>>() {}.getType(); assertThat(factory.get(oBodyClass, NO_ANNOTATIONS, retrofit).responseType(), equalTo(new TypeToken<String>() {}.getType())); Type oBodyWildcard = new TypeToken<Supplier<Result<? extends String>>>() {}.getType(); assertThat(factory.get(oBodyWildcard, NO_ANNOTATIONS, retrofit).responseType(), equalTo(new TypeToken<String>() {}.getType())); Type oBodyGeneric = new TypeToken<Supplier<Result<List<String>>>>() {}.getType(); assertThat(factory.get(oBodyGeneric, NO_ANNOTATIONS, retrofit).responseType(), equalTo(new TypeToken<List<String>>() {}.getType())); Type oResponseClass = new TypeToken<Supplier<Result<Response<String>>>>() {}.getType(); assertThat(factory.get(oResponseClass, NO_ANNOTATIONS, retrofit).responseType(), equalTo(new TypeToken<String>() {}.getType())); Type oResponseWildcard = new TypeToken<Supplier<Result<Response<? extends String>>>>() {}.getType(); assertThat(factory.get(oResponseWildcard, NO_ANNOTATIONS, retrofit).responseType(), equalTo(new TypeToken<String>() {}.getType())); }
@Test public void rawBodyTypeThrows() { Type reservoirType = new TypeToken<Supplier>() {}.getType(); try { factory.get(reservoirType, NO_ANNOTATIONS, retrofit); fail(); } catch (IllegalStateException e) { assertThat(e, hasMessage(containsString( "Supplier return type must be parameterized as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>"))); } }
@Test public void noUseResultAsFirstInnerTypeThrows() { Type reservoirType = new TypeToken<Supplier<String>>() {}.getType(); try { factory.get(reservoirType, NO_ANNOTATIONS, retrofit); fail(); } catch (IllegalStateException e) { assertThat(e, hasMessage(containsString( "Supplier return type must be parameterized as Supplier<Result<Foo>> or Supplier<Result<? extends Foo>>"))); } }
@Test public void rawResponseTypeThrows() { Type reservoirType = new TypeToken<Supplier<Result<Response>>>() {}.getType(); try { factory.get(reservoirType, NO_ANNOTATIONS, retrofit); fail(); } catch (IllegalStateException e) { assertThat(e, hasMessage(containsString( "Response must be parameterized as Response<Foo> or Response<? extends Foo>"))); } }