Unverified Commit e74ebc11 authored by AMIT KUMAR's avatar AMIT KUMAR Committed by GitHub
Browse files

Issue #SB-27871 feat:Added orgid validation (#1020)


* Issue #SB-27871 feat:Added orgid validation

* Issue #SB-27871 feat:Added orgid validation

Co-authored-by: default avatarReshmi V Nair <54312456+reshmi-nair@users.noreply.github.com>
Showing with 40 additions and 21 deletions
+40 -21
......@@ -19,6 +19,7 @@ import org.sunbird.request.Request;
import org.sunbird.request.RequestContext;
import org.sunbird.response.Response;
import org.sunbird.service.userconsent.UserConsentService;
import org.sunbird.util.DataCacheHandler;
import org.sunbird.util.ProjectUtil;
import org.sunbird.util.user.DateUtil;
......@@ -111,20 +112,13 @@ public class UserConsentServiceImpl implements UserConsentService {
}
public void validateConsumerId(String consumerId, RequestContext context) {
Map<String, Object> org = null;
try {
org = orgDao.getOrgById(consumerId, context);
} catch (Exception ex) {
throw new ProjectCommonException(
ResponseCode.internalError.getErrorCode(),
ResponseCode.internalError.getErrorMessage(),
ResponseCode.SERVER_ERROR.getResponseCode());
String custodianOrgId = DataCacheHandler.getConfigSettings().get(JsonKey.CUSTODIAN_ORG_ID);
if (consumerId.equalsIgnoreCase(custodianOrgId)) {
ProjectCommonException.throwClientErrorException(ResponseCode.invalidOrgId);
}
Map<String, Object> org = orgDao.getOrgById(consumerId, context);
if (MapUtils.isEmpty(org)) {
throw new ProjectCommonException(
ResponseCode.invalidOrgId.getErrorCode(),
ResponseCode.invalidOrgId.getErrorMessage(),
ResponseCode.RESOURCE_NOT_FOUND.getResponseCode());
ProjectCommonException.throwClientErrorException(ResponseCode.invalidOrgId);
}
}
......
package org.sunbird.service.userconsent.impl;
import static org.junit.Assert.*;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
......@@ -15,17 +17,21 @@ 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.cassandra.CassandraOperation;
import org.sunbird.cassandraimpl.CassandraOperationImpl;
import org.sunbird.common.Constants;
import org.sunbird.dao.organisation.OrgDao;
import org.sunbird.dao.organisation.impl.OrgDaoImpl;
import org.sunbird.dao.userconsent.UserConsentDao;
import org.sunbird.dao.userconsent.impl.UserConsentDaoImpl;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.helper.ServiceFactory;
import org.sunbird.keys.JsonKey;
import org.sunbird.request.Request;
import org.sunbird.request.RequestContext;
import org.sunbird.response.Response;
import org.sunbird.service.userconsent.UserConsentService;
import org.sunbird.util.DataCacheHandler;
@RunWith(PowerMockRunner.class)
@PrepareForTest({
......@@ -34,7 +40,8 @@ import org.sunbird.service.userconsent.UserConsentService;
UserConsentDaoImpl.class,
UserConsentDao.class,
OrgDaoImpl.class,
OrgDao.class
OrgDao.class,
DataCacheHandler.class
})
@PowerMockIgnore({
"javax.management.*",
......@@ -46,15 +53,26 @@ import org.sunbird.service.userconsent.UserConsentService;
public class UserConsentServiceImplTest {
private static UserConsentService userConsentService;
private static CassandraOperation cassandraOperationImpl = null;
@Before
public void setUp() {
PowerMockito.mockStatic(ServiceFactory.class);
cassandraOperationImpl = mock(CassandraOperationImpl.class);
when(ServiceFactory.getInstance()).thenReturn(cassandraOperationImpl);
Response response = new Response();
List<Map<String, Object>> orgList = new ArrayList<>();
response.getResult().put(JsonKey.RESPONSE, orgList);
when(cassandraOperationImpl.getRecordById(
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any()))
.thenReturn(response);
PowerMockito.mockStatic(DataCacheHandler.class);
Map<String, String> config = new HashMap<>();
config.put(JsonKey.CUSTODIAN_ORG_ID, "custodianOrgId");
PowerMockito.when(DataCacheHandler.getConfigSettings()).thenReturn(config);
PowerMockito.mockStatic(UserConsentDaoImpl.class);
PowerMockito.mockStatic(OrgDaoImpl.class);
UserConsentDao userConsentDao = PowerMockito.mock(UserConsentDaoImpl.class);
PowerMockito.when(UserConsentDaoImpl.getInstance()).thenReturn(userConsentDao);
OrgDao orgDao = PowerMockito.mock(OrgDaoImpl.class);
PowerMockito.when(OrgDaoImpl.getInstance()).thenReturn(orgDao);
Response upsertResponse = new Response();
Map<String, Object> responseMap2 = new HashMap<>();
responseMap2.put(Constants.RESPONSE, Constants.SUCCESS);
......@@ -69,22 +87,29 @@ public class UserConsentServiceImplTest {
responseMap.put(JsonKey.CONSENT_OBJECTID, "do_31313966505806233613406");
responseMap.put(JsonKey.CONSENT_OBJECTTYPE, "Collection");
responseMap.put(JsonKey.STATUS, "ACTIVE");
Map<String, Object> orgMap = new HashMap<>();
orgMap.put(JsonKey.ID, "0130107621805015045");
PowerMockito.when(userConsentDao.getConsent(Mockito.anyMap(), Mockito.any()))
.thenReturn(Arrays.asList(responseMap));
PowerMockito.when(userConsentDao.updateConsent(Mockito.anyMap(), Mockito.any()))
.thenReturn(upsertResponse);
PowerMockito.when(orgDao.getOrgById(Mockito.anyString(), Mockito.any())).thenReturn(orgMap);
userConsentService = UserConsentServiceImpl.getInstance();
}
@Test
public void testUpdateConsent() {
Response response = userConsentService.updateConsent(consentUpdateMap(), null);
Response response = userConsentService.updateConsent(consentUpdateMap(), new RequestContext());
Assert.assertNotNull(response);
}
@Test(expected = ProjectCommonException.class)
public void testValidateConsumerId() {
userConsentService.validateConsumerId("custodianOrgId", new RequestContext());
}
@Test(expected = ProjectCommonException.class)
public void testValidateConsumerIdV2() {
userConsentService.validateConsumerId("OrgId", new RequestContext());
}
@Test
public void testGetConsent() {
List<Map<String, Object>> consentResponse = userConsentService.getConsent(getConsentRequest());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment