Skip to content

Commit

Permalink
Display default categories in summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Nisha Shenoy authored and Nisha Shenoy committed Nov 19, 2019
1 parent a7d9752 commit 524cb61
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.tmobile.pacman.api.asset.repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -428,18 +431,32 @@ public List<Map<String,Object>> getGeneralRecommendationSummary(List<String> pro
JsonParser parser = new JsonParser();
JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject();
JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject();
JsonArray categoryBuckets = aggregations.get("category").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray();
if (categoryBuckets.size() > 0) {
for (int i=0; i<categoryBuckets.size();i++) {
JsonObject categoryObj = (JsonObject) categoryBuckets.get(i);
if (categoryObj != null) {
Map<String,Object> category = new HashMap<>();
category.put("category", categoryObj.get("key").getAsString());
category.put("recommendations", categoryObj.get("doc_count").getAsLong());
recommendationSummary.add(category);
}
}
}

if (aggregations != null) {

JsonArray categoryBuckets = aggregations.get("category").getAsJsonObject().get(Constants.BUCKETS)
.getAsJsonArray();
if (categoryBuckets.size() > 0) {
for (int i = 0; i < categoryBuckets.size(); i++) {
JsonObject categoryObj = (JsonObject) categoryBuckets.get(i);
if (categoryObj != null) {
Map<String, Object> category = new HashMap<>();
category.put("category", categoryObj.get("key").getAsString());
category.put("recommendations", categoryObj.get("doc_count").getAsLong());
recommendationSummary.add(category);
}
}
} else {
// passing 0 values if there are no recommendations
String[] categories = recommendationCategories.split(",");
for (int i = 0; i < categories.length; i++) {
Map<String, Object> category = new HashMap<>();
category.put("category", categories[i]);
category.put("recommendations", 0L);
recommendationSummary.add(category);
}
}
}
return recommendationSummary;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.util.ReflectionTestUtils;

import com.tmobile.pacman.api.commons.exception.DataException;
import com.tmobile.pacman.api.commons.repo.ElasticSearchRepository;
Expand All @@ -45,6 +46,7 @@ public class RecommendationsRepositoryTest {
@Before
public void init() {
recommendationsRepository.init();
ReflectionTestUtils.setField(recommendationsRepository, "recommendationCategories", "fault_tolerance, performance");
}

@Test
Expand All @@ -56,6 +58,14 @@ public void getRecommendationSummaryTest() throws Exception {
assertTrue(recommendationsRepository.getRecommendationSummary("ag", "app").size() == 2);
}

@Test
public void getRecommendationSummaryAzureTest() throws Exception {
String summary = "{\"aggregations\":{\"recommendations\":{\"doc_count\":0}}}";
mockStatic(PacHttpUtils.class);
when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(summary);
assertTrue(recommendationsRepository.getRecommendationSummary("ag", null).size() == 2);
}

@Test
public void getRecommendationSummaryTest_Exception() throws Exception {
mockStatic(PacHttpUtils.class);
Expand Down

0 comments on commit 524cb61

Please sign in to comment.