Created
July 11, 2016 17:43
-
-
Save ferrarafer/8170fc430bdec992db9842b01b7b802f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ferrarafer.android.androidrecipes.recipemain; | |
import com.ferrarafer.android.androidrecipes.BuildConfig; | |
import com.ferrarafer.android.androidrecipes.api.RecipeSearchResponse; | |
import com.ferrarafer.android.androidrecipes.api.RecipeService; | |
import com.ferrarafer.android.androidrecipes.entities.Recipe; | |
import com.ferrarafer.android.androidrecipes.libs.base.EventBus; | |
import com.ferrarafer.android.androidrecipes.recipemain.events.RecipeMainEvent; | |
import java.util.Random; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
/** | |
* Created by Fernando on 28/6/2016. | |
*/ | |
public class RecipeMainRepositoryImpl implements RecipeMainRepository { | |
private int recipePage; | |
private EventBus eventBus; | |
private RecipeService service; | |
public RecipeMainRepositoryImpl(EventBus eventBus, RecipeService service) { | |
this.eventBus = eventBus; | |
this.service = service; | |
this.recipePage = new Random().nextInt(RECIPE_RANGE); | |
} | |
@Override | |
public void getNextRecipe() { | |
Call<RecipeSearchResponse> call = service.search( | |
BuildConfig.FOOD_API_KEY, RECENT_SORT, COUNT, recipePage); | |
Callback<RecipeSearchResponse> callback = new Callback<RecipeSearchResponse>() { | |
@Override | |
public void onResponse(Call<RecipeSearchResponse> call, Response<RecipeSearchResponse> response) { | |
if (response.isSuccess()) { | |
RecipeSearchResponse recipeSearchResponse = response.body(); | |
if (recipeSearchResponse.getCount() == 0) { | |
setRecipePage(new Random().nextInt(RECIPE_RANGE)); | |
getNextRecipe(); | |
} else { | |
Recipe recipe = recipeSearchResponse.getFirstRecipe(); | |
if (recipe != null) { | |
post(recipe); | |
} else { | |
post(response.message()); | |
} | |
} | |
} else { | |
post(response.message()); | |
} | |
} | |
@Override | |
public void onFailure(Call<RecipeSearchResponse> call, Throwable t) { | |
post(t.getLocalizedMessage()); | |
} | |
}; | |
call.enqueue(callback); | |
} | |
@Override | |
public void saveRecipe(Recipe recipe) { | |
recipe.save(); | |
post(); | |
} | |
@Override | |
public void setRecipePage(int recipePage) { | |
this.recipePage = recipePage; | |
} | |
private void post(String error, int type, Recipe recipe) { | |
RecipeMainEvent event = new RecipeMainEvent(); | |
event.setType(type); | |
event.setError(error); | |
event.setRecipe(recipe); | |
eventBus.post(event); | |
} | |
private void post(Recipe recipe) { | |
post(null, RecipeMainEvent.NEXT_EVENT, recipe); | |
} | |
private void post(String error) { | |
post(error, RecipeMainEvent.NEXT_EVENT, null); | |
} | |
private void post() { | |
post(null, RecipeMainEvent.SAVE_EVENT, null); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ferrarafer.android.androidrecipes.recipemain; | |
import com.ferrarafer.android.androidrecipes.BaseTest; | |
import com.ferrarafer.android.androidrecipes.BuildConfig; | |
import com.ferrarafer.android.androidrecipes.api.RecipeSearchResponse; | |
import com.ferrarafer.android.androidrecipes.api.RecipeService; | |
import com.ferrarafer.android.androidrecipes.entities.Recipe; | |
import com.ferrarafer.android.androidrecipes.libs.base.EventBus; | |
import com.ferrarafer.android.androidrecipes.recipemain.events.RecipeMainEvent; | |
import org.junit.Test; | |
import org.mockito.ArgumentCaptor; | |
import org.mockito.Mock; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.Random; | |
import okhttp3.Request; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
import static junit.framework.Assert.assertEquals; | |
import static junit.framework.Assert.assertNotNull; | |
import static junit.framework.Assert.assertNull; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
/** | |
* Created by Fernando on 6/7/2016. | |
*/ | |
public class RecipeMainRepositoryImplTest extends BaseTest { | |
@Mock | |
private EventBus eventBus; | |
@Mock | |
private RecipeService service; | |
@Mock | |
private Recipe recipe; | |
private RecipeMainRepository repository; | |
private ArgumentCaptor<RecipeMainEvent> recipeMainEventArgumentCaptor; | |
@Override | |
public void setUp() throws Exception { | |
super.setUp(); | |
repository = new RecipeMainRepositoryImpl(eventBus, service); | |
recipeMainEventArgumentCaptor = ArgumentCaptor.forClass(RecipeMainEvent.class); | |
} | |
@Test | |
public void testSaveRecipeCalled_EventPosted() throws Exception { | |
when(recipe.exists()).thenReturn(true); | |
repository.saveRecipe(recipe); | |
verify(eventBus).post(recipeMainEventArgumentCaptor.capture()); | |
RecipeMainEvent event = recipeMainEventArgumentCaptor.getValue(); | |
assertEquals(RecipeMainEvent.SAVE_EVENT, event.getType()); | |
assertNull(event.getError()); | |
assertNull(event.getRecipe()); | |
} | |
@Test | |
public void testGetNextRecipeCalled_APIServiceSuccessCall_EventPosted() throws Exception { | |
int recipePage = new Random().nextInt(RecipeMainRepository.RECIPE_RANGE); | |
when(service.search(BuildConfig.FOOD_API_KEY, | |
RecipeMainRepository.RECENT_SORT, | |
RecipeMainRepository.COUNT, recipePage)) | |
.thenReturn(buildCall(true, null)); | |
repository.setRecipePage(recipePage); | |
repository.getNextRecipe(); | |
verify(service).search(BuildConfig.FOOD_API_KEY, | |
RecipeMainRepository.RECENT_SORT, | |
RecipeMainRepository.COUNT, recipePage); | |
verify(eventBus).post(recipeMainEventArgumentCaptor.capture()); | |
RecipeMainEvent event = recipeMainEventArgumentCaptor.getValue(); | |
assertEquals(RecipeMainEvent.NEXT_EVENT, event.getType()); | |
assertNull(event.getError()); | |
assertNotNull(event.getRecipe()); | |
assertEquals(recipe, event.getRecipe()); | |
} | |
@Test | |
public void testGetNextRecipeCalled_APIServiceFailedCall_EventPosted() throws Exception { | |
String errorMsg = "error"; | |
int recipePage = new Random().nextInt(RecipeMainRepository.RECIPE_RANGE); | |
when(service.search(BuildConfig.FOOD_API_KEY, | |
RecipeMainRepository.RECENT_SORT, | |
RecipeMainRepository.COUNT, recipePage)) | |
.thenReturn(buildCall(true, errorMsg)); | |
repository.setRecipePage(recipePage); | |
repository.getNextRecipe(); | |
verify(service).search(BuildConfig.FOOD_API_KEY, | |
RecipeMainRepository.RECENT_SORT, | |
RecipeMainRepository.COUNT, recipePage); | |
verify(eventBus).post(recipeMainEventArgumentCaptor.capture()); | |
RecipeMainEvent event = recipeMainEventArgumentCaptor.getValue(); | |
assertEquals(RecipeMainEvent.NEXT_EVENT, event.getType()); | |
assertNull(event.getRecipe()); | |
assertNotNull(event.getError()); | |
assertEquals(errorMsg, event.getError()); | |
} | |
private Call<RecipeSearchResponse> buildCall(final boolean success, final String errorMsg) { | |
Call<RecipeSearchResponse> response = new Call<RecipeSearchResponse>() { | |
@Override | |
public Response<RecipeSearchResponse> execute() throws IOException { | |
Response<RecipeSearchResponse> result = null; | |
if (success) { | |
RecipeSearchResponse response = new RecipeSearchResponse(); | |
response.setCount(1); | |
response.setRecipes(Arrays.asList(recipe)); | |
result = Response.success(response); | |
} else { | |
result = Response.error(null, null); | |
} | |
return result; | |
} | |
@Override | |
public void enqueue(Callback<RecipeSearchResponse> callback) { | |
if (success) { | |
try { | |
callback.onResponse(this, execute()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
callback.onFailure(this, new Throwable(errorMsg)); | |
} | |
} | |
@Override | |
public boolean isExecuted() { | |
return true; | |
} | |
@Override | |
public void cancel() { | |
} | |
@Override | |
public boolean isCanceled() { | |
return false; | |
} | |
@Override | |
public Call<RecipeSearchResponse> clone() { | |
return null; | |
} | |
@Override | |
public Request request() { | |
return null; | |
} | |
}; | |
return response; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
junit.framework.AssertionFailedError: Expected: <null> but was: recipe | |
Expected :<null> | |
Actual :recipe | |
<Click to see difference> | |
at junit.framework.Assert.fail(Assert.java:57) | |
at junit.framework.Assert.assertTrue(Assert.java:22) | |
at junit.framework.Assert.assertNull(Assert.java:277) | |
at junit.framework.Assert.assertNull(Assert.java:268) | |
at com.ferrarafer.android.androidrecipes.recipemain.RecipeMainRepositoryImplTest.testGetNextRecipeCalled_APIServiceFailedCall_EventPosted(RecipeMainRepositoryImplTest.java:105) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:498) | |
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) | |
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) | |
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) | |
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) | |
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) | |
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) | |
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) | |
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) | |
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) | |
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) | |
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) | |
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) | |
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) | |
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) | |
at org.junit.runners.Suite.runChild(Suite.java:128) | |
at org.junit.runners.Suite.runChild(Suite.java:27) | |
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) | |
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) | |
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) | |
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) | |
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) | |
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) | |
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) | |
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) | |
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) | |
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:498) | |
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment