Skip to content

Commit

Permalink
LPD-34383 - fixing patch methods, adding tests for request without name
Browse files Browse the repository at this point in the history
  • Loading branch information
matyaswollner authored and brianchandotcom committed Oct 1, 2024
1 parent e6b7e28 commit 5b498e5
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public AccountGroup toDTO(
accountGroup.getAccountGroupId(),
accountGroup.getCompanyId(),
dtoConverterContext.getLocale()));
setDescription(accountGroup::getDescription);
setExternalReferenceCode(
accountGroup::getExternalReferenceCode);
setId(accountGroup::getAccountGroupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.liferay.headless.admin.user.internal.resource.v1_0;

import com.liferay.account.constants.AccountActionKeys;
import com.liferay.account.exception.NoSuchGroupException;
import com.liferay.account.model.AccountEntry;
import com.liferay.account.model.AccountGroupRel;
import com.liferay.account.service.AccountGroupRelService;
Expand Down Expand Up @@ -175,6 +176,33 @@ public EntityModel getEntityModel(MultivaluedMap multivaluedMap)
return _entityModel;
}

@Override
public AccountGroup patchAccountGroup(
Long accountGroupId, AccountGroup accountGroup)
throws Exception {

com.liferay.account.model.AccountGroup serviceBuilderAccountGroup =
_accountGroupService.getAccountGroup(accountGroupId);

return _updateAccountGroup(accountGroup, serviceBuilderAccountGroup);
}

@Override
public AccountGroup patchAccountGroupByExternalReferenceCode(
String externalReferenceCode, AccountGroup accountGroup)
throws Exception {

com.liferay.account.model.AccountGroup serviceBuilderAccountGroup =
_accountGroupService.fetchAccountGroupByExternalReferenceCode(
externalReferenceCode, contextCompany.getCompanyId());

if (serviceBuilderAccountGroup == null) {
throw new NoSuchGroupException();
}

return _updateAccountGroup(accountGroup, serviceBuilderAccountGroup);
}

@Override
public AccountGroup postAccountGroup(AccountGroup accountGroup)
throws Exception {
Expand Down Expand Up @@ -361,6 +389,26 @@ private AccountGroup _toAccountGroup(
_getDTOConverterContext(accountGroup.getAccountGroupId()));
}

private AccountGroup _updateAccountGroup(
AccountGroup accountGroup,
com.liferay.account.model.AccountGroup serviceBuilderAccountGroup)
throws Exception {

serviceBuilderAccountGroup = _accountGroupService.updateAccountGroup(
serviceBuilderAccountGroup.getAccountGroupId(),
GetterUtil.getString(
accountGroup.getDescription(),
serviceBuilderAccountGroup.getDescription()),
GetterUtil.getString(
accountGroup.getName(), serviceBuilderAccountGroup.getName()),
_createServiceContext(accountGroup));

return _toAccountGroup(
_accountGroupService.updateExternalReferenceCode(
serviceBuilderAccountGroup.getAccountGroupId(),
accountGroup.getExternalReferenceCode()));
}

@Reference(
target = "(model.class.name=com.liferay.account.model.AccountGroup)"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import com.liferay.account.service.AccountGroupRelLocalServiceUtil;
import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.headless.admin.user.client.dto.v1_0.AccountGroup;
import com.liferay.headless.admin.user.client.problem.Problem;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.test.rule.DataGuard;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.test.rule.Inject;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -26,6 +29,7 @@
/**
* @author Javier Gamarra
*/
@DataGuard(scope = DataGuard.Scope.METHOD)
@RunWith(Arquillian.class)
public class AccountGroupResourceTest extends BaseAccountGroupResourceTestCase {

Expand Down Expand Up @@ -77,13 +81,52 @@ public void testGraphQLGetAccountGroupNotFound() throws Exception {
public void testGraphQLGetAccountGroupsPage() throws Exception {
}

@Override
@Test
public void testPatchAccountGroup() throws Exception {
super.testPatchAccountGroup();

_testPatchAccountGroupWithoutName();
}

@Override
@Test
public void testPatchAccountGroupByExternalReferenceCode()
throws Exception {

super.testPatchAccountGroupByExternalReferenceCode();

_testPatchAccountGroupByExternalReferenceCodeWithoutName();
}

@Ignore
@Override
@Test
public void testPostAccountGroupByExternalReferenceCodeAccountByExternalReferenceCode()
throws Exception {
}

@Override
@Test
public void testPutAccountGroup() throws Exception {
super.testPutAccountGroup();

_testPutAccountGroupWithoutName();
}

@Override
@Test
public void testPutAccountGroupByExternalReferenceCode() throws Exception {
super.testPutAccountGroupByExternalReferenceCode();

_testPutAccountGroupByExternalReferenceWithoutName();
}

@Override
protected String[] getAdditionalAssertFieldNames() {
return new String[] {"description", "externalReferenceCode", "name"};
}

@Override
protected AccountGroup testDeleteAccountGroup_addAccountGroup()
throws Exception {
Expand Down Expand Up @@ -241,6 +284,103 @@ private AccountGroup _postAccountGroup(AccountGroup accountGroup)
return accountGroupResource.postAccountGroup(accountGroup);
}

private void _testPatchAccountGroupByExternalReferenceCodeWithoutName()
throws Exception {

AccountGroup postAccountGroup = _postAccountGroup(randomAccountGroup());

AccountGroup randomPatchAccountGroup = randomPatchAccountGroup();

randomPatchAccountGroup.setName(() -> null);

AccountGroup patchAccountGroup =
accountGroupResource.patchAccountGroupByExternalReferenceCode(
postAccountGroup.getExternalReferenceCode(),
randomPatchAccountGroup);

AccountGroup expectedPatchAccountGroup = postAccountGroup.clone();

BeanTestUtil.copyProperties(
randomPatchAccountGroup, expectedPatchAccountGroup);

expectedPatchAccountGroup.setName(postAccountGroup.getName());

AccountGroup getAccountGroup = accountGroupResource.getAccountGroup(
patchAccountGroup.getId());

assertEquals(expectedPatchAccountGroup, getAccountGroup);
assertValid(getAccountGroup);
}

private void _testPatchAccountGroupWithoutName() throws Exception {
AccountGroup postAccountGroup = _postAccountGroup(randomAccountGroup());

AccountGroup randomPatchAccountGroup = randomPatchAccountGroup();

randomPatchAccountGroup.setName(() -> null);

AccountGroup patchAccountGroup = accountGroupResource.patchAccountGroup(
postAccountGroup.getId(), randomPatchAccountGroup);

AccountGroup expectedPatchAccountGroup = postAccountGroup.clone();

BeanTestUtil.copyProperties(
randomPatchAccountGroup, expectedPatchAccountGroup);

expectedPatchAccountGroup.setName(postAccountGroup.getName());

AccountGroup getAccountGroup = accountGroupResource.getAccountGroup(
patchAccountGroup.getId());

assertEquals(expectedPatchAccountGroup, getAccountGroup);
assertValid(getAccountGroup);
}

private void _testPutAccountGroupByExternalReferenceWithoutName()
throws Exception {

AccountGroup postAccountGroup = _postAccountGroup(randomAccountGroup());

AccountGroup randomAccountGroup = randomAccountGroup();

randomAccountGroup.setName(() -> null);

try {
accountGroupResource.putAccountGroupByExternalReferenceCode(
postAccountGroup.getExternalReferenceCode(),
randomAccountGroup);

Assert.fail();
}
catch (Problem.ProblemException problemException) {
Problem problem = problemException.getProblem();

Assert.assertEquals(
"The account group name is invalid", problem.getTitle());
}
}

private void _testPutAccountGroupWithoutName() throws Exception {
AccountGroup postAccountGroup = _postAccountGroup(randomAccountGroup());

AccountGroup randomAccountGroup = randomAccountGroup();

randomAccountGroup.setName(() -> null);

try {
accountGroupResource.putAccountGroup(
postAccountGroup.getId(), randomAccountGroup);

Assert.fail();
}
catch (Problem.ProblemException problemException) {
Problem problem = problemException.getProblem();

Assert.assertEquals(
"The account group name is invalid", problem.getTitle());
}
}

private AccountEntry _accountEntry;

@Inject
Expand Down

0 comments on commit 5b498e5

Please sign in to comment.