Skip to content

Commit

Permalink
#21697 Fix to aviod issues with the system host on cloning content ty…
Browse files Browse the repository at this point in the history
…pe (#22089)

* #21697 fix to aviod issues with the system host on cloning content type

* #21697 adding unit test
  • Loading branch information
jdotcms authored Apr 26, 2022
1 parent 3679e05 commit 810ed86
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;

import com.dotcms.business.WrapInTransaction;
import com.dotcms.contenttype.business.CopyContentTypeBean;
import com.dotcms.contenttype.exception.NotFoundInDbException;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.contenttype.transform.contenttype.JsonContentTypeTransformer;
Expand All @@ -16,8 +17,10 @@
import com.dotcms.rest.api.v1.contenttype.ContentTypeResource;
import com.dotcms.util.ConfigTestHelper;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.liferay.portal.model.User;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
Expand All @@ -31,6 +34,7 @@
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.internal.util.Base64;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -67,6 +71,36 @@ public static Object[] testCases() throws URISyntaxException {
return testCases.toArray();
}

/**
* Method to test: {@link ContentTypeResource#setHostAndFolderAsIdentifer(String, String, User, CopyContentTypeBean.Builder)}
* Given Scenario: Sends the host and folder null
* ExpectedResult: null would be expected
*/
@Test
public void test_setHostAndFolderAsIdentifer_with_null() throws Exception {

final CopyContentTypeBean.Builder builder = new CopyContentTypeBean.Builder();
ContentTypeResource.setHostAndFolderAsIdentifer(null, null, APILocator.systemUser(), builder);
final CopyContentTypeBean copyContentTypeBean = builder.build();
Assert.assertEquals(null, copyContentTypeBean.getHost());
}

/**
* Method to test: {@link ContentTypeResource#setHostAndFolderAsIdentifer(String, String, User, CopyContentTypeBean.Builder)}
* Given Scenario: Sends the host as a SYSTEM_HOST
* ExpectedResult: The system host is expected as a result
*/
@Test
public void test_setHostAndFolderAsIdentifer_with_System_Host() throws Exception {

final String folderPathOrIdentifier = "";
final String hostOrId = "SYSTEM_HOST";
final CopyContentTypeBean.Builder builder = new CopyContentTypeBean.Builder();
ContentTypeResource.setHostAndFolderAsIdentifer(folderPathOrIdentifier, hostOrId, APILocator.systemUser(), builder);
final CopyContentTypeBean copyContentTypeBean = builder.build();
Assert.assertEquals(Host.SYSTEM_HOST, copyContentTypeBean.getHost());
}

@Test
@UseDataProvider("testCases")
@WrapInTransaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,22 @@ public final Response copyType(@Context final HttpServletRequest req,
return response;
}

private void setHostAndFolderAsIdentifer (final String folderPathOrIdentifier, final String hostOrId, final User user, final CopyContentTypeBean.Builder builder) {
@VisibleForTesting
public static void setHostAndFolderAsIdentifer (final String folderPathOrIdentifier, final String hostOrId, final User user, final CopyContentTypeBean.Builder builder) {

Host site = APILocator.systemHost();
if (null != hostOrId) {

site = Try.of(() -> UUIDUtil.isUUID(hostOrId) ? APILocator.getHostAPI().find(hostOrId, user, false) :
APILocator.getHostAPI().findByName(hostOrId, user, false)).getOrElse(APILocator.systemHost());
if (Host.SYSTEM_HOST.equals(hostOrId)) {

site = APILocator.systemHost();
} else {

site = Try.of(() -> UUIDUtil.isUUID(hostOrId) ? APILocator.getHostAPI().find(hostOrId, user, false) :
APILocator.getHostAPI().findByName(hostOrId, user, false)).getOrElse(APILocator.systemHost());
}

builder.host(site.getIdentifier());
builder.host(null == site? APILocator.systemHost().getIdentifier():site.getIdentifier());
}

if (null != folderPathOrIdentifier) {
Expand All @@ -200,7 +207,7 @@ private ImmutableMap<Object, Object> copyContentTypeAndDependencies (final Conte
.sourceContentType(type).icon(copyContentTypeForm.getIcon()).name(copyContentTypeForm.getName())
.newVariable(copyContentTypeForm.getVariable());

this.setHostAndFolderAsIdentifer(copyContentTypeForm.getFolder(), copyContentTypeForm.getHost(), user, builder);
setHostAndFolderAsIdentifer(copyContentTypeForm.getFolder(), copyContentTypeForm.getHost(), user, builder);
final ContentType contentTypeSaved = contentTypeAPI.copyFrom(builder.build());

// saving the workflow information
Expand Down

0 comments on commit 810ed86

Please sign in to comment.