Permalink
Browse files

Added ApiUrlConverter

  • Loading branch information...
1 parent 78e7bf9 commit 527fa0a381fe2b1dd50f4468e38e11038bac1f69 Matthew Wall committed Jun 21, 2010
@@ -0,0 +1,79 @@
+/**
+ * Copyright 2010 Guardian News And Media
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ **/
+
+package com.gu.openplatform.contentapi;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+abstract class ApiUrlTranslator {
+
+ private static final Logger logger = LoggerFactory.getLogger(ApiUrlTranslator.class);
+
+ static boolean canTranslateUrl(String url) {
+ return (url != null) && (url.startsWith("http://www.guardian.co.uk/")
+ || url.startsWith("/")
+ || url.startsWith("www.guardian.co.uk/"));
+ }
+
+ static boolean canTranslateUrl(URL url) {
+ return canTranslateUrl(url.toString());
+ }
+
+ static URL translateToApiUrl(URL from) {
+ return translateToApiUrl(from.toString());
+ }
+
+ static URL translateToApiUrl(String from) {
+ assert canTranslateUrl(from);
+ String to;
+
+ int guHost = from.indexOf("http://www.guardian.co.uk/");
+
+ if (guHost != -1) {
+ to = from.replace("http://www.guardian.co.uk", GuardianApiHostProvider.getGuardianApiHost());
+ } else {
+ guHost = from.indexOf("www.guardian.co.uk/");
+
+ if (guHost != -1) {
+ to = from.replace("www.guardian.co.uk", GuardianApiHostProvider.getGuardianApiHost());
+ } else {
+ if (!from.startsWith("/")) {
+ to = GuardianApiHostProvider.getGuardianApiHost().concat("/").concat(from);
+ } else {
+ to = GuardianApiHostProvider.getGuardianApiHost().concat(from);
+ }
+ }
+ }
+
+ return makeUrl(to);
+ }
+
+ private static URL makeUrl(String to) {
+ URL toUrl;
+ try {
+ toUrl = new URL(to);
+ } catch (MalformedURLException e) {
+ logger.error("Cannot create URL " + to, e);
+ throw new RuntimeException(e);
+ }
+ return toUrl;
+ }
+
+}
@@ -17,30 +17,19 @@
package com.gu.apitest;
import com.gu.openplatform.contentapi.ContentValidator;
+import com.gu.openplatform.contentapi.FirstItemInIndexAwareTest;
import com.gu.openplatform.contentapi.GuardianContentApi;
import com.gu.openplatform.contentapi.ItemResult;
import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
import java.net.URL;
import static org.junit.Assert.assertTrue;
-public class OutOfPackageItemEndpointTest {
+public class OutOfPackageItemEndpointTest extends FirstItemInIndexAwareTest {
private ItemResult result;
- private String firstItemInIndexUrl;
-
- @Before
- public void setUp() throws Exception {
- firstItemInIndexUrl =
- GuardianContentApi.minimalContentSearchQuery()
- .execute()
- .getFirst()
- .getApiUrl()
- .toString();
- }
@After
public void tearDown() throws Exception {
@@ -0,0 +1,53 @@
+/**
+ * Copyright 2010 Guardian News And Media
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ **/
+
+package com.gu.openplatform.contentapi;
+
+import org.junit.Test;
+
+import java.net.URL;
+
+import static com.gu.openplatform.contentapi.ApiUrlTranslator.canTranslateUrl;
+import static com.gu.openplatform.contentapi.ApiUrlTranslator.translateToApiUrl;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class ApiUrlTranslatorTest {
+
+ @Test
+ public void testWeUnderstandWhichUrlsCanBeTranslated() throws Exception {
+ assertTrue(canTranslateUrl("http://www.guardian.co.uk/commentisfree/2009/sep/30/polly-toynbee-public-service-cuts"));
+ assertTrue(canTranslateUrl("/commentisfree/2009/sep/30/polly-toynbee-public-service-cuts"));
+ assertFalse(canTranslateUrl("http://www.whelks.com/foo"));
+ assertTrue(canTranslateUrl("www.guardian.co.uk/commentisfree/2009/sep/30/polly-toynbee-public-service-cuts"));
+ assertTrue(canTranslateUrl(new URL("http://www.guardian.co.uk/foo")));
+ }
+
+ @Test
+ public void testCanTranslateUrls() throws Exception {
+ assertThat(apiUrl("/foo"), equalTo(translateToApiUrl("http://www.guardian.co.uk/foo").toString()));
+ assertThat(apiUrl("/foo"), equalTo(translateToApiUrl("www.guardian.co.uk/foo").toString()));
+ assertThat(apiUrl("/foo"), equalTo(translateToApiUrl("foo").toString()));
+ assertThat(apiUrl("/foo"), equalTo(translateToApiUrl("/foo").toString()));
+ assertThat(apiUrl("/foo"), equalTo(translateToApiUrl(new URL("http://www.guardian.co.uk/foo")).toString()));
+ }
+
+ private String apiUrl(String path) {
+ return GuardianApiHostProvider.getGuardianApiHost().concat(path);
+ }
+}
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2010 Guardian News And Media
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ **/
+
+package com.gu.openplatform.contentapi;
+
+import org.junit.Before;
+
+public abstract class FirstItemInIndexAwareTest {
+
+ protected String firstItemInIndexUrl;
+
+ @Before
+ public void setUp() throws Exception {
+ firstItemInIndexUrl =
+ GuardianContentApi.minimalContentSearchQuery()
+ .execute()
+ .getFirst()
+ .getApiUrl()
+ .toString();
+ }
+}

0 comments on commit 527fa0a

Please sign in to comment.