Commit 7c9ec917 authored by Rucha Deshpande's avatar Rucha Deshpande
Browse files

Merge branch 'feat/aws-int-test-fixes' into 'master'

Feat/aws int test fixes

See merge request !11
parents 3cf75fd2 03b396a7
Pipeline #18022 passed with stages
in 8 minutes and 12 seconds
......@@ -61,7 +61,7 @@
<dependency>
<groupId>org.opengroup.osdu.core.aws</groupId>
<artifactId>os-core-lib-aws</artifactId>
<version>0.3.7</version>
<version>0.3.11</version>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
......
......@@ -13,7 +13,9 @@
// limitations under the License.
package org.opengroup.osdu.aws.entitlements.service;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.aws.entitlements.dto.CreateGroupRequest;
import org.opengroup.osdu.aws.entitlements.model.GroupInfoDoc;
......@@ -71,17 +73,17 @@ public class EntitlementsAwsService {
int httpStatusCode = HttpStatusCodes.UNASSIGNED;
private static final String ERROR_REASON_SERVER_ERROR = "Internal Server Error";
private static final String ERROR_MSG_GET_GROUPS = "Could not fetch groups";
private static final String ERROR_MSG_CREATE_GROUP = "Could not create groups";
private static final String ERROR_REASON_UNAUTHORIZED = "The user is not authorized to perform this function";
private static final String ERROR_BAD_REQUEST = "bad request";
private static final String ERROR_ACCESS_DENIED = "Access denied";
private static final String DDB_HASH_KEY = "groupUniqueIdentifier";
String error_msg="";
String memberEmail=null;
enum ROLES {OWNER, MEMBER,EDITOR, CREATOR};
/*
Method to get groups
......@@ -144,17 +146,15 @@ public class EntitlementsAwsService {
groupsDoc.setDataPartitionId(dataPartitionId);
groupsDoc.setMemberEmailAndDataPartition(String.format("%s:%s", memberEmail, dataPartitionId));
try {
dynamoDBQueryHelper.save(groupsDoc);
}catch(Exception e)
{
httpStatusCode = HttpStatusCodes.INTERNAL_SERVER_ERROR;
error_msg = ERROR_REASON_SERVER_ERROR;
throw new AppException(httpStatusCode, error_msg, ERROR_MSG_CREATE_GROUP);
dynamoDBQueryHelper.saveWithHashCondition(groupsDoc, DDB_HASH_KEY);
}
catch(ConditionalCheckFailedException e){
logger.error(String.format("Conflict. Failed to insert group. Entity already exists: %s", groupsDoc.getGroupUniqueIdentifier()));
httpStatusCode = HttpStatus.SC_CONFLICT;
error_msg = "Failed to insert group. Entity already exists.";
throw new AppException(httpStatusCode, error_msg, error_msg);
}
groupInfo.setName(groupName);
......@@ -166,7 +166,7 @@ public class EntitlementsAwsService {
catch(Exception e)
{
logger.error("Error in createGroup"+e.getMessage());
throw new AppException(httpStatusCode, error_msg, ERROR_MSG_CREATE_GROUP);
throw new AppException(httpStatusCode, error_msg, error_msg);
}
......@@ -182,6 +182,19 @@ public class EntitlementsAwsService {
Members members = new Members();
//check first if group exists
GroupsDoc groupsDocKey = new GroupsDoc();
groupsDocKey.setGroupEmail(groupEmail.getGroupEmail());
PaginatedQueryList<GroupsDoc> groupsDocPages= null;
groupsDocPages = dynamoDBQueryHelper.queryByGSI(GroupsDoc.class, groupsDocKey);
if (!(groupsDocPages.size() > 0)) {
httpStatusCode = HttpStatus.SC_NOT_FOUND;
error_msg="Group not found.";
throw new AppException(httpStatusCode, error_msg,error_msg);
}
......@@ -210,8 +223,7 @@ public class EntitlementsAwsService {
}
GroupsDoc groupsDocKey = new GroupsDoc();
groupsDocKey.setGroupEmail(groupEmail.getGroupEmail());
try {
List<MemberInfo> memberInfos = helper.getMemberInfos(dynamoDBQueryHelper, groupsDocKey, "role", role, limit, cursor);
......@@ -253,6 +265,21 @@ public class EntitlementsAwsService {
String memInfo_email = memInfo.getEmail();
String memInfo_role = memInfo.getRole();
if(memInfo_email == null )
{
httpStatusCode = HttpStatusCodes.BAD_REQUEST;
error_msg = "Invalid email provided";
throw new AppException(httpStatusCode, error_msg, error_msg);
}
else if(memInfo_role==null || isValidRole(memInfo_role) ==false)
{
httpStatusCode = HttpStatusCodes.BAD_REQUEST;
error_msg = "Invalid role provided.";
throw new AppException(httpStatusCode, error_msg, error_msg);
}
String groupEmail= groupEmailObj.getGroupEmail();
GroupInfoDoc groupInfoDoc = parseGroupEmail(groupEmail);
String dataPartitionId = groupInfoDoc.getTenant();
......@@ -350,17 +377,26 @@ public class EntitlementsAwsService {
error_msg=ERROR_REASON_UNAUTHORIZED;
throw new AppException(httpStatusCode, error_msg, ERROR_REASON_UNAUTHORIZED);
}
String primaryKey = String.format("%s:%s:%s", dataPartitionId, groupEmail, groupMember);
try {
dynamoDBQueryHelper.deleteByPrimaryKey(GroupsDoc.class, primaryKey);
}catch(Exception ex)
{
httpStatusCode = HttpStatusCodes.INTERNAL_SERVER_ERROR;
error_msg="Error in removeMember. Internal Server error";
}
try {
GroupsDoc groupsDoc = new GroupsDoc();
groupsDoc.setGroupUniqueIdentifier(primaryKey);
DynamoDBDeleteExpression deleteExpression = new DynamoDBDeleteExpression()
.withConditionExpression("attribute_exists(groupUniqueIdentifier)");
dynamoDBQueryHelper.deleteByObjectWithCondition(groupsDoc, deleteExpression);
}
catch(ConditionalCheckFailedException e){
logger.error(String.format("Entitlements: Failed to delete: %s Object does not exist",primaryKey));
httpStatusCode = HttpStatus.SC_NOT_FOUND;
error_msg="Not Found";
throw new AppException(httpStatusCode, error_msg, error_msg);
}
} catch (Exception e) {
logger.error("Error in deleteMember" + e.getMessage());
error_msg = "Error in deleteMember";
throw new AppException(httpStatusCode, error_msg, error_msg);
}
}
......@@ -413,5 +449,16 @@ public class EntitlementsAwsService {
}
}
public static boolean isValidRole(String role) {
for (ROLES c : ROLES.values()) {
if (c.name().equals(role)) {
return true;
}
}
return false;
}
}
......@@ -15,6 +15,7 @@
package org.opengroup.osdu.aws.entitlements;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -117,6 +118,9 @@ public class GetMembersTest {
GroupsDoc groupsDocKey = new GroupsDoc();
groupsDocKey.setGroupEmail(groupEmailObj.getGroupEmail());
Mockito.when(helper.getMemberInfos(Mockito.any(DynamoDBQueryHelper.class),Mockito.any(GroupsDoc.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyInt(),Mockito.anyString())).thenReturn(memberInfos);
PaginatedQueryList<GroupsDoc> groupsDocPages = Mockito.mock(PaginatedQueryList.class);
Mockito.when(dynamoDBQueryHelper.queryByGSI(GroupsDoc.class, groupsDocKey)).thenReturn(groupsDocPages);
Mockito.when(groupsDocPages.size()).thenReturn(1);
Members members = sut.getMembers(dynamoDBQueryHelper,groupEmailObj, TestUtils.getLim(), TestUtils.getRoleOwner(), cursor);
......@@ -149,6 +153,9 @@ public class GetMembersTest {
Mockito.when(helper.isUserMemberOfGroup(Mockito.any(DynamoDBQueryHelper.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(true);
GroupsDoc groupsDocKey = new GroupsDoc();
groupsDocKey.setGroupEmail(groupEmailObj.getGroupEmail());
PaginatedQueryList<GroupsDoc> groupsDocPages = Mockito.mock(PaginatedQueryList.class);
Mockito.when(dynamoDBQueryHelper.queryByGSI(GroupsDoc.class, groupsDocKey)).thenReturn(groupsDocPages);
Mockito.when(groupsDocPages.size()).thenReturn(1);
Mockito.when(helper.getMemberInfos(Mockito.any(DynamoDBQueryHelper.class),Mockito.any(GroupsDoc.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyInt(),Mockito.anyString())).thenReturn(memberInfos);
Members members = sut.getMembers(dynamoDBQueryHelper,groupEmailObj, TestUtils.getLim(), TestUtils.getRoleMember(), cursor);
......@@ -159,7 +166,7 @@ public class GetMembersTest {
}
@Test
@Test
public void getMembersTest_userNotMemberOfGroup() throws UnsupportedEncodingException {
List<MemberInfo> memberInfos = new ArrayList<>();
MemberInfo member = new MemberInfo();
......@@ -180,6 +187,9 @@ public class GetMembersTest {
Mockito.when(helper.isUserMemberOfGroup(Mockito.any(DynamoDBQueryHelper.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(false);
GroupsDoc groupsDocKey = new GroupsDoc();
groupsDocKey.setGroupEmail(groupEmailObj.getGroupEmail());
PaginatedQueryList<GroupsDoc> groupsDocPages = Mockito.mock(PaginatedQueryList.class);
Mockito.when(dynamoDBQueryHelper.queryByGSI(GroupsDoc.class, groupsDocKey)).thenReturn(groupsDocPages);
Mockito.when(groupsDocPages.size()).thenReturn(1);
Mockito.when(helper.getMemberInfos(Mockito.any(DynamoDBQueryHelper.class),Mockito.any(GroupsDoc.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyInt(),Mockito.anyString())).thenReturn(memberInfos);
......@@ -214,6 +224,9 @@ public class GetMembersTest {
Mockito.when(helper.isUserMemberOfGroup(Mockito.any(DynamoDBQueryHelper.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(true);
GroupsDoc groupsDocKey = new GroupsDoc();
groupsDocKey.setGroupEmail(groupEmailObj.getGroupEmail());
PaginatedQueryList<GroupsDoc> groupsDocPages = Mockito.mock(PaginatedQueryList.class);
Mockito.when(dynamoDBQueryHelper.queryByGSI(GroupsDoc.class, groupsDocKey)).thenReturn(groupsDocPages);
Mockito.when(groupsDocPages.size()).thenReturn(1);
Mockito.when(helper.getMemberInfos(Mockito.any(DynamoDBQueryHelper.class),Mockito.any(GroupsDoc.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyInt(),Mockito.anyString())).thenThrow(e);
......
......@@ -14,7 +14,9 @@
package org.opengroup.osdu.aws.entitlements;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -34,6 +36,7 @@ import org.opengroup.osdu.core.common.model.entitlements.MemberInfo;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.util.ReflectionTestUtils;
import utils.TestUtils;
......@@ -96,13 +99,13 @@ public class RemoveMemberTest {
dynamoDBQueryHelper = Mockito.mock(DynamoDBQueryHelper.class);
Mockito.when(helper.isUserOwnerOfGroup(Mockito.any(DynamoDBQueryHelper.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(true);
Mockito.doNothing().when(dynamoDBQueryHelper).deleteByPrimaryKey(Mockito.anyObject(), Mockito.eq(String.format("%s:%s:%s", TestUtils.getTenantName(),TestUtils.getGroupEmail(), TestUtils.getMember_to_delete())));
Mockito.doNothing().when(dynamoDBQueryHelper).deleteByObjectWithCondition(Mockito.any(GroupsDoc.class),Mockito.any(DynamoDBDeleteExpression.class));
sut.deleteMember(dynamoDBQueryHelper,groupEmailObj,TestUtils.getMember_to_delete());
// assert
Mockito.verify(dynamoDBQueryHelper, Mockito.times(1))
.deleteByPrimaryKey(Mockito.anyObject(), Mockito.eq(String.format("%s:%s:%s", TestUtils.getTenantName(),TestUtils.getGroupEmail(), TestUtils.getMember_to_delete())));
.deleteByObjectWithCondition(Mockito.anyObject(), Mockito.anyObject());
}
@Test
......@@ -117,8 +120,7 @@ public class RemoveMemberTest {
dynamoDBQueryHelper = Mockito.mock(DynamoDBQueryHelper.class);
Mockito.when(helper.isUserOwnerOfGroup(Mockito.any(DynamoDBQueryHelper.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(false);
Mockito.doNothing().when(dynamoDBQueryHelper).deleteByPrimaryKey(Mockito.anyObject(), Mockito.eq(String.format("%s:%s:%s", TestUtils.getTenantName(),TestUtils.getGroupEmail(), TestUtils.getMember_to_delete())));
Mockito.doNothing().when(dynamoDBQueryHelper).deleteByObjectWithCondition(Mockito.any(GroupsDoc.class),Mockito.any(DynamoDBDeleteExpression.class));
try {
sut.deleteMember(dynamoDBQueryHelper,groupEmailObj,TestUtils.getMember_to_delete());
......@@ -132,7 +134,7 @@ public class RemoveMemberTest {
@Test
public void removeMemberTest_failure() {
AppException e = new AppException(500, "Error", "Error is removeMember");
ConditionalCheckFailedException e = new ConditionalCheckFailedException("Not Found");
GroupEmail groupEmailObj = new GroupEmail(TestUtils.getGroupEmail());
Map<String,String> headers = new HashMap<String,String>();
......@@ -142,14 +144,15 @@ public class RemoveMemberTest {
dynamoDBQueryHelper = Mockito.mock(DynamoDBQueryHelper.class);
Mockito.when(helper.isUserOwnerOfGroup(Mockito.any(DynamoDBQueryHelper.class),Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(true);
String primaryKey = String.format("%s:%s:%s", TestUtils.getTenantName(),TestUtils.getGroupEmail(), TestUtils.getMember_to_delete());
Mockito.doThrow(e).when(dynamoDBQueryHelper).deleteByPrimaryKey(Mockito.anyObject(), Mockito.eq(String.format("%s:%s:%s", TestUtils.getTenantName(),TestUtils.getGroupEmail(), TestUtils.getMember_to_delete())));
Mockito.doThrow(e).when(dynamoDBQueryHelper).deleteByObjectWithCondition(Mockito.any(GroupsDoc.class),Mockito.any(DynamoDBDeleteExpression.class));
try {
sut.deleteMember(dynamoDBQueryHelper,groupEmailObj,TestUtils.getMember_to_delete());
} catch (AppException ae)
{
assertEquals(HttpStatusCodes.INTERNAL_SERVER_ERROR, ae.getError().getCode());
assertEquals(404, ae.getError().getCode());
}
......
......@@ -43,7 +43,7 @@
<dependency>
<groupId>org.opengroup.osdu.core.aws</groupId>
<artifactId>os-core-lib-aws</artifactId>
<version>0.3.7</version>
<version>0.3.11</version>
</dependency>
......
......@@ -24,6 +24,7 @@ import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.opengroup.osdu.entitlements.utils.*;
import org.opengroup.osdu.core.aws.entitlements.MemberInfo;
import java.sql.Timestamp;
import java.util.*;
import static org.junit.Assert.assertEquals;
......@@ -99,8 +100,10 @@ public class EntitlementsApiTests {
String path = "groups";
String token = client.getToken(AwsConfig.getAWSCognitoTestUsername(),AwsConfig.getAWSCognitoTestUserPassword(),"bearer");
String groupName= "int-test-group-12345";
String groupDescription = "int-test-group-12345 description";
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String ts= Long.toString(timestamp.getTime());
String groupName= "int-test-group"+ts;
String groupDescription = "int-test-group description";
String groupEmail = groupName + "@" + AwsConfig.getTenant() + "." + AwsConfig.getDomain();
Map<String, String> group = new HashMap();
......@@ -120,6 +123,32 @@ public class EntitlementsApiTests {
}
@Test
public void createGroup_throw409IfGroupAlreadyExists() throws Exception {
String path = "groups";
String token = client.getToken(AwsConfig.getAWSCognitoTestUsername(),AwsConfig.getAWSCognitoTestUserPassword(),"bearer");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String ts= Long.toString(timestamp.getTime());
String groupName= "int-test-group-dup"+ts;
String groupDescription = "int-test-group-dup description";
String groupEmail = groupName + "@" + AwsConfig.getTenant() + "." + AwsConfig.getDomain();
Map<String, String> group = new HashMap();
group.put("name",groupName);
group.put("description", groupDescription);
String newGroupRequestBody = new Gson().toJson(group);
//Create the group first
ClientResponse response = HTTPUtil.send(path, "POST",HTTPUtil.getHeaders(AwsConfig.getTenant(), token), newGroupRequestBody, "");
//attempt to create the group again
ClientResponse responsedup = HTTPUtil.send(path, "POST",HTTPUtil.getHeaders(AwsConfig.getTenant(), token), newGroupRequestBody, "");
assertEquals(HttpStatus.SC_CONFLICT, responsedup.getStatus());
}
@Test
public void createGroup_noToken_Forbiddden() throws Exception {
......
Markdown is supported
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