diff --git a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java index 0ec114bad4e4204e4c8e17b31aab5987c37f1ecb..5c9ed8eff4b2c42f33b09bef7276e0a9c3b41bcf 100644 --- a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java +++ b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java @@ -596,9 +596,6 @@ public final class JsonKey { public static final String NIC = "NIC"; public static final String SMS_GATEWAY_PROVIDER = "sms_gateway_provider"; public static final String WELCOME_SMS_TEMPLATE = "welcomeSmsTemplate"; - public static final String IDS = "ids"; - public static final String NOTIFICATIONS = "notifications"; - public static final String FEEDS = "feeds"; public static final String EMAIL_VERIFICATION_SUBJECT = "OTP to verify Email"; public static final String CONTACT_UPDATE_TEMPLATE_ID = "otpContactUpdateTemplate"; public static final String OTP_CONTACT_UPDATE_TEMPLATE_EMAIL = "otpContactUpdateTemplateEmail"; diff --git a/service/src/main/java/org/sunbird/actor/feed/UserFeedActor.java b/service/src/main/java/org/sunbird/actor/feed/UserFeedActor.java index a2f86483d1e10a2d3422d479b71c27c697e66de4..87516f4d3272c2389156a19138403ff548121cc2 100644 --- a/service/src/main/java/org/sunbird/actor/feed/UserFeedActor.java +++ b/service/src/main/java/org/sunbird/actor/feed/UserFeedActor.java @@ -1,21 +1,25 @@ package org.sunbird.actor.feed; import java.util.*; + +import com.fasterxml.jackson.databind.ObjectMapper; import org.sunbird.actor.core.BaseActor; import org.sunbird.keys.JsonKey; import org.sunbird.model.user.Feed; +import org.sunbird.model.user.FeedStatus; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; import org.sunbird.service.feed.FeedFactory; import org.sunbird.service.feed.IFeedService; import org.sunbird.telemetry.dto.TelemetryEnvKey; +import org.sunbird.util.ProjectUtil; import org.sunbird.util.Util; public class UserFeedActor extends BaseActor { private final IFeedService feedService = FeedFactory.getInstance(); - + ObjectMapper mapper = new ObjectMapper(); @Override public void onReceive(Request request) throws Throwable { Util.initializeContext(request, TelemetryEnvKey.USER); @@ -53,14 +57,31 @@ public class UserFeedActor extends BaseActor { } private void createUserFeed(Request request, RequestContext context) { - request.getRequest().put(JsonKey.CREATED_BY, request.getContext().get(JsonKey.REQUESTED_BY)); - Response feedCreateResponse = feedService.insert(request, context); + Feed feed = mapper.convertValue(request.getRequest(), Feed.class); + feed.setStatus(FeedStatus.UNREAD.getfeedStatus()); + feed.setCreatedBy((String) request.getContext().get(JsonKey.REQUESTED_BY)); + Response feedCreateResponse = feedService.insert(feed, context); sender().tell(feedCreateResponse, self()); + // Delete the old user feed + Map<String, Object> reqMap = new WeakHashMap<>(2); + reqMap.put(JsonKey.USER_ID, feed.getUserId()); + List<Feed> feedList = feedService.getFeedsByProperties(reqMap, context); + if (feedList.size() >= Integer.parseInt(ProjectUtil.getConfigValue(JsonKey.FEED_LIMIT))) { + feedList.sort(Comparator.comparing(Feed::getCreatedOn)); + Feed delRecord = feedList.get(0); + feedService.delete( + delRecord.getId(), delRecord.getUserId(), delRecord.getCategory(), context); + } } private void deleteUserFeed(Request request, RequestContext context) { Response feedDeleteResponse = new Response(); - feedService.delete(request, context); + Map<String, Object> deleteRequest = request.getRequest(); + feedService.delete( + (String) deleteRequest.get(JsonKey.FEED_ID), + (String) deleteRequest.get(JsonKey.USER_ID), + (String) deleteRequest.get(JsonKey.CATEGORY), + context); feedDeleteResponse.getResult().put(JsonKey.RESPONSE, JsonKey.SUCCESS); sender().tell(feedDeleteResponse, self()); } @@ -68,9 +89,11 @@ public class UserFeedActor extends BaseActor { private void updateUserFeed(Request request, RequestContext context) { Map<String, Object> updateRequest = request.getRequest(); String feedId = (String) updateRequest.get(JsonKey.FEED_ID); - updateRequest.put(JsonKey.IDS, Arrays.asList(feedId)); - updateRequest.put(JsonKey.UPDATED_BY, request.getContext().get(JsonKey.REQUESTED_BY)); - Response feedUpdateResponse = feedService.update(request, context); + Feed feed = mapper.convertValue(updateRequest, Feed.class); + feed.setId(feedId); + feed.setStatus(FeedStatus.READ.getfeedStatus()); + feed.setUpdatedBy((String) request.getContext().get(JsonKey.REQUESTED_BY)); + Response feedUpdateResponse = feedService.update(feed, context); sender().tell(feedUpdateResponse, self()); } } diff --git a/service/src/main/java/org/sunbird/service/feed/IFeedService.java b/service/src/main/java/org/sunbird/service/feed/IFeedService.java index 1aab537aa134a59f4e84816425d6793c88f24769..ecb2866059e96c1e7d7b98763a5815acc2cebfe5 100644 --- a/service/src/main/java/org/sunbird/service/feed/IFeedService.java +++ b/service/src/main/java/org/sunbird/service/feed/IFeedService.java @@ -3,7 +3,6 @@ package org.sunbird.service.feed; import java.util.List; import java.util.Map; import org.sunbird.model.user.Feed; -import org.sunbird.request.Request; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; @@ -14,21 +13,21 @@ public interface IFeedService { * this method will be responsible to insert the feed in the user_feed table and sync the data * with the ES * - * @param request + * @param feed * @param context * @return response */ - Response insert(Request request, RequestContext context); + Response insert(Feed feed, RequestContext context); /** * this method will be responsible to update the feed in the user_feed table and sync the data * with the ES * - * @param request + * @param feed * @param context * @return response */ - Response update(Request request, RequestContext context); + Response update(Feed feed, RequestContext context); /** * this method will be responsible to get the records by userId from the user_feed table @@ -42,8 +41,10 @@ public interface IFeedService { /** * this method will be holding responsibility to delete the feed from DB and ES. * - * @param request + * @param id + * @param userId + * @param action * @param context */ - Response delete(Request request, RequestContext context); + void delete(String id, String userId, String category, RequestContext context); } diff --git a/service/src/main/java/org/sunbird/service/feed/impl/FeedServiceImpl.java b/service/src/main/java/org/sunbird/service/feed/impl/FeedServiceImpl.java index 861eb1ec6c4ee16ca50a126d2fff6ce6453f1e83..4f8e0a8783d9ec210198487faea92a10f7afa2d7 100644 --- a/service/src/main/java/org/sunbird/service/feed/impl/FeedServiceImpl.java +++ b/service/src/main/java/org/sunbird/service/feed/impl/FeedServiceImpl.java @@ -1,17 +1,18 @@ package org.sunbird.service.feed.impl; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; + +import java.sql.Timestamp; +import java.util.*; + import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; +import org.sunbird.dao.feed.IFeedDao; +import org.sunbird.dao.feed.impl.FeedDaoImpl; import org.sunbird.exception.ProjectCommonException; import org.sunbird.exception.ResponseCode; -import org.sunbird.http.HttpClientUtil; import org.sunbird.keys.JsonKey; import org.sunbird.logging.LoggerUtil; import org.sunbird.model.user.Feed; @@ -23,103 +24,82 @@ import org.sunbird.util.ProjectUtil; public class FeedServiceImpl implements IFeedService { private final LoggerUtil logger = new LoggerUtil(FeedServiceImpl.class); - private static final String notification_service_base_url = - System.getenv("notification_service_base_url"); + private static IFeedDao iFeedDao = FeedDaoImpl.getInstance(); private final ObjectMapper mapper = new ObjectMapper(); @Override - public Response insert(Request request, RequestContext context) { - String NOTIFICATION_SERVICE_URL = - notification_service_base_url + "/private/v2/notification/send"; - logger.debug( - context, - "FeedServiceImpl:insert :: calling notification service URL :" + NOTIFICATION_SERVICE_URL); - Response response = new Response(); - Request req = new Request(); - Map<String, Object> reqObj = new HashMap<>(); - reqObj.put(JsonKey.NOTIFICATIONS, Arrays.asList(request.getRequest())); - req.setRequest(reqObj); + public Response insert(Feed feed, RequestContext context) { + + logger.debug(context, "FeedServiceImpl:insert method called : "); + Map<String, Object> dbMap = mapper.convertValue(feed, Map.class); + String feedId = ProjectUtil.generateUniqueId(); + dbMap.put(JsonKey.ID, feedId); + dbMap.put(JsonKey.CREATED_ON, new Timestamp(Calendar.getInstance().getTimeInMillis())); try { - String json = mapper.writeValueAsString(req); - json = new String(json.getBytes(), StandardCharsets.UTF_8); - String responseStr = - HttpClientUtil.post(NOTIFICATION_SERVICE_URL, json, getHeaders(context), context); - logger.debug(context, "FeedServiceImpl:insert :: Response =" + response); - response = mapper.readValue(responseStr, Response.class); + if (MapUtils.isNotEmpty(feed.getData())) { + dbMap.put(JsonKey.FEED_DATA, mapper.writeValueAsString(feed.getData())); + } } catch (Exception ex) { logger.error(context, "FeedServiceImpl:insert Exception occurred while mapping.", ex); ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } - return response; + return iFeedDao.insert(dbMap, context); } @Override - public Response update(Request request, RequestContext context) { - String NOTIFICATION_SERVICE_URL = - notification_service_base_url + "/private/v1/notification/feed/update"; - Response response = new Response(); + public Response update(Feed feed, RequestContext context) { + logger.debug(context, "FeedServiceImpl:update method called : "); + Map<String, Object> dbMap = mapper.convertValue(feed, Map.class); try { - String json = mapper.writeValueAsString(request); - json = new String(json.getBytes(), StandardCharsets.UTF_8); - String responseStr = - HttpClientUtil.patch(NOTIFICATION_SERVICE_URL, json, getHeaders(context), context); - logger.debug(context, "FeedServiceImpl:insert :: Response =" + response); - response = mapper.readValue(responseStr, Response.class); + if (MapUtils.isNotEmpty(feed.getData())) { + dbMap.put(JsonKey.FEED_DATA, mapper.writeValueAsString(feed.getData())); + } } catch (Exception ex) { logger.error(context, "FeedServiceImpl:update Exception occurred while mapping.", ex); ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } - return response; + dbMap.remove(JsonKey.CREATED_ON); + dbMap.put(JsonKey.UPDATED_ON, new Timestamp(Calendar.getInstance().getTimeInMillis())); + return iFeedDao.update(dbMap, context); } @Override public List<Feed> getFeedsByProperties(Map<String, Object> properties, RequestContext context) { - String NOTIFICATION_SERVICE_URL = - notification_service_base_url - + "/private/v1/notification/feed/read/" - + properties.get(JsonKey.USER_ID); - logger.debug( - context, - "FeedServiceImpl:insert :: calling notification service URL :" + NOTIFICATION_SERVICE_URL); + logger.debug(context, "FeedServiceImpl:getFeedsByUserId method called : "); + Response dbResponse = iFeedDao.getFeedsByProperties(properties, context); + List<Map<String, Object>> responseList = null; List<Feed> feedList = new ArrayList<>(); - try { - String response = HttpClientUtil.get(NOTIFICATION_SERVICE_URL, getHeaders(context), context); - logger.debug(context, "FeedServiceImpl:callNotificationService :: Response =" + response); - if (!StringUtils.isBlank(response)) { - Response notificationRes = mapper.readValue(response, Response.class); - List<Map<String, Object>> feeds = - (List<Map<String, Object>>) notificationRes.getResult().get(JsonKey.FEEDS); - if (CollectionUtils.isNotEmpty(feeds)) { - for (Map<String, Object> feed : feeds) { - feedList.add(mapper.convertValue(feed, Feed.class)); - } - } + if (null != dbResponse && null != dbResponse.getResult()) { + responseList = (List<Map<String, Object>>) dbResponse.getResult().get(JsonKey.RESPONSE); + if (CollectionUtils.isNotEmpty(responseList)) { + responseList.forEach( + s -> { + try { + String data = (String) s.get(JsonKey.FEED_DATA); + if (StringUtils.isNotBlank(data)) { + s.put( + JsonKey.FEED_DATA, + mapper.readValue(data, new TypeReference<Map<String, Object>>() {})); + } else { + s.put(JsonKey.FEED_DATA, Collections.emptyMap()); + } + feedList.add(mapper.convertValue(s, Feed.class)); + } catch (Exception ex) { + logger.error( + context, + "FeedServiceImpl:getRecordsByUserId :Exception occurred while mapping feed data.", + ex); + } }); } - } catch (Exception ex) { - logger.error(context, "FeedServiceImpl:read Exception occurred while mapping.", ex); - ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } return feedList; } @Override - public Response delete(Request request, RequestContext context) { - String NOTIFICATION_SERVICE_URL = - notification_service_base_url + "/private/v1/notification/feed/delete"; - Response response = new Response(); - request.getRequest().put(JsonKey.IDS, Arrays.asList(request.getRequest().get(JsonKey.FEED_ID))); - try { - String json = mapper.writeValueAsString(request); - json = new String(json.getBytes(), StandardCharsets.UTF_8); - String responseStr = - HttpClientUtil.post(NOTIFICATION_SERVICE_URL, json, getHeaders(context), context); - logger.debug(context, "FeedServiceImpl:insert :: Response =" + response); - response = mapper.readValue(responseStr, Response.class); - } catch (Exception ex) { - logger.error(context, "FeedServiceImpl:read Exception occurred while mapping.", ex); - ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); - } - return response; + public void delete(String id, String userId, String category, RequestContext context) { + logger.debug( + context, "FeedServiceImpl:delete method called for feedId : " + id + "user-id:" + userId); + iFeedDao.delete(id, userId, category, context); } private Map<String, String> getHeaders(RequestContext context) { diff --git a/service/src/main/java/org/sunbird/util/feed/FeedUtil.java b/service/src/main/java/org/sunbird/util/feed/FeedUtil.java index f8fb681c5d8e52471974283695788c96e28c42d7..250b852601895ff35311580da0ba75cdf538b413 100644 --- a/service/src/main/java/org/sunbird/util/feed/FeedUtil.java +++ b/service/src/main/java/org/sunbird/util/feed/FeedUtil.java @@ -61,18 +61,12 @@ public class FeedUtil { (ArrayList<Map<String, String>>) data.get(JsonKey.PROSPECT_CHANNELS_IDS); orgList.addAll(getOrgDetails(shadowUser.getChannel(), context)); } - Request request = new Request(); - ObjectMapper mapper = new ObjectMapper(); - request.setRequest(mapper.convertValue(feedList.get(index), Map.class)); - response = feedService.update(request, context); + response = feedService.update(feedList.get(index), context); } return response; } - private static Request createFeedObj( - ShadowUser shadowUser, String userId, RequestContext context) { - Request request = new Request(); - ObjectMapper mapper = new ObjectMapper(); + private static Feed createFeedObj(ShadowUser shadowUser, String userId, RequestContext context){ Feed feed = new Feed(); feed.setPriority(1); feed.setCreatedBy(shadowUser.getAddedBy()); @@ -86,8 +80,7 @@ public class FeedUtil { JsonKey.PROSPECT_CHANNELS_IDS, getOrgDetails(shadowUser.getChannel(), context)); feed.setData(prospectsChannel); feed.setUserId(userId); - request.setRequest(mapper.convertValue(feed, Map.class)); - return request; + return feed; } private static List<Map<String, String>> getOrgDetails(String channel, RequestContext context) { diff --git a/service/src/test/java/org/sunbird/actor/user/UserFeedActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserFeedActorTest.java index d41a1c14ed6831f9091077278051092ee5a11cbf..7c0e7e322ae008e873e81635a4f83f764cb3a2ef 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserFeedActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserFeedActorTest.java @@ -8,9 +8,9 @@ import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import akka.testkit.javadsl.TestKit; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -43,8 +43,7 @@ import org.sunbird.service.feed.impl.FeedServiceImpl; CassandraOperationImpl.class, IFeedService.class, FeedServiceImpl.class, - org.sunbird.datasecurity.impl.ServiceFactory.class, - HttpClientUtil.class + org.sunbird.datasecurity.impl.ServiceFactory.class }) @SuppressStaticInitializationFor("org.sunbird.common.ElasticSearchUtil") @PowerMockIgnore({ @@ -62,7 +61,7 @@ public class UserFeedActorTest { private static CassandraOperation cassandraOperation = null; @Before - public void setUp() throws JsonProcessingException { + public void setUp() { PowerMockito.mockStatic(ServiceFactory.class); userFeed.put(JsonKey.ID, "123-456-789"); response = new Response(); @@ -76,23 +75,18 @@ public class UserFeedActorTest { Map<String, Object> responseMap2 = new HashMap<>(); responseMap2.put(Constants.RESPONSE, Constants.SUCCESS); upsertResponse.getResult().putAll(responseMap2); - ObjectMapper Obj = new ObjectMapper(); - String jsonStr = Obj.writeValueAsString(upsertResponse); - PowerMockito.mockStatic(HttpClientUtil.class); PowerMockito.when( - HttpClientUtil.post( - Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(jsonStr); + cassandraOperation.insertRecord( + Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(upsertResponse); PowerMockito.when( - HttpClientUtil.patch( - Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(jsonStr); - PowerMockito.when(HttpClientUtil.get(Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(getUserFeedData()); + cassandraOperation.updateRecord( + Mockito.any(), Mockito.any(), Mockito.anyMap(), Mockito.anyMap(), Mockito.any())) + .thenReturn(upsertResponse); } @Test - public void getUserFeedTest() throws JsonProcessingException { + public void getUserFeedTest() { TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); Request reqObj = new Request(); @@ -152,7 +146,6 @@ public class UserFeedActorTest { TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); subject.tell(reqObj, probe.getRef()); - if (errorCode == null) { Response res = probe.expectMsgClass(duration("100 second"), Response.class); return null != res && res.getResponseCode() == ResponseCode.OK; @@ -164,22 +157,4 @@ public class UserFeedActorTest { } } - public String getUserFeedData() { - Response response = new Response(); - Map<String, Object> result = new HashMap<>(); - List<Map<String, Object>> feeds = new ArrayList<>(); - Map<String, Object> feed = new HashMap<>(); - feed.put(JsonKey.ID, "12312312"); - feeds.add(feed); - result.put(JsonKey.FEEDS, feeds); - response.putAll(result); - ObjectMapper Obj = new ObjectMapper(); - String jsonStr = null; - try { - jsonStr = Obj.writeValueAsString(response); - } catch (Exception e) { - Assert.assertFalse(false); - } - return jsonStr; - } } diff --git a/service/src/test/java/org/sunbird/service/feed/impl/FeedServiceImplTest.java b/service/src/test/java/org/sunbird/service/feed/impl/FeedServiceImplTest.java index e7c6683a0d2d3827570c91fcf73e81502fd83bcb..ca32dce113b200614ef59a152980c7eb3fdd8b83 100644 --- a/service/src/test/java/org/sunbird/service/feed/impl/FeedServiceImplTest.java +++ b/service/src/test/java/org/sunbird/service/feed/impl/FeedServiceImplTest.java @@ -1,7 +1,5 @@ package org.sunbird.service.feed.impl; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -16,19 +14,25 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.sunbird.cassandraimpl.CassandraOperationImpl; import org.sunbird.common.Constants; +import org.sunbird.dao.feed.IFeedDao; +import org.sunbird.dao.feed.impl.FeedDaoImpl; import org.sunbird.helper.ServiceFactory; -import org.sunbird.http.HttpClientUtil; import org.sunbird.keys.JsonKey; import org.sunbird.model.user.Feed; -import org.sunbird.request.Request; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; import org.sunbird.service.feed.FeedFactory; import org.sunbird.service.feed.IFeedService; @RunWith(PowerMockRunner.class) -@PrepareForTest({ServiceFactory.class, HttpClientUtil.class}) +@PrepareForTest({ + ServiceFactory.class, + CassandraOperationImpl.class, + FeedDaoImpl.class, + IFeedDao.class +}) @PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*", @@ -40,7 +44,10 @@ public class FeedServiceImplTest { private static IFeedService feedService; @Before - public void setUp() throws JsonProcessingException { + public void setUp() { + PowerMockito.mockStatic(FeedDaoImpl.class); + IFeedDao iFeedDao = PowerMockito.mock(FeedDaoImpl.class); + PowerMockito.when(FeedDaoImpl.getInstance()).thenReturn(iFeedDao); Response upsertResponse = new Response(); Map<String, Object> responseMap2 = new HashMap<>(); responseMap2.put(Constants.RESPONSE, Constants.SUCCESS); @@ -49,32 +56,37 @@ public class FeedServiceImplTest { Map<String, Object> responseMap = new HashMap<>(); responseMap.put(Constants.RESPONSE, Arrays.asList(getFeedMap())); response.getResult().putAll(responseMap); - ObjectMapper Obj = new ObjectMapper(); - String jsonStr = Obj.writeValueAsString(upsertResponse); - PowerMockito.mockStatic(HttpClientUtil.class); - PowerMockito.when( - HttpClientUtil.post( - Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(jsonStr); - PowerMockito.when( - HttpClientUtil.patch( - Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(jsonStr); - PowerMockito.when(HttpClientUtil.get(Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(getUserFeedData()); + PowerMockito.when(iFeedDao.insert(Mockito.anyMap(), Mockito.any())).thenReturn(upsertResponse); + PowerMockito.when(iFeedDao.update(Mockito.anyMap(), Mockito.any())).thenReturn(upsertResponse); + PowerMockito.doNothing() + .when(iFeedDao) + .delete(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any()); + PowerMockito.when(iFeedDao.getFeedsByProperties(Mockito.anyMap(), Mockito.any())) + .thenReturn(response); feedService = FeedFactory.getInstance(); } @Test public void testInsert() { - Response res = feedService.insert(getFeed(false), new RequestContext()); + Response res = feedService.update(getFeed(true), null); Assert.assertTrue( ((String) res.getResult().get(JsonKey.RESPONSE)).equalsIgnoreCase(JsonKey.SUCCESS)); } + @Test + public void testDelete() { + boolean response = false; + try { + feedService.delete("123-456-789", null, null, null); + response = true; + } catch (Exception ex) { + Assert.assertTrue(response); + } + Assert.assertTrue(response); + } @Test public void testUpdate() { - Response res = feedService.update(getFeedUpdate(true), new RequestContext()); + Response res = feedService.update(getFeed(true), null); Assert.assertTrue( ((String) res.getResult().get(JsonKey.RESPONSE)).equalsIgnoreCase(JsonKey.SUCCESS)); } @@ -95,24 +107,7 @@ public class FeedServiceImplTest { return fMap; } - private Request getFeed(boolean needId) { - Request request = new Request(); - Feed feed = new Feed(); - feed.setUserId("123-456-7890"); - feed.setCategory("category"); - if (needId) { - feed.setId("123-456-789"); - } - Map<String, Object> map = new HashMap<>(); - List<String> channelList = new ArrayList<>(); - channelList.add("SI"); - map.put(JsonKey.PROSPECT_CHANNELS, channelList); - request.setRequest(new ObjectMapper().convertValue(feed, Map.class)); - return request; - } - - private Request getFeedUpdate(boolean needId) { - Request request = new Request(); + private Feed getFeed(boolean needId) { Feed feed = new Feed(); feed.setUserId("123-456-7890"); feed.setCategory("category"); @@ -124,26 +119,7 @@ public class FeedServiceImplTest { channelList.add("SI"); map.put(JsonKey.PROSPECT_CHANNELS, channelList); feed.setData(map); - request.setRequest(new ObjectMapper().convertValue(feed, Map.class)); - return request; + return feed; } - public String getUserFeedData() { - Response response = new Response(); - Map<String, Object> result = new HashMap<>(); - List<Map<String, Object>> feeds = new ArrayList<>(); - Map<String, Object> feed = new HashMap<>(); - feed.put(JsonKey.ID, "12312312"); - feeds.add(feed); - result.put(JsonKey.FEEDS, feeds); - response.putAll(result); - ObjectMapper Obj = new ObjectMapper(); - String jsonStr = null; - try { - jsonStr = Obj.writeValueAsString(response); - } catch (Exception e) { - Assert.assertFalse(false); - } - return jsonStr; - } }