// Copyright 2007 Google Inc. All Rights Reserved.
//
// 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.google.enterprise.connector.gdata;
import junit.framework.Assert;
import junit.framework.TestCase;
import java.util.List;
import java.util.LinkedList;
import com.google.gdata.client.Service;
import com.google.gdata.client.Query;
import com.google.gdata.data.BaseFeed;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Entry;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.util.NotModifiedException;
import com.google.gdata.util.ServiceException;
import com.google.enterprise.connector.spi.Connector;
import com.google.enterprise.connector.spi.Document;
import com.google.enterprise.connector.spi.DocumentList;
import com.google.enterprise.connector.spi.Property;
import com.google.enterprise.connector.spi.Session;
import com.google.enterprise.connector.spi.AuthenticationManager;
import com.google.enterprise.connector.spi.AuthorizationManager;
import com.google.enterprise.connector.spi.TraversalManager;
import com.google.enterprise.connector.spi.RepositoryException;
import com.google.enterprise.connector.spi.SpiConstants;
/**
* This class is a test case that verifies several properties of the GdConnector
* implementation, namely:
* (1) The connector implements the interfaces required by the SPI
* (2) The connector can handle a not-modified condition of the feed
* (3) The connector can extract the required meta data from an entry
* (4) The connector actually queries the service and returns the results
*
* @author amsmith@google.com (Adam Smith)
*/
public class GdConnectorTest extends TestCase {
public void testConnectorImplementsRequiredInterfaces() {
try {
Connector connector = new GdConnector();
Session session = connector.login();
AuthenticationManager authnManager = session.getAuthenticationManager();
AuthorizationManager authzManager = session.getAuthorizationManager();
TraversalManager traversalManager = session.getTraversalManager();
} catch (ClassCastException cce) {
Assert.fail(cce.toString());
} catch (RepositoryException re) {
// this is OK to throw
}
}
public void testHandlesNotModifiedException() {
Service alwaysNotModifiedService = new Service() {
public BaseFeed query(Query query, Class entryClass, DateTime since)
throws ServiceException {
throw new NotModifiedException();
}
};
try {
GdConnector gdc = new GdConnector();
gdc.setService(alwaysNotModifiedService);
DocumentList resultSet = gdc.startTraversal();
assertNull(resultSet.nextDocument());
} catch (RepositoryException re) {
fail(re.toString());
}
}
public void testQueriesServiceAndReturnsAllAndOnlyResultingEntries() {
Service alwaysOneEntryService = new Service() {
public BaseFeed query(Query query, Class entryClass, DateTime since) {
return new Feed() {
public List getEntries() {
List list = new LinkedList();
Entry entry = new Entry();
entry.setId("\u9762");
entry.setUpdated(DateTime.now());
entry.addHtmlLink("http://localhost", "en", "example");
entry.setContent(
TextConstruct.create(TextConstruct.Type.TEXT,
"\u9762" + System.currentTimeMillis(), null));
list.add(entry);
return list;
}
};
}
};
try {
GdConnector gdc = new GdConnector();
gdc.setService(alwaysOneEntryService);
DocumentList resultSet = gdc.startTraversal();
Document docA = resultSet.nextDocument();
Document docB = resultSet.nextDocument();
assertNotNull(docA);
assertEquals("\u9762",
getFirstStringValue(docA, SpiConstants.PROPNAME_DOCID));
assertNull(docB);
} catch (RepositoryException re) {
fail(re.toString());
}
}
public void testMakeDocumentExtractsRequiredMetadata() {
String id = "ID";
String uri = "http://localhost";
DateTime dt = DateTime.now();
String contentNonce = "\u9762" + System.currentTimeMillis();
Entry entry = new Entry();
entry.setId(id);
entry.setUpdated(dt);
entry.addHtmlLink(uri, "en", "example");
entry.setContent(
TextConstruct.create(TextConstruct.Type.TEXT, contentNonce, null));
Document doc = null;
try {
doc = GdConnector.makeDocument(entry);
} catch (RepositoryException re) {
fail(re.toString());
}
assertEquals(id,
getFirstStringValue(doc, SpiConstants.PROPNAME_DOCID));
assertEquals(uri,
getFirstStringValue(doc, SpiConstants.PROPNAME_DISPLAYURL));
assertEquals(dt.toStringRfc822(),
getFirstStringValue(doc, SpiConstants.PROPNAME_LASTMODIFIED));
assertTrue(getFirstStringValue(doc, SpiConstants.PROPNAME_CONTENT)
.contains(contentNonce));
}
private static String getFirstStringValue(Document doc, String key) {
try {
Property prop = doc.findProperty(key);
return prop.nextValue().toString();
} catch (RepositoryException re) {
return null;
}
}
}