Search in sources :

Example 1 with IRecipeCookerCallbacks

use of com.amazon.android.recipe.IRecipeCookerCallbacks in project zype-firebuilder by zype.

the class DynamicParser method cookRecipeObservable.

/**
 * {@inheritDoc}
 */
@Override
public Observable<Object> cookRecipeObservable(Recipe recipe, Object input, Bundle bundle, String[] params) {
    Observable<Object> dynamicParserObservable = Observable.create(subscriber -> {
        try {
            // Make sure recipe and input is valid.
            checkCookRecipeInput(recipe, input);
            // Parse input into a list of maps for translation
            List<Map<String, Object>> resultList = parseInput(recipe, input.toString(), params);
            // Translate each map in the result list into the model object defined in the
            // recipe. Return each model once it completes translation via subscriber.
            translateMapsToObjects(false, recipe, resultList, new IRecipeCookerCallbacks() {

                @Override
                public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
                }

                @Override
                public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(output);
                        if (done) {
                            subscriber.onCompleted();
                        }
                    }
                }

                @Override
                public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
                }

                @Override
                public void onRecipeError(Recipe recipe, Exception e, String msg) {
                    if (e instanceof ValueNotFoundException) {
                        Log.e(TAG, "Error during parsing, skipping an item:", e);
                    } else {
                        subscriber.onError(e);
                    }
                }
            }, bundle);
        } catch (Exception e) {
            subscriber.onError(e);
        }
    });
    return dynamicParserObservable;
}
Also used : IRecipeCookerCallbacks(com.amazon.android.recipe.IRecipeCookerCallbacks) Recipe(com.amazon.android.recipe.Recipe) Bundle(android.os.Bundle) HashMap(java.util.HashMap) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with IRecipeCookerCallbacks

use of com.amazon.android.recipe.IRecipeCookerCallbacks in project zype-firebuilder by zype.

the class DynamicParserTest method testCookRecipeWithReflectionForSampleVideoFeed.

/**
 * Tests the {@link DynamicParser#cookRecipe(Recipe, Object, IRecipeCookerCallbacks, Bundle,
 * String[])} method using reflection with valid input parameters using the sample video feed.
 * This feed contains 3 categories.
 */
@Test
public void testCookRecipeWithReflectionForSampleVideoFeed() throws Exception {
    DynamicParser dynamicParser = new DynamicParser();
    dynamicParser.cookRecipe(createJsonSampleVideoContainerReflectionRecipe(), mJsonSampleFeed, new IRecipeCookerCallbacks() {

        int cookedCount = 0;

        @Override
        public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
            cookedCount++;
            assertNotNull(output);
            assertEquals(output.getClass(), DummyContainer.class);
            if (done) {
                assertEquals(3, cookedCount);
            }
        }

        @Override
        public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeError(Recipe recipe, Exception e, String msg) {
            // Force failure if this happens.
            assertTrue("Recipe should have been cooked without " + "error", false);
        }
    }, null, null);
    dynamicParser.cookRecipe(createXmlSampleVideoContainterReflectionRecipe(), mXmlSampleFeed, new IRecipeCookerCallbacks() {

        int cookedCount = 0;

        @Override
        public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
            cookedCount++;
            assertNotNull(output);
            assertEquals(output.getClass(), DummyContainer.class);
            if (done) {
                assertEquals(3, cookedCount);
            }
        }

        @Override
        public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeError(Recipe recipe, Exception e, String msg) {
            // Force failure if this happens.
            assertTrue("Recipe should have been cooked without " + "error", false);
        }
    }, null, null);
}
Also used : DummyContainer(com.amazon.dynamicparser.testResources.DummyContainer) IRecipeCookerCallbacks(com.amazon.android.recipe.IRecipeCookerCallbacks) Recipe(com.amazon.android.recipe.Recipe) Bundle(android.os.Bundle) Test(org.junit.Test)

Example 3 with IRecipeCookerCallbacks

use of com.amazon.android.recipe.IRecipeCookerCallbacks in project zype-firebuilder by zype.

the class DataLoadManagerTest method createSuccessfulCookedRecipe.

/**
 * Creates a recipe callback handler that expects a successful response with mock data. It
 * fails the test if its {@link IRecipeCookerCallbacks#onRecipeError(Recipe, Exception,
 * String)}
 * method is called
 *
 * @return A recipe callback handler which expects successful response with mock data.
 */
private IRecipeCookerCallbacks createSuccessfulCookedRecipe() {
    return new IRecipeCookerCallbacks() {

        @Override
        public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
            Data data = (Data) output;
            assertTrue(mData.equals(data));
            assertTrue(done);
            verifyUtil.verified();
        }

        @Override
        public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeError(Recipe recipe, Exception e, String msg) {
            Log.e("Test failed", msg, e);
            fail(e.getMessage());
        }
    };
}
Also used : IRecipeCookerCallbacks(com.amazon.android.recipe.IRecipeCookerCallbacks) Recipe(com.amazon.android.recipe.Recipe) Bundle(android.os.Bundle) Data(com.amazon.utils.model.Data) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with IRecipeCookerCallbacks

use of com.amazon.android.recipe.IRecipeCookerCallbacks in project zype-firebuilder by zype.

the class DynamicParserTest method testCancelTranslationTasks.

/**
 * Tests the {@link DynamicParser#cancelTranslationTasks()} method using a large feed of 5000
 * items. The feed should be large enough that we can prove canceling the task works before the
 * task has a chance to finish.
 */
@Test
public void testCancelTranslationTasks() throws Exception {
    DynamicParser dynamicParser = new DynamicParser();
    dynamicParser.setAsyncMode(true);
    // Test the method using a large JSON feed.
    String feed = FileHelper.readFile(InstrumentationRegistry.getContext(), "feeds/5000PhotosFeed.json");
    Recipe reflectionRecipe = createParserRecipe(// cooker
    "DynamicParser", // format
    "json", // model
    "com.amazon.dynamicparser.testResources.PhotoModel", // model type
    "array", // translator
    null, // query
    "$.photos", // query result type
    null, // key data path
    null, new ArrayList<>(// match list
    Arrays.asList("albumId@albumId", "id@id", "title@title", "url@url", "thumbnailUrl@thumbnailUrl")));
    dynamicParser.cookRecipe(reflectionRecipe, feed, new IRecipeCookerCallbacks() {

        @Override
        public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
            assertTrue("Should not have reached this", false);
        }

        @Override
        public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeError(Recipe recipe, Exception e, String msg) {
            assertTrue("Should not have reached this", false);
        }
    }, null, null);
    dynamicParser.cancelTranslationTasks();
    assertFalse(dynamicParser.areTasksInProgress());
    // Test the method using a large XML feed.
    feed = FileHelper.readFile(InstrumentationRegistry.getContext(), "feeds/5000PhotosFeed.xml");
    reflectionRecipe = createParserRecipe(// cooker
    "DynamicParser", // format
    "xml", // model
    "com.amazon.dynamicparser.testResources.PhotoModel", // model type
    "array", // translator
    null, // query
    "photos/photo", // query result type
    null, // key data path
    null, new ArrayList<>(// match list
    Arrays.asList("albumId/#text@albumId", "id/#text@id", "title/#text@title", "url/#text@url", "thumbnailUrl/#text@thumbnailUrl")));
    dynamicParser.cookRecipe(reflectionRecipe, feed, new IRecipeCookerCallbacks() {

        @Override
        public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
            assertTrue("Should not have reached this", false);
        }

        @Override
        public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeError(Recipe recipe, Exception e, String msg) {
            assertTrue("Should not have reached this", false);
        }
    }, null, null);
    dynamicParser.cancelTranslationTasks();
    assertFalse(dynamicParser.areTasksInProgress());
}
Also used : IRecipeCookerCallbacks(com.amazon.android.recipe.IRecipeCookerCallbacks) Recipe(com.amazon.android.recipe.Recipe) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with IRecipeCookerCallbacks

use of com.amazon.android.recipe.IRecipeCookerCallbacks in project zype-firebuilder by zype.

the class DynamicParserTest method testParseToString.

/**
 * Tests parsing json file containing a list of strings to a String object.
 * @throws Exception
 */
@Test
public void testParseToString() throws Exception {
    DynamicParser dynamicParser = new DynamicParser();
    dynamicParser.setBatchMode(true);
    Recipe recipe = Recipe.newInstance(InstrumentationRegistry.getContext(), "recipes/ParseStringObjectRecipe.json");
    String feed = FileHelper.readFile(InstrumentationRegistry.getContext(), "feeds/ContentIdFeed.json");
    dynamicParser.cookRecipe(recipe, feed, new IRecipeCookerCallbacks() {

        @Override
        public void onPreRecipeCook(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeCooked(Recipe recipe, Object output, Bundle bundle, boolean done) {
            assertNotNull(output);
            List<String> ids = (List<String>) output;
            assertEquals("Should have returned 3 items", 3, ids.size());
        }

        @Override
        public void onPostRecipeCooked(Recipe recipe, Object output, Bundle bundle) {
        }

        @Override
        public void onRecipeError(Recipe recipe, Exception e, String msg) {
            // Force failure if this happens.
            assertTrue("Recipe should have been cooked without error", false);
        }
    }, null, null);
}
Also used : IRecipeCookerCallbacks(com.amazon.android.recipe.IRecipeCookerCallbacks) Recipe(com.amazon.android.recipe.Recipe) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Bundle (android.os.Bundle)5 IRecipeCookerCallbacks (com.amazon.android.recipe.IRecipeCookerCallbacks)5 Recipe (com.amazon.android.recipe.Recipe)5 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 DummyContainer (com.amazon.dynamicparser.testResources.DummyContainer)1 Data (com.amazon.utils.model.Data)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1