diff --git a/dotCMS/src/integration-test/java/com/dotcms/contenttype/test/ContentTypeResourceTest.java b/dotCMS/src/integration-test/java/com/dotcms/contenttype/test/ContentTypeResourceTest.java index 3358ff4aa898..c751d8f714f5 100644 --- a/dotCMS/src/integration-test/java/com/dotcms/contenttype/test/ContentTypeResourceTest.java +++ b/dotCMS/src/integration-test/java/com/dotcms/contenttype/test/ContentTypeResourceTest.java @@ -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; @@ -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; @@ -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; @@ -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 diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java index be9ba8d75a76..6d5f9fa96afd 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java @@ -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) { @@ -200,7 +207,7 @@ private ImmutableMap 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