### Question:
GraphQLCategoryProvider { List<CategoryTree> getChildCategories(Integer categoryId, Integer depth) { if (magentoGraphqlClient == null || categoryId == null) { LOGGER.debug("No Graphql client present, cannot retrieve top categories"); return Collections.emptyList(); } QueryQuery.CategoryArgumentsDefinition searchArgs = q -> q.id(categoryId); String queryString = Operations.query(query -> query.category(searchArgs, defineCategoriesQuery(depth))).toString(); GraphqlResponse<Query, Error> response = magentoGraphqlClient.execute(queryString); Query rootQuery = response.getData(); CategoryTree category = rootQuery.getCategory(); if (category == null) { LOGGER.warn("Magento category not found for id: " + categoryId); return Collections.emptyList(); } List<CategoryTree> children = category.getChildren(); if (children == null) { return Collections.emptyList(); } return children; } GraphQLCategoryProvider(Resource resource, Page page);   }### Answer:
@Test public void testMissingMagentoGraphqlClient() throws IOException { Page page = Mockito.spy(context.currentPage("/content/pageA")); GraphQLCategoryProvider categoryProvider = new GraphQLCategoryProvider(page .getContentResource(), null); Assert.assertNull(Whitebox.getInternalState(categoryProvider, "magentoGraphqlClient")); Assert.assertTrue(categoryProvider.getChildCategories(10, 10).isEmpty()); }
@Test public void testCategoryNotOrNoChildren() throws IOException { Page page = mock(Page.class); Resource pageContent = mock(Resource.class); when(page.getContentResource()).thenReturn(pageContent); GraphQLCategoryProvider categoryProvider = new GraphQLCategoryProvider(page .getContentResource(), null); MagentoGraphqlClient graphqlClient = mock(MagentoGraphqlClient.class); Whitebox.setInternalState(categoryProvider, "magentoGraphqlClient", graphqlClient); GraphqlResponse<Query, Error> response = mock(GraphqlResponse.class); when(graphqlClient.execute(anyString())).thenReturn(response); Query rootQuery = mock(Query.class); when(response.getData()).thenReturn(rootQuery); Assert.assertNull(rootQuery.getCategory()); Assert.assertTrue(categoryProvider.getChildCategories(-10, 10).isEmpty()); CategoryTree category = mock(CategoryTree.class); when(rootQuery.getCategory()).thenReturn(category); when(category.getChildren()).thenReturn(null); Assert.assertNull(category.getChildren()); Assert.assertTrue(categoryProvider.getChildCategories(13, 10).isEmpty()); }
@Test public void testGetChildCategories() throws IOException { Page page = spy(context.currentPage("/content/pageA")); Resource pageContent = spy(page.getContentResource()); when(page.getContentResource()).thenReturn(pageContent); when(pageContent.adaptTo(GraphqlClient.class)).thenReturn(graphqlClient); when(pageContent.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT); GraphQLCategoryProvider categoryProvider = new GraphQLCategoryProvider(page .getContentResource(), null); Assert.assertTrue(categoryProvider.getChildCategories(null, 5).isEmpty()); List<CategoryTree> categories = categoryProvider.getChildCategories(2, 5); Assert.assertEquals(6, categories.size()); }