Skip to content

Instantly share code, notes, and snippets.

@cachrisman
Created March 31, 2023 13:51
Show Gist options
  • Save cachrisman/97646c26ba32a237a863193f185a0f99 to your computer and use it in GitHub Desktop.
Save cachrisman/97646c26ba32a237a863193f185a0f99 to your computer and use it in GitHub Desktop.
package com.contentful.java.cda.integration;
import com.contentful.java.cda.*;
import com.contentful.java.cda.QueryOperation.*;
import com.contentful.java.cda.TransformQuery.ContentfulEntryModel;
import com.contentful.java.cda.TransformQuery.ContentfulField;
import com.contentful.java.cda.TransformQuery.ContentfulSystemField;
import com.contentful.java.cda.lib.Enqueue;
import com.contentful.java.cda.lib.EnqueueResponse;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static com.contentful.java.cda.CDAType.SPACE;
import static com.contentful.java.cda.QueryOperation.*;
import static com.contentful.java.cda.SyncType.onlyDeletedAssets;
import static com.contentful.java.cda.SyncType.onlyEntriesOfType;
import static com.google.common.truth.Truth.assertThat;
public class IntegrationChina {
public static final String DEFAULT_TOKEN = "b4c0n73n7fu1";
public static final String DEFAULT_SPACE = "cfexampleapi";
public static final String CHINA_HOST = "content-cdn.customer.com.cn";
public static final String CHINA_ENDPOINT = "://content-cdn.philips.com.cn";
public static final String CONTENTFUL_DEFAULT_HOST = "://ctfassets.net";
public static final String ERROR_MESSAGE = "This is an expected error!";
CDAClient client;
@Before public void setUp() {
client = CDAClient.builder()
.setSpace(DEFAULT_SPACE)
.setToken(DEFAULT_TOKEN)
.setEndpoint(CHINA_ENDPOINT)
.build();
}
@Test
public void getChinaUrlForAsset() {
CDAAsset entry = client.fetch(CDAAsset.class).one("nyancat");
String url = entry.url();
String urlReplaced = url.replace(CONTENTFUL_DEFAULT_HOST, CHINA_HOST);
assertThat(urlReplaced).startsWith("//images" + "." + CHINA_HOST);
}
@Test(expected = RuntimeException.class) @Enqueue
public void throwingAnRuntimeException() {
try {
CDAClient.builder()
.setToken(DEFAULT_TOKEN)
.setSpace(DEFAULT_SPACE)
.build()
.fetchSpace();
} catch (RuntimeException e) {
assertThat(e.getCause()).isInstanceOf(IOException.class);
assertThat(e.getCause()).hasMessageThat().isEqualTo(ERROR_MESSAGE);
throw (e);
}
}
@Test(expected = CDAHttpException.class)
@Enqueue(
defaults = {},
complex = {@EnqueueResponse(fileName = "errors/invalid_query.json", code = 404)}
)
public void sendingInvalidQueriesThrowsMeaningfulException() {
try {
client.fetch(CDAEntry.class).where("not", "existing").all();
} catch (CDAHttpException cdaException) {
assertThat(cdaException.responseCode()).isEqualTo(404);
assertThat(cdaException.responseMessage()).isEqualTo("Client Error");
throw cdaException;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment