From cae65ce01dc07228e9b9f71015223bff3494a5d8 Mon Sep 17 00:00:00 2001 From: jomu Date: Tue, 3 Feb 2015 18:20:57 +0000 Subject: [PATCH] added basic query functionality, added listsitem update for lookup items --- sharepoint/api/pom.xml | 16 +- .../shared/sharepoint/api/SPJaxbObject.java | 19 +- .../shared/sharepoint/api/lists/SPList.java | 231 +++-- .../shared/sharepoint/api/lists/SPLists.java | 138 +++ .../src/main/resources/2010/wsdl/lists.wsdl | 4 +- .../src/main/resources/2010/wsdl/search.wsdl | 366 ++++++++ .../api/src/main/resources/jaxb/listitems.xml | 12 + .../api/src/main/resources/xsd/list.xsd | 570 ++++++++++++ .../api/src/main/resources/xsd/lists.xsd | 89 +- .../sharepoint/api/NtlmAuthenticatorTest.java | 15 +- .../sharepoint/api/lists/SPListTest.java | 154 ++-- .../sharepoint/api/lists/SPListsTest.java | 41 + .../shared/sharepoint/api/lists/testlist.xml | 811 ++++++++++++++++++ .../shared/sharepoint/api/lists/testlists.xml | 14 + sharepoint/mock/pom.xml | 2 +- 15 files changed, 2248 insertions(+), 234 deletions(-) create mode 100644 sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPLists.java create mode 100644 sharepoint/api/src/main/resources/2010/wsdl/search.wsdl create mode 100644 sharepoint/api/src/main/resources/jaxb/listitems.xml create mode 100644 sharepoint/api/src/main/resources/xsd/list.xsd create mode 100644 sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListsTest.java create mode 100644 sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlist.xml create mode 100644 sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlists.xml diff --git a/sharepoint/api/pom.xml b/sharepoint/api/pom.xml index 6ec6af0..1eaabda 100644 --- a/sharepoint/api/pom.xml +++ b/sharepoint/api/pom.xml @@ -136,6 +136,20 @@ + + search2010 + generate-sources + + wsimport + + + com.microsoft.schemas.sharepoint.soap.search + ${basedir}/src/main/resources/2010/wsdl + + search.wsdl + + + @@ -196,7 +210,7 @@ lists.xml true - + diff --git a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java index a9a932e..254956d 100644 --- a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java +++ b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java @@ -1,16 +1,3 @@ -/* - * File: $$RCSfile$$ - * - * Copyright (c) 2011 by Wincor Nixdorf, - * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany - * - * This software is the confidential and proprietary information - * of Wincor Nixdorf. - * - * You shall not disclose such confidential information and shall - * use it only in accordance with the terms of the license agreement - * you entered into with Wincor Nixdorf. - */ package de.muehlencord.shared.sharepoint.api; import java.io.IOException; @@ -18,7 +5,6 @@ import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import java.util.List; -import java.util.logging.Level; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -81,7 +67,7 @@ public abstract class SPJaxbObject extends SPObject { return null; } - Schema schema; + Schema schema = null; try { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); StreamSource[] streamSources = new StreamSource[schemaLocations.size()]; @@ -93,7 +79,8 @@ public abstract class SPJaxbObject extends SPObject { } schema = sf.newSchema(streamSources); } catch (Exception ex) { - return null; // TODO add error handling + ex.printStackTrace(); // TODO add error handling + // throw new SAXException ("Cannot convert to object. Reason: "+ex.getMessage(), ex); } return schema; } diff --git a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java index dacaf07..a0ce5fe 100644 --- a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java +++ b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java @@ -1,10 +1,11 @@ package de.muehlencord.shared.sharepoint.api.lists; -import com.microsoft.schemas.sharepoint.soap.lists.GetListCollectionResponse; -import com.microsoft.schemas.sharepoint.soap.lists.GetListResponse.GetListResult; -import com.microsoft.schemas.sharepoint.soap.lists.Lists; +import com.microsoft.schemas.sharepoint.soap.lists.GetListItems; +import com.microsoft.schemas.sharepoint.soap.lists.GetListItems.QueryOptions; +import com.microsoft.schemas.sharepoint.soap.lists.GetListItemsResponse; +import com.microsoft.schemas.sharepoint.soap.lists.GetListResponse; import com.microsoft.schemas.sharepoint.soap.lists.ListsSoap; -import com.microsoft.schemas.sharepoint.soap.lists.UpdateListItems.Updates; +import com.microsoft.schemas.sharepoint.soap.lists.UpdateListItems; import de.muehlencord.shared.sharepoint.api.SPContext; import de.muehlencord.shared.sharepoint.api.SPJaxbObject; import de.muehlencord.shared.sharepoint.api.SPObject; @@ -14,138 +15,184 @@ import de.muehlencord.shared.sharepoint.api.batch.Method; import de.muehlencord.shared.sharepoint.api.batch.MethodType; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.text.ParseException; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedList; -import java.util.List; import java.util.Map; import javax.xml.bind.JAXBException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.ws.BindingProvider; import javax.xml.ws.handler.Handler; import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * - * @author jomu + * @author joern.muehlencord */ -public class SPList extends SPJaxbObject { +public class SPList extends SPJaxbObject { - public SPList(SPContext context) throws JAXBException { - super(de.muehlencord.shared.sharepoint.api.lists.Lists.class, context); + private String listName = null; + + public SPList(SPContext context, String listName) throws JAXBException { + super(List.class, context); + getValue().name = listName; } @Override - protected List getSchemaLocation() { - List schemaList = new ArrayList(); - schemaList.add("/xsd/lists.xsd"); + protected java.util.List getSchemaLocation() { + java.util.List schemaList = new ArrayList(); + schemaList.add("/xsd/list.xsd"); return schemaList; } - public List getListNames() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, ParseException, JAXBException, + + /* *** list information *** */ + + public String getListName() { + return getValue().name; + } + + /* *** queries *** */ + public java.util.List> getListItems(java.util.List listColumnNames, String rowLimit) throws NoSuchAlgorithmException, + KeyManagementException, JAXBException, MalformedURLException, SAXException { - if (this.getValue().lists == null) { - getValueFromSharepoint(); - } - List returnList = new LinkedList<>(); - this.getValue().lists.stream(). - forEach((currentList) -> { - returnList.add(currentList.name); - }); - return returnList; - } - public String getListName(String listTitle) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, MalformedURLException, SAXException { - if (this.getValue().lists == null) { - getValueFromSharepoint(); - } + //Here are additional parameters that may be set + String viewName = null; + GetListItems.ViewFields viewFields = null; + GetListItems.Query query = null; + QueryOptions queryOptions = new QueryOptions(); + String webId = null; - for (de.muehlencord.shared.sharepoint.api.lists.List currentList : this.getValue().lists) { - if (currentList.title.equals(listTitle)) { - return currentList.name; - } - } - return null; - } + GetListItemsResponse.GetListItemsResult result = getListsPort(). + getListItems(getValue().name, viewName, query, viewFields, rowLimit, queryOptions, webId); - public void getListColumns(String listName) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, MalformedURLException, SAXException { - GetListResult result = getListsPort(getContext().getSiteURL()).getList(listName); - - if (result.getContent() != null) { + java.util.List> returnList = new LinkedList<>(); + if (result != null && result.getContent() != null) { for (Object content : result.getContent()) { // TODO - handdling more than one result / should not occur if (content instanceof Element) { - // Parse XML file + + // parse XML manually. Attribute list is generated automatically as the columns + // are stored as attribute with ows_. This makes an easy to use + // XSD generated java object unhandy. Element rootElement = (Element) content; - String listsString = SPObject.xmlToString(rootElement.getOwnerDocument()); - System.out.println (listsString); - // TODO implement handling - // this.setValue(listsString); + + NodeList list = rootElement.getElementsByTagName("z:row"); + for (int i = 0; i < list.getLength(); i++) { + java.util.List rowList = new LinkedList<>(); + //Gets the attributes of the current row/element + NamedNodeMap attributes = list.item(i).getAttributes(); + // include all attributes which are requested by the given column names + // important - blank codes to _x0020_ as blank is not allowed in XML attribute names + + for (String columnName : listColumnNames) { + String internalColumnName = "ows_" + columnName.replace(" ", "_x0020_"); + if (attributes.getNamedItem(internalColumnName) != null) { + rowList.add(attributes.getNamedItem(internalColumnName).getNodeValue()); + } else { + // TODO create better exception handling + throw new JAXBException("Couldn't find the '" + columnName + "' column in the '" + listName + "' list in SharePoint.\n"); + } + } + returnList.add(rowList); + } } } } + return returnList; // TODO either return null or return empty lists, currently handled differently } - public void addListItem(String listName, Map data) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, - MalformedURLException, SAXException, ParserConfigurationException, IOException { - List> dataList = new LinkedList<>(); + + /* *** manipulating list items *** */ + public void addListItem(String listName, Map data) throws NoSuchAlgorithmException, KeyManagementException, + JAXBException, MalformedURLException, SAXException, ParserConfigurationException, IOException { + java.util.List> dataList = new LinkedList<>(); dataList.add(data); addListItems(listName, dataList); } - public void addListItemByTitle(String listTitle, Map data) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, - MalformedURLException, SAXException, ParserConfigurationException, IOException { - List> dataList = new LinkedList<>(); - dataList.add(data); - addListItems(getListName(listTitle), dataList); - } + public void addListItems(String listName, java.util.List> dataList) throws NoSuchAlgorithmException, + KeyManagementException, JAXBException, MalformedURLException, SAXException, ParserConfigurationException, IOException { - public void addListItems(String listName, List> dataList) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, - MalformedURLException, SAXException, ParserConfigurationException, IOException { - if (this.getValue().lists == null) { - getValueFromSharepoint(); - } - - // prepare batch to be posted to sharepoint server SPBatch batch = new SPBatch(getContext()); - for (Integer batchId = 1; batchId <= dataList.size(); batchId++) { + for (Integer batchId = 1; batchId <= dataList.size(); + batchId++) { Method method = new Method(); method.setID(batchId); method.setCmd(MethodType.NEW); Map data = dataList.get(batchId - 1); data.keySet().stream(). - map((key) -> { + map((String key) -> { Field field = new Field(); - field.setName(key); + field.setName(key.replace(" ", "_x0020_")); field.setContent(data.get(key)); return field; }). - forEach((field) -> { + forEach((Field field) -> { method.getFields().add(field); }); batch.addMethod(method); } - - // convert batch to node and attach it to update to be executed - Updates updates = new Updates(); - String batchXML = batch.getValueXmlString(); - System.out.println(batchXML); + UpdateListItems.Updates updates = new UpdateListItems.Updates(); Node node = batch.createSharePointCAMLNode(); updates.getContent().add(node); - - // use created update object to execute on sharepoint - getListsPort(getContext().getSiteURL()).updateListItems(listName, updates); + getListsPort().updateListItems(listName, updates); } - private ListsSoap getListsPort(URL webUrl) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException { - URL wsURL = new URL(webUrl.toString() + "/_vti_bin/Lists.asmx"); - URL wsdlURL = new URL(SPContext.class.getResource("/2010/wsdl/lists.wsdl").toExternalForm()); - Lists service = new Lists(wsdlURL); + /* *** information & tools *** */ + public Map getLookupValueMap(String value, String rowLimit) throws NoSuchAlgorithmException, + KeyManagementException, JAXBException, MalformedURLException, SAXException { + + // get list values from sharepoint + java.util.List listColumnNames = new ArrayList<>(); + listColumnNames.add("ID"); + listColumnNames.add(value); + java.util.List> listItems = getListItems(listColumnNames, rowLimit); + + // convert to map from value to ;# + Map returnMap = new HashMap<>(); + + for (java.util.List row : listItems) { + String rowId = row.get(0); + String rowValue = row.get(1); + String rowLookupValue = rowId + ";#" + rowValue; + if (returnMap.containsKey(rowValue)) { + throw new SAXException("Key " + rowValue + " already exists - key values must be unique"); + } else { + returnMap.put(rowValue, rowLookupValue); + } + } + return returnMap; + } + + public String getColumnNameByDisplayName(String columnDisplayName) throws NoSuchAlgorithmException, + KeyManagementException, JAXBException, MalformedURLException, SAXException { + if (getValue().fields == null) { + getListFromSharepoint(getValue().name); + } + + Fields fields = getValue().fields; + for (de.muehlencord.shared.sharepoint.api.lists.Field field : fields.getFields()) { + if (field.getDisplayName().equals(columnDisplayName)) { + return field.getName(); + } + } + return null; + } + + /* *** private helper methods *** */ + private ListsSoap getListsPort() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException { + java.net.URL contextURL = getContext().getSiteURL(); + java.net.URL wsURL = new java.net.URL(contextURL.toString() + "/_vti_bin/Lists.asmx"); + java.net.URL wsdlURL = new java.net.URL(SPContext.class.getResource("/2010/wsdl/lists.wsdl").toExternalForm()); + com.microsoft.schemas.sharepoint.soap.lists.Lists service = new com.microsoft.schemas.sharepoint.soap.lists.Lists(wsdlURL); ListsSoap listsPort = service.getListsSoap(); ((BindingProvider) listsPort).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsURL.toString()); @@ -157,8 +204,10 @@ public class SPList extends SPJaxbObject { + + /* *** constructors & object methods needed *** */ + public SPLists(SPContext context) throws JAXBException { + super(de.muehlencord.shared.sharepoint.api.lists.Lists.class, context); + } + + @Override + protected java.util.List getSchemaLocation() { + java.util.List schemaList = new ArrayList(); + schemaList.add("/xsd/lists.xsd"); + return schemaList; + } + + /* ** get list objects *** */ + public SPList getSpListByTitle(String listName) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, MalformedURLException, SAXException { + if (this.getValue().lists == null) { + getValueFromSharepoint(); + } + List list = getListByTitle(listName); + SPList returnList = new SPList(getContext(), listName); + returnList.setValue(list); + + return returnList; + } + + /* *** get lists information *** */ + public java.util.List getListNames() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, ParseException, JAXBException, + SAXException { + if (this.getValue().lists == null) { + getValueFromSharepoint(); + } + java.util.List returnList = new LinkedList<>(); + this.getValue().lists.stream(). + forEach((currentList) -> { + returnList.add(currentList.name); + }); + return returnList; + } + + public String getListNameByTitle(String listTitle) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, + MalformedURLException, SAXException { + + if (this.getValue().lists == null) { + getValueFromSharepoint(); + } + + for (List list : getValue().lists) { + if (list.title.equals(listTitle)) { + return list.name; + } + } + return null; + } + + + /* *** private helper methods *** */ + private ListsSoap getListsPort() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException { + + URL wsURL = new URL(getContext().getSiteURL().toString() + "/_vti_bin/Lists.asmx"); + URL wsdlURL = new URL(SPContext.class.getResource("/2010/wsdl/lists.wsdl").toExternalForm()); + Lists service = new Lists(wsdlURL); + ListsSoap listsPort = service.getListsSoap(); + ((BindingProvider) listsPort).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsURL.toString()); + + // log all incoming and outgoing communication - TODO make this configurable by DEBUG switch + java.util.List handlers = ((BindingProvider) listsPort).getBinding().getHandlerChain(); + handlers.add(new ServiceLogHandler()); + ((BindingProvider) listsPort).getBinding().setHandlerChain(handlers); + + return listsPort; + } + + private void getValueFromSharepoint() throws NoSuchAlgorithmException, KeyManagementException, JAXBException, MalformedURLException, SAXException { + GetListCollectionResponse.GetListCollectionResult result = getListsPort().getListCollection(); + + if (result.getContent() != null) { + for (Object content : result.getContent()) { + // TODO - handdling more than one result / should not occur + if (content instanceof Element) { + // Parse XML file + Element rootElement = (Element) content; + String listsString = SPObject.xmlToString(rootElement.getOwnerDocument()); + System.out.println(listsString); + this.setValue(listsString); + } + } + } + } + + private List getListByTitle(String listTitle) { + for (List list : this.getValue().lists) { + if (list.getTitle().equals(listTitle)) { + return list; + } + } + // return null, if list is not found + return null; + } + + private List getListByName(String listName) { + for (List list : this.getValue().lists) { + if (list.getName().equals(listName)) { + return list; + } + } + // return null, if list is not found + return null; + } + +} diff --git a/sharepoint/api/src/main/resources/2010/wsdl/lists.wsdl b/sharepoint/api/src/main/resources/2010/wsdl/lists.wsdl index 7eb9c75..12da63a 100644 --- a/sharepoint/api/src/main/resources/2010/wsdl/lists.wsdl +++ b/sharepoint/api/src/main/resources/2010/wsdl/lists.wsdl @@ -184,7 +184,7 @@ - + @@ -263,7 +263,7 @@ - + diff --git a/sharepoint/api/src/main/resources/2010/wsdl/search.wsdl b/sharepoint/api/src/main/resources/2010/wsdl/search.wsdl new file mode 100644 index 0000000..281091d --- /dev/null +++ b/sharepoint/api/src/main/resources/2010/wsdl/search.wsdl @@ -0,0 +1,366 @@ + + + Microsoft SharePoint Server 2013 Preview Search Query Web Service + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Microsoft SharePoint Server 2013 Preview Search Query Web Service + + + + + + + + diff --git a/sharepoint/api/src/main/resources/jaxb/listitems.xml b/sharepoint/api/src/main/resources/jaxb/listitems.xml new file mode 100644 index 0000000..e87cd0a --- /dev/null +++ b/sharepoint/api/src/main/resources/jaxb/listitems.xml @@ -0,0 +1,12 @@ + + + + + + + + + \ No newline at end of file diff --git a/sharepoint/api/src/main/resources/xsd/list.xsd b/sharepoint/api/src/main/resources/xsd/list.xsd new file mode 100644 index 0000000..f064ca0 --- /dev/null +++ b/sharepoint/api/src/main/resources/xsd/list.xsd @@ -0,0 +1,570 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Restrict + Cascade + + + + + diff --git a/sharepoint/api/src/main/resources/xsd/lists.xsd b/sharepoint/api/src/main/resources/xsd/lists.xsd index f1d89f8..874f482 100644 --- a/sharepoint/api/src/main/resources/xsd/lists.xsd +++ b/sharepoint/api/src/main/resources/xsd/lists.xsd @@ -1,79 +1,14 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + diff --git a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/NtlmAuthenticatorTest.java b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/NtlmAuthenticatorTest.java index c2ff2c8..303b940 100644 --- a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/NtlmAuthenticatorTest.java +++ b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/NtlmAuthenticatorTest.java @@ -2,24 +2,21 @@ package de.muehlencord.shared.sharepoint.api; import de.muehlencord.shared.sharepoint.api.sites.SPSite; import java.net.MalformedURLException; -import java.net.URL; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; import org.junit.Test; /** * * @author jomu */ -public class NtlmAuthenticatorTest { - +public class NtlmAuthenticatorTest extends BaseTest { @Test - public void testAuthentication() throws MalformedURLException { - // Create NTLM v2 credentials (authenticator) & setup context - NtlmAuthenticator credentials = new NtlmAuthenticator("DOMAIN", "username", "password"); - SPContext context = new SPContext(new URL ("http://localhost:8088/"), credentials, SPVersion.SP2010); - // Connect to Sharepoint - SPSite instance = new SPSite(context); + public void testAuthentication() throws MalformedURLException, NoSuchAlgorithmException, KeyManagementException { + SPSite instance = new SPSite(context); + instance.getRootWeb(); } } diff --git a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java index 888fab4..02bcdf0 100644 --- a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java +++ b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java @@ -1,18 +1,17 @@ package de.muehlencord.shared.sharepoint.api.lists; -import de.muehlencord.shared.sharepoint.api.NtlmAuthenticator; -import de.muehlencord.shared.sharepoint.api.SPContext; -import de.muehlencord.shared.sharepoint.api.SPVersion; +import de.muehlencord.shared.sharepoint.api.BaseTest; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.text.ParseException; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXBException; import javax.xml.parsers.ParserConfigurationException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Ignore; import org.junit.Test; import org.xml.sax.SAXException; @@ -20,56 +19,111 @@ import org.xml.sax.SAXException; * * @author joern.muehlencord */ -public class SPListTest { +public class SPListTest extends BaseTest { - public SPListTest() { + @Test + public void testFromXML() throws Exception { + String xmlString = readFileContentFromTest("lists/testlist.xml", "UTF-8"); + SPList list = new SPList(context, "{924883B9-41B7-430C-8206-151786A67319}"); + assertNotNull(xmlString); + list.setValue(xmlString); } @Test - public void testSPList() throws MalformedURLException, NoSuchAlgorithmException, KeyManagementException, ParseException, JAXBException, SAXException, - ParserConfigurationException, IOException { - // Create NTLM v2 credentials (authenticator) & setup context - - // FIXME - needs to be implemented using mock; replace with real data but make sure password is not commited - NtlmAuthenticator credentials = null; - SPContext context = null; - // Connect to Sharepoint - // SPSite instance = new SPSite(context); - SPList instance = new SPList(context); - // System.out.println (instance.getListNames()); - // System.out.println (instance.getListName("Test")); - // instance.getListColumns(instance.getListName("Questionnaire Test")); + @Ignore // Depends on available sharepoint currently + public void addListItemByTitle() throws NoSuchAlgorithmException, KeyManagementException, JAXBException, SAXException, ParserConfigurationException, + IOException { + + SPLists instance = new SPLists(context); + SPList list = instance.getSpListByTitle("Test"); + assertNotNull(list); + String listName = list.getListName(); + assertEquals("Listname", "{D8C8D97E-1065-4674-A8F2-026D6D478794}", listName); + + // get list of all country codes + java.util.List columns = new ArrayList<>(); + columns.add("Title"); + columns.add("ID"); + java.util.List> countryCodes = list.getListItems(columns, "300"); -// Map data = new HashMap<>(); -// data.put("Country", "DE"); -// data.put("WNRegion", "DACH"); -// data.put("AccountManager", "Jörn"); -// data.put("Customer", "Mühlencord"); -// data.put("Current OS", "Windows XP"); -// data.put("PlannedOS", "migrate to Windows 7"); -// data.put("RolloutStart", "in 2014"); -// data.put("RolloutEnd", "in 2015"); -// data.put("InstalledBase", "2000"); -// data.put("IbaseMigrated", "1000"); -// data.put("IbaseOrdered", "500"); -// data.put("IbaseNotOrdered", "500"); -// data.put("IbaseNotMigrated", "0"); -// data.put("ReaseonNotMigrated", ""); -// data.put("Platform", "ProBase/C"); -// data.put("IbaseOldPlatform", "0"); -// data.put("Application", "ProFlex4"); -// data.put("IbaseOldApplication", "0"); -// data.put("MaintenanceContract", "none"); -// data.put("Comments", "This is a test"); -// instance.addListItemByTitle("Questionnaire Test", data); - Map data = new HashMap<>(); - data.put ("Title", "Test"); - data.put ("Choice", "Enter Choice #1"); // is not check, possible to add wrong value - // data.put ("Country", "DE"); // reference needed, need to implement lookup - data.put ("Number", "17"); // is validating - data.put ("Lines", "Long text
with line breaks"); // line breaks using
- instance.addListItemByTitle("Test", data); + data.put("Title", "Test"); + data.put("Choice", "Enter Choice #1"); // is not checked, possible to add wrong value + data.put("Country", "357;#DE"); // reference taken from testGetLookupValuesMap + data.put("Number", "17"); // is validating + data.put("Lines", "Long text
with line breaks"); // line breaks using
+ list.addListItem(listName, data); } + @Test + // @Ignore // Depends on available sharepoint currently + public void addListItemBigList() throws NoSuchAlgorithmException, KeyManagementException, JAXBException, SAXException, ParserConfigurationException, + IOException { + + SPLists instance = new SPLists(context); + SPList list = instance.getSpListByTitle("Questionnaire"); + assertNotNull(list); + String listName = list.getListName(); + assertEquals ("Listname", "{4728157C-CFA4-447E-8863-170133FDC351}", listName); + + Map data = new HashMap<>(); + data.put(list.getColumnNameByDisplayName("Country"), "357;#DE"); + data.put(list.getColumnNameByDisplayName("WNRegion"), "DACH"); + data.put(list.getColumnNameByDisplayName("AccountManager"), "Jörn"); + data.put(list.getColumnNameByDisplayName("Customer"), "Mühlencord"); + data.put(list.getColumnNameByDisplayName("Current OS"), "Windows XP"); + data.put(list.getColumnNameByDisplayName("PlannedOS"), "migrate to Windows 7"); + data.put(list.getColumnNameByDisplayName("RolloutStart"), "in 2014"); + data.put(list.getColumnNameByDisplayName("RolloutEnd"), "in 2015"); + data.put(list.getColumnNameByDisplayName("InstalledBase"), "2000"); + data.put(list.getColumnNameByDisplayName("IbaseMigrated"), "1000"); + data.put(list.getColumnNameByDisplayName("IbaseOrdered"), "500"); + data.put(list.getColumnNameByDisplayName("IbaseNotOrdered"), "500"); + data.put(list.getColumnNameByDisplayName("IbaseNotMigrated"), "0"); + data.put(list.getColumnNameByDisplayName("ReaseonNotMigrated"), ""); + data.put(list.getColumnNameByDisplayName("Platform"), "ProBase/C"); + data.put(list.getColumnNameByDisplayName("IbaseOldPlatform"), "0"); + data.put(list.getColumnNameByDisplayName("Application"), "ProFlex4"); + data.put(list.getColumnNameByDisplayName("IbaseOldApplication"), "0"); + data.put(list.getColumnNameByDisplayName("MaintenanceContract"), "none"); + data.put(list.getColumnNameByDisplayName("Comments"), "This is a test"); + list.addListItem(listName, data); + } + + @Test + @Ignore // Depends on available sharepoint currently + public void testGetListItems() throws Exception { + SPLists instance = new SPLists(context); + SPList list = instance.getSpListByTitle("Questionnaire_Countries"); + assertNotNull(list); + java.util.List columns = new ArrayList<>(); + // columns.add("CountryCode"); + columns.add("Title"); + columns.add("ID"); + java.util.List> smallList = list.getListItems(columns, "30"); + assertNotNull(smallList); + assertEquals("Size smallList", 30, smallList.size()); + java.util.List> bigList = list.getListItems(columns, "300"); + assertNotNull(bigList); + assertEquals("Size bigList", 250, bigList.size()); + } + + @Test + @Ignore // Depends on available sharepoint currently + public void testGetLookupValueMap() throws Exception { + SPLists instance = new SPLists(context); + SPList list = instance.getSpListByTitle("Questionnaire_Countries"); + Map lookupValues = list.getLookupValueMap("Title", "0"); + System.out.println(lookupValues); + + } + + @Test + @Ignore // Depends on available sharepoint currently + public void testGetColumnNameByDisplayName() throws Exception { + SPLists instance = new SPLists(context); + SPList list = instance.getSpListByTitle("Questionnaire"); + assertEquals ("Displayname of Current OS", "Current_x0020_OS", list.getColumnNameByDisplayName("Current OS")); + + } } diff --git a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListsTest.java b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListsTest.java new file mode 100644 index 0000000..80ffd1e --- /dev/null +++ b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListsTest.java @@ -0,0 +1,41 @@ +package de.muehlencord.shared.sharepoint.api.lists; + +import de.muehlencord.shared.sharepoint.api.BaseTest; +import static de.muehlencord.shared.sharepoint.api.BaseTest.readFileContentFromTest; +import java.io.IOException; +import java.net.MalformedURLException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import javax.xml.bind.JAXBException; +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.xml.sax.SAXException; + +/** + * + * @author joern.muehlencord + */ +public class SPListsTest extends BaseTest { + + + + @Test + //@Ignore + public void testFromXML() throws IOException, JAXBException, SAXException { + String xmlString = readFileContentFromTest("lists/testlists.xml", "UTF-8"); + SPLists lists = new SPLists(context); + lists.setValue (xmlString); + System.out.println(xmlString); + } + + @Test + @Ignore // Depends on available sharepoint currently + public void testGetListName() throws Exception { + SPLists instance = new SPLists(context); + String listName = instance.getListNameByTitle("Questionnaire"); + assertEquals("Listname", "{4728157C-CFA4-447E-8863-170133FDC351}", listName); + } + +} diff --git a/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlist.xml b/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlist.xml new file mode 100644 index 0000000..82b380a --- /dev/null +++ b/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlist.xml @@ -0,0 +1,811 @@ + + diff --git a/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlists.xml b/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlists.xml new file mode 100644 index 0000000..1c3dbf7 --- /dev/null +++ b/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/testlists.xml @@ -0,0 +1,14 @@ + + + diff --git a/sharepoint/mock/pom.xml b/sharepoint/mock/pom.xml index 333a2b8..751ca1b 100644 --- a/sharepoint/mock/pom.xml +++ b/sharepoint/mock/pom.xml @@ -3,7 +3,7 @@ 4.0.0 de.muehlencord.shared - sharepoint + shared-sharepoint 1.0-SNAPSHOT de.muehlencord.shared.sharepoint