Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commercetools/support additional types for contentful #27

Merged
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ script:
- "sbt test it:test unidoc"
- if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then sbt
"gitPublish target/javaunidoc https://$GH_TOKEN:[email protected]/$TRAVIS_REPO_SLUG.git
javadoc sphere-oss [email protected]"; fi\
javadoc sphere-oss [email protected]"; fi

jdk:
- oraclejdk8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.commercetools.sunrise.cms.CmsPage;
import com.commercetools.sunrise.cms.CmsServiceException;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
Expand All @@ -14,19 +12,20 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.ThrowableAssert.catchThrowable;

public class ContentfulCmsServiceIT {
private static final List<Locale> SUPPORTED_LOCALES = Collections.singletonList(Locale.GERMANY);
private static final List<Locale> SUPPORTED_LOCALES = singletonList(Locale.GERMANY);

// credentials for contentful demo account
private static final String IT_PREFIX = "CONTENTFUL_";
private static final String IT_CF_SPACE_ID = IT_PREFIX + "SPACE_ID";
private static final String IT_CF_TOKEN = IT_PREFIX + "TOKEN";
private static final String PAGE_TYPE_NAME = "page";
private static final String PAGE_TYPE_ID_FIELD_NAME = "slug";
private ContentfulCmsService contentfulCmsService;

private static String spaceId() {
return getValueForEnvVar(IT_CF_SPACE_ID);
Expand All @@ -36,11 +35,6 @@ private static String token() {
return getValueForEnvVar(IT_CF_TOKEN);
}

@Before
public void setUp() throws Exception {
contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);
}

@Test
public void whenCouldNotFetchEntry_thenThrowException() throws Exception {
final ContentfulCmsService cmsService = ContentfulCmsService.of("", "", PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);
Expand All @@ -52,63 +46,214 @@ public void whenCouldNotFetchEntry_thenThrowException() throws Exception {
}

@Test
public void whenAskForExistingStringContentThenGet() throws Exception {
public void whenAskForExistingStringContent_thenGet() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", SUPPORTED_LOCALES));
assertThat(content).isPresent();

assertThat(content.get().field("pageContent.description")).contains("Fearless Abenteurer! Verteidiger von Pfannkuchen.");
assertThat(content).isPresent();
Optional<String> field = content.get().field("pageContent.description");
assertThat(field).hasValue("Fearless Abenteurer! Verteidiger von Pfannkuchen.");
}

@Test
public void whenAskForExistingStringContentAndLocalesAreEmptyThenGetDefaultLocaleContent() throws Exception {
Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", Collections.emptyList()));
assertThat(content).isPresent();
public void whenAskForExistingStringContentAndLocalesAreEmpty_thenGetDefaultLocaleContent() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

assertThat(content.get().field("pageContent.description")).contains("Fearless Abenteurer! Verteidiger von Pfannkuchen.");
Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", emptyList()));

assertThat(content).isPresent();
Optional<String> field = content.get().field("pageContent.description");
assertThat(field).hasValue("Fearless Abenteurer! Verteidiger von Pfannkuchen.");
}

@Test
public void whenAskForExistingStringContentAndLocalesAreNullThenGetDefaultLocaleContent() throws Exception {
public void whenAskForExistingStringContentAndLocalesAreNull_thenGetDefaultLocaleContent() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", null));
assertThat(content).isPresent();

assertThat(content.get().field("pageContent.description")).contains("Fearless Abenteurer! Verteidiger von Pfannkuchen.");
assertThat(content).isPresent();
Optional<String> field = content.get().field("pageContent.description");
assertThat(field).hasValue("Fearless Abenteurer! Verteidiger von Pfannkuchen.");
}

@Test
public void whenAskForExistingStringContentWithNotDefaultLocaleThenGet() throws Exception {
Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", Collections.singletonList(Locale.ENGLISH)));
assertThat(content).isPresent();
public void whenAskForExistingStringContentWithNotDefaultLocale_thenGetDefaultLocaleContent() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", singletonList(Locale.ENGLISH)));

assertThat(content.get().field("pageContent.description")).contains("Fearless adventurer! Defender of pancakes.");
assertThat(content).isPresent();
Optional<String> field = content.get().field("pageContent.description");
assertThat(field).hasValue("Fearless adventurer! Defender of pancakes.");
}

@Test
public void whenAskForExistingStringContentWithNotDefinedLocaleThenNotPresent() throws Exception {
Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", Collections.singletonList(Locale.ITALIAN)));
assertThat(content).isEmpty();
public void whenAskForExistingStringContentWithNotDefinedLocale_thenReturnEmpty() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", singletonList(Locale.ITALIAN)));

assertThat(content).isNotPresent();
}

@Test
public void whenAskForNotExistingStringContentThenNotPresent() throws Exception {
public void whenAskForNotExistingStringContent_thenReturnEmpty() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("finn", SUPPORTED_LOCALES));

assertThat(content).isPresent();
assertThat(content.get().field("pageContent.notExistingField")).isEmpty();
Optional<String> field = content.get().field("pageContent.notExistingField");
assertThat(field).isNotPresent();
}

@Test
public void whenAskForExistingAssetContentThenGet() throws Exception {
public void whenAskForExistingAssetContent_thenGet() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("jacke", SUPPORTED_LOCALES));

assertThat(content).isPresent();
assertThat(content.get().fieldOrEmpty("pageContent.image")).isEqualToIgnoringCase("//images.contentful.com/l6chdlzlf8jn/2iVeCh1FGoy00Oq8WEI2aI/93c3f0841fcf59743f57e238f6ed67aa/jake.png");
String actual = content.get().fieldOrEmpty("pageContent.image");
assertThat(actual).isEqualToIgnoringCase("//images.contentful.com/l6chdlzlf8jn/2iVeCh1FGoy00Oq8WEI2aI/93c3f0841fcf59743f57e238f6ed67aa/jake.png");
}

@Test
public void whenAskForNotExistingAssetContentThenNotPresent() throws Exception {
public void whenAskForNotExistingAssetContent_thenReturnEmpty() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), PAGE_TYPE_NAME, PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = waitAndGet(contentfulCmsService.page("jacke", SUPPORTED_LOCALES));

assertThat(content).isPresent();
assertThat(content.get().fieldOrEmpty("pageContent.notExistingAsset")).isEmpty();
String actual = content.get().fieldOrEmpty("pageContent.notExistingAsset");
assertThat(actual).isEmpty();

}

@Test
public void whenAskForContentInArray_thenGetElement() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);
Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("array[0].name");
assertThat(field).hasValue("author1");
}

@Test
public void whenAskForContentInArrayOutsideTheScope_thenReturnEmpty() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
// array element consists of only 4 items
Optional<String> field = content.get().field("array[4].name");
assertThat(field).isNotPresent();
}

@Test
public void whenAskForContentInNestedArray_thenGetElement() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("array[1].images[1].photo");
assertThat(field).hasValue("//images.contentful.com/l6chdlzlf8jn/3slBXXe6WcsiM46OuEuIKe/bab374a315d1825c111d9d89843cafc0/logo.gif");
}

@Test
public void whenAskForTextContentArray_thenGetElement() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("array[2].simpleText");
assertThat(field).hasValue("simpleText1");
}

@Test
public void whenAskForLocation_thenGetLocationField() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("locationField");
assertThat(field).hasValue("{lon=19.62158203125, lat=51.37199469960235}");
}

@Test
public void whenAskForContentInMediaField_thenGetUrlField() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("mediaOneFile");
assertThat(field).hasValue("//images.contentful.com/l6chdlzlf8jn/2m1NzbeXYUksOGIwceEy0U/4e3cc53a96313a4bd822777af78a3b4d/some-5.jpg");
}

@Test
public void whenAskForContentInMediaArray_thenGetUrlElements() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("mediaManyFiles[0]");
assertThat(field).hasValue("//images.contentful.com/l6chdlzlf8jn/6j9p38phC0oU0g42aqUSc4/2eb0c261bc13353ed867b13076af6b1f/logo.gif");

field = content.get().field("mediaManyFiles[1]");
assertThat(field).hasValue("//images.contentful.com/l6chdlzlf8jn/27BPx56xMcuGKe8uk8Auss/335581cd9daf3e9d0de254313e36d43b/some-5.jpg");
}

@Test
public void whenAskForArray_thenReturnEmpty() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
// textArrayField is an array element so it can't be fetched as a whole
Optional<String> field = content.get().field("array[3].textArrayField");
assertThat(field).isNotPresent();
}

@Test
public void whenAskForContentWithGermanOrEmptyLocales_thenGetGermanTranslation() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", SUPPORTED_LOCALES).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("array[3].textArrayField[1]");
assertThat(field).hasValue("zwei");

content = contentfulCmsService.page("finn", emptyList()).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
field = content.get().field("array[3].textArrayField[1]");
assertThat(field).hasValue("zwei");

content = waitAndGet(contentfulCmsService.page("finn", singletonList(Locale.ITALIAN)));

assertThat(content).isNotPresent();
}

@Test
public void whenAskForContentWithEnglishTranslation_thenGetEnglishTranslation() throws Exception {
ContentfulCmsService contentfulCmsService = ContentfulCmsService.of(spaceId(), token(), "finn", PAGE_TYPE_ID_FIELD_NAME);

Optional<CmsPage> content = contentfulCmsService.page("finn", singletonList(Locale.ENGLISH)).toCompletableFuture().get(5, TimeUnit.SECONDS);

assertThat(content).isPresent();
Optional<String> field = content.get().field("array[3].textArrayField[1]");
assertThat(field).hasValue("two");
}

private <T> T waitAndGet(final CompletionStage<T> stage) throws InterruptedException, ExecutionException, TimeoutException {
Expand Down
Loading