http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4261deb7/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/EdmUriBuilderTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/EdmUriBuilderTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/EdmUriBuilderTest.java new file mode 100644 index 0000000..4e4baf6 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/EdmUriBuilderTest.java @@ -0,0 +1,988 @@ +package org.apache.olingo.odata2.client.core.uri; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.URI; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmComplexType; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.edm.EdmFunctionImport; +import org.apache.olingo.odata2.api.edm.EdmNavigationProperty; +import org.apache.olingo.odata2.api.edm.EdmParameter; +import org.apache.olingo.odata2.api.edm.EdmProperty; +import org.apache.olingo.odata2.api.exception.ODataException; +import org.apache.olingo.odata2.client.api.uri.QueryOption; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.junit.Before; +import org.junit.Test; + +public class EdmUriBuilderTest { + + protected static final String SERVICE_ROOT_URI = "http://host:80/service/"; + protected static final String SERVICE_ROOT_URI_1 = "http://host:80/service"; + private Edm edm; + + @Before + public void getEdm() throws ODataException { + edm = MockFacade.getMockEdm(); + } + + @Test + public void testUriSimpleES() throws EdmException { + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(edm.getDefaultEntityContainer().getEntitySet("Employees")). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithCountUri1() throws EdmException { + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(edm.getDefaultEntityContainer().getEntitySet("Employees")). + appendCountSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees/$count", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void negTestWithCountAndFormat() throws EdmException { + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(edm.getDefaultEntityContainer().getEntitySet("Employees")). + appendCountSegment(). + format("application/json"). + build(); + } + + @Test + public void testSimpleESWithCountUri2() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendCountSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees/$count", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithCountAndFilter() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendCountSegment(). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees/" + + "$count?$filter=TeamId%20eq%20'1'", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void negTestUriWithCountSegment1() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet employeeEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)employeeEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendCountSegment(). + build(); + } + + @Test(expected=RuntimeException.class) + public void negTestUriWithCountSegment2() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet employeeEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)employeeEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)employeeEntitySet.getEntityType().getProperty("ne_Team")). + appendCountSegment(). + build(); + } + + @Test + public void testMetadataUri() throws EdmException { + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendMetadataSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/$metadata", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithKeyUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty) entitySet.getEntityType().getProperty("EmployeeId"), "1"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')", uri.toASCIIString()); + } + + @Test + public void testCompositeKeysUri() throws EdmException { + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + Map<EdmProperty, Object> keyMap = new LinkedHashMap<EdmProperty,Object>(); + keyMap.put((EdmProperty) entitySet.getEntityType().getProperty("Id"), 4); + keyMap.put((EdmProperty) entitySet.getEntityType().getProperty("Type"), "foo"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment(keyMap). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos(Id=4,Type='foo')", uri.toASCIIString()); + } + + @Test + public void testFilterUri() throws EdmException { + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + filter("Name eq 'Photo 1'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$filter=Name%20eq%20'Photo%201'", uri.toASCIIString()); + } + + @Test + public void testTopUri1() throws EdmException { + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$top=2", uri.toASCIIString()); + } + + @Test + public void testTopUri2() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$filter=TeamId%20eq%20'1'&$top=2", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void negTestQueryOption() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet employeeEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)employeeEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$filter=TeamId%20eq%20'1'&$top=2", uri.toASCIIString()); + } + + @Test + public void testSkipUri() throws EdmException { + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + skip(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$skip=2", uri.toASCIIString()); + } + + @Test + public void testUriWithQueryOptions() throws EdmException { + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + filter("Name eq 'Photo 1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$filter=Name%20eq%20'Photo%201'&$top=2", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationSegment1() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("ne_Team")). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/ne_Team", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationSegment2() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet employeeEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)employeeEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)employeeEntitySet.getEntityType().getProperty("ne_Team")). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees('1')/ne_Team", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationSegment3() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + EdmEntitySet teamEntitySet = edm.getDefaultEntityContainer().getEntitySet("Teams"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("ne_Team")). + appendNavigationSegment((EdmNavigationProperty)teamEntitySet.getEntityType().getProperty("nt_Employees")). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/ne_Team/nt_Employees", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void negTest1UriWithNavigationSegment() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet employeeEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendNavigationSegment((EdmNavigationProperty)employeeEntitySet.getEntityType().getProperty("ne_Team")). + build(); + } + + @Test(expected=RuntimeException.class) + public void negTest2UriWithNavigationSegment() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("ne_Team")). + build(); + } + + @Test + public void testUriWithSimplePropertySegment() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendPropertySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeName"), "EmployeeName"). + appendValueSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/EmployeeName/$value", uri.toASCIIString()); + } + + @Test + public void testUriWithComplexPropertySegment() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendPropertySegment((EdmProperty)entitySet.getEntityType().getProperty("Location"), "Location"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/Location", uri.toASCIIString()); + } + + @Test + public void testUriWithComplexPropertySegment1() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + EdmComplexType complexType = edm.getComplexType("RefScenario", "c_Location"); + EdmProperty property = (EdmProperty) complexType.getProperty("City"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendPropertySegment((EdmProperty)entitySet.getEntityType().getProperty("Location"), "Location"). + appendPropertySegment(property, "City"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/Location/City", uri.toASCIIString()); + } + + @Test(expected = RuntimeException.class) + public void testUriWithComplexPropertySegmentWithValueSegment() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + EdmComplexType complexType = edm.getComplexType("RefScenario", "c_Location"); + EdmProperty property = (EdmProperty) complexType.getProperty("City"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendPropertySegment((EdmProperty)entitySet.getEntityType().getProperty("Location"), "Location"). + appendPropertySegment(property, "City"). + appendValueSegment(). + build(); + } + + @Test(expected = RuntimeException.class) + public void negTestUriWithFormat() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + EdmComplexType complexType = edm.getComplexType("RefScenario", "c_Location"); + EdmProperty property = (EdmProperty) complexType.getProperty("City"); + EdmComplexType complexType1 = (EdmComplexType) property.getType(); + EdmProperty property1 = (EdmProperty) complexType1.getProperty("CityName"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendPropertySegment((EdmProperty)entitySet.getEntityType().getProperty("Location"), "Location"). + appendPropertySegment(property, "City"). + appendPropertySegment(property1, "CityName"). + appendValueSegment(). + format("application/json"). + build(); + } + + @Test(expected=RuntimeException.class) + public void wrongESInUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employee"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + build(); + } + + @Test(expected = RuntimeException.class) + public void duplicateKeyPropertyInUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + build(); + } + + @Test(expected=RuntimeException.class) + public void duplicateKeyForNavPropertyInUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet empEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendKeySegment((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + build(); + } + + @Test + public void testNavigationToManyInUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet empEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + Map<EdmProperty, Object> keyMap = new HashMap<EdmProperty, Object>(); + keyMap.put((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeId"), "1"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment(keyMap). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees(EmployeeId='1')", uri.toASCIIString()); + } + + @Test + public void testUriWithOrderby() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + orderBy("EmployeeId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees?$orderby=EmployeeId", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void negTestUriWithOrderby() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + orderBy("EmployeeId"). + build(); + } + + @Test + public void testUriWithOrderbyAndFormat() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + orderBy("EmployeeId"). + format("application/json"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$orderby=EmployeeId&$format=application%2Fjson", uri.toASCIIString()); + } + + @Test + public void testUriWithOrderbyWithNullValue() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + try { + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + orderBy("EmployeeName"). + build(); + } catch(EdmException e) { + assertEquals("Property not defined.", e.getMessage()); + } + } + + @Test + public void testUriWithSelect() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$select=EmployeeId%2CEmployeeName%2CRoomId%2CTeamId", uri.toASCIIString()); + } + + @Test + public void testUriWithSelectAndFilter() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + filter("EmployeeId eq 1"). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees?$filter=" + + "EmployeeId%20eq%201&$select=EmployeeId%2CEmployeeName%2CRoomId%2CTeamId", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void testUriWithSelectAndCount() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendCountSegment(). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + build(); + } + + @Test + public void testUriWithSelectOnEntity() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')" + + "?$select=EmployeeId%2CEmployeeName%2CRoomId%2CTeamId", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void testUriWithSelectOnEntityWithTop() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + top(2). + build(); + } + + @Test + public void testUriWithExpand() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + expand("nm_Employees"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')?$expand=nm_Employees", uri.toASCIIString()); + } + + @Test + public void testUriWithExpandAndFilter() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + expand("nm_Employees"). + filter("EmployeeName eq 'Walter Winter'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$expand=nm_Employees&$filter=" + + "EmployeeName%20eq%20'Walter%20Winter'", uri.toASCIIString()); + } + + @Test(expected=RuntimeException.class) + public void testUriWithExpandAndCount() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendCountSegment(). + expand("nm_Employees"). + build(); + } + + @Test + public void testUriWithFilterAndExpand() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + filter("EmployeeName eq 'Walter Winter'"). + expand("nm_Employees"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$filter=EmployeeName%20eq%20'Walter%20Winter'" + + "&$expand=nm_Employees", uri.toASCIIString()); + } + + @Test + public void testUriWithCustomQueryOption() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + top(2). + addCustomQueryOption("x", "y"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$top=2&x=y", uri.toASCIIString()); + } + + @Test + public void testUriWithCustomQueryOptionWithFormat() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + top(2). + addCustomQueryOption("x", "y"). + format("application/json"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$top=2&$format=application%2Fjson&x=y", uri.toASCIIString()); + } + + @Test + public void testUriWithFilters() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + filter("EmployeeId ge '1' and EmployeeId le '10'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$filter=EmployeeId%20ge%20'1'%20" + + "and%20EmployeeId%20le%20'10'", uri.toASCIIString()); + } + + @Test + public void testUriWithDuplicateExpands() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + expand("nm_Employees"). + expand("nm_Employees"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$expand=nm_Employees%2Cnm_Employees", uri.toASCIIString()); + } + + @Test + public void testUriWithTwoCustomQueryOptions() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + top(2). + addCustomQueryOption("x", "y"). + addCustomQueryOption("z", "y"). + format("application/json"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$top=2&$" + + "format=application%2Fjson&x=y&z=y", uri.toASCIIString()); + } + + @Test + public void testUriWithOnlyCustomQueryOption() throws EdmException { + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI_1). + addCustomQueryOption("x", "y"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service?x=y", uri.toASCIIString()); + } + + @Test + public void testUriWithDuplicateOrderby() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + orderBy("EmployeeId"). + orderBy("EmployeeId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$orderby=EmployeeId%2CEmployeeId", uri.toASCIIString()); + } + + @Test + public void testUriWithTwoOrderby() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + orderBy("EmployeeId"). + orderBy("EmployeeName desc"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$orderby=EmployeeId%2CEmployeeName%20desc", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationToManyWithKeyWithSimpleProperty() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet empEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendPropertySegment((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeName"), "EmployeeName"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees('1')/EmployeeName", uri.toASCIIString()); + } + + @Test + public void negTestUriWithNavigationToManyWithSimpleProperty() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet empEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + try { + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendPropertySegment((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeName"), "EmployeeName"). + build(); + } catch (Exception e) { + assertEquals("Can't specify a property at this position", e.getMessage()); + } + } + + @Test + public void testSimpleESWithEncodedKeyUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty) entitySet.getEntityType().getProperty("EmployeeId"), "abc/def"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('abc%2Fdef')", uri.toASCIIString()); + } + + @Test + public void testCompositeKeysEncodedUri() throws EdmException { + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + Map<EdmProperty, Object> keyMap = new LinkedHashMap<EdmProperty,Object>(); + keyMap.put((EdmProperty) entitySet.getEntityType().getProperty("Id"), 4); + keyMap.put((EdmProperty) entitySet.getEntityType().getProperty("Type"), "foo,foo;"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment(keyMap). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos(Id=4,Type='foo%2Cfoo%3B')", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationToManyWithKeyEncoded() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + EdmEntitySet empEntitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1()*;"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + appendKeySegment((EdmProperty)empEntitySet.getEntityType().getProperty("EmployeeId"), "@#$%"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1%28%29%2A%3B')/nm_Employees('%40%23%24%25')", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithEmptyParams() throws EdmException { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("AllLocations"); + Map<EdmParameter, Object> functionImportParams = new HashMap<EdmParameter, Object>(); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/AllLocations", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithNullParams() throws EdmException { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("AllLocations"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendFunctionImportParameters(null).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/AllLocations", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithParams() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("EmployeeSearch"); + Map<EdmParameter, Object> functionImportParams = new HashMap<EdmParameter, Object>(); + EdmParameter param = edm.getDefaultEntityContainer().getFunctionImport("EmployeeSearch").getParameter("q"); + functionImportParams.put(param, "Emp1"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/EmployeeSearch?q='Emp1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithPathSegmentsAndParams() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("EmployeeSearch"); + Map<EdmParameter, Object> functionImportParams = new HashMap<EdmParameter, Object>(); + EdmParameter param = edm.getDefaultEntityContainer().getFunctionImport("EmployeeSearch").getParameter("q"); + functionImportParams.put(param, "Emp1"); + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + EdmProperty property = (EdmProperty) entitySet.getEntityType().getProperty("Location"); + + try { + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendPropertySegment(property, "Location"). + appendFunctionImportParameters(functionImportParams).build(); + } catch (RuntimeException e) { + assertEquals("Can't specify a property at this position", e.getMessage()); + } + } + + @Test + public void testUriWithFunctionImportWithKeyEncodedSegment() throws EdmException { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("EmployeeSearch"); + Map<EdmParameter, Object> functionImportParams = new HashMap<EdmParameter, Object>(); + EdmParameter param = edm.getDefaultEntityContainer().getFunctionImport("EmployeeSearch").getParameter("q"); + functionImportParams.put(param, "Emp1"); + Map<EdmProperty, Object> keySegParams = new HashMap<EdmProperty, Object>(); + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + keySegParams.put((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1()*;"); + + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport). + appendKeySegment(keySegParams). + appendFunctionImportParameters(functionImportParams). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/EmployeeSearch(EmployeeId='1%28%29%2A%3B')?q='Emp1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithParamsWithNullFacets() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("FINullableParameter"); + Map<EdmParameter, Object> functionImportParams = new HashMap<EdmParameter, Object>(); + EdmParameter param = edm.getDefaultEntityContainer().getFunctionImport("FINullableParameter").getParameter("Id"); + functionImportParams.put(param, "1"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/FINullableParameter?Id='1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithParamsWithFalseNullFacets() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("ManagerPhoto"); + Map<EdmParameter, Object> functionImportParams = new HashMap<EdmParameter, Object>(); + EdmParameter param = edm.getDefaultEntityContainer().getFunctionImport("FINullableParameter").getParameter("Id"); + functionImportParams.put(param, "1"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/ManagerPhoto?Id='1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParams() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch"); + Map<EdmParameter, Object> functionImportParams = new LinkedHashMap<EdmParameter, Object>(); + EdmParameter param1 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("q"); + functionImportParams.put(param1, "1"); + EdmParameter param2 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("r"); + functionImportParams.put(param2, 1); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParamsWithMoreSegments() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch"); + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Buildings"); + EdmProperty property = (EdmProperty) entitySet.getEntityType().getProperty("Id"); + EdmNavigationProperty navProperty = (EdmNavigationProperty) entitySet.getEntityType().getProperty("nb_Rooms"); + Map<EdmParameter, Object> functionImportParams = new LinkedHashMap<EdmParameter, Object>(); + EdmParameter param1 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("q"); + functionImportParams.put(param1, "1"); + EdmParameter param2 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("r"); + functionImportParams.put(param2, 1); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendKeySegment(property, "1"). + appendNavigationSegment(navProperty). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch('1')/nb_Rooms?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParamsWithCount() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch"); + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Buildings"); + EdmProperty property = (EdmProperty) entitySet.getEntityType().getProperty("Id"); + EdmNavigationProperty navProperty = (EdmNavigationProperty) entitySet.getEntityType().getProperty("nb_Rooms"); + Map<EdmParameter, Object> functionImportParams = new LinkedHashMap<EdmParameter, Object>(); + EdmParameter param1 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("q"); + functionImportParams.put(param1, "1"); + EdmParameter param2 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("r"); + functionImportParams.put(param2, 1); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendKeySegment(property, "1"). + appendNavigationSegment(navProperty). + appendCountSegment(). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch('1')/nb_Rooms/$count?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithCount() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch"); + Map<EdmParameter, Object> functionImportParams = new LinkedHashMap<EdmParameter, Object>(); + EdmParameter param1 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("q"); + functionImportParams.put(param1, "1"); + EdmParameter param2 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("r"); + functionImportParams.put(param2, 1); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport). + appendCountSegment(). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch/$count?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParamsWithPropertySegment() throws Exception { + EdmFunctionImport functionImport = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch"); + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Buildings"); + EdmProperty property = (EdmProperty) entitySet.getEntityType().getProperty("Id"); + EdmNavigationProperty navProperty = (EdmNavigationProperty) entitySet.getEntityType().getProperty("nb_Rooms"); + EdmEntitySet entitySet1 = edm.getDefaultEntityContainer().getEntitySet("Rooms"); + EdmProperty property1 = (EdmProperty) entitySet1.getEntityType().getProperty("Id"); + EdmProperty property2 = (EdmProperty) entitySet1.getEntityType().getProperty("Name"); + Map<EdmParameter, Object> functionImportParams = new LinkedHashMap<EdmParameter, Object>(); + EdmParameter param1 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("q"); + functionImportParams.put(param1, "1"); + EdmParameter param2 = edm.getDefaultEntityContainer().getFunctionImport("BuildingSearch").getParameter("r"); + functionImportParams.put(param2, 1); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport).appendKeySegment(property, "1"). + appendNavigationSegment(navProperty).appendKeySegment(property1, "1"). + appendPropertySegment(property2, "Name"). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch('1')/nb_Rooms('1')/Name?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithKeySegment() throws Exception { + EdmFunctionImport functionImport = edm.getEntityContainer("Container2").getFunctionImport("PhotoSearch"); + EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos"); + EdmProperty property = (EdmProperty) entitySet.getEntityType().getProperty("Id"); + EdmProperty property1 = (EdmProperty) entitySet.getEntityType().getProperty("Type"); + Map<EdmProperty, Object> keySegments = new LinkedHashMap<EdmProperty, Object>(); + keySegments.put(property, "1"); + keySegments.put(property1, "Internal"); + Map<EdmParameter, Object> functionImportParams = new LinkedHashMap<EdmParameter, Object>(); + EdmParameter param1 = edm.getEntityContainer("Container2").getFunctionImport("PhotoSearch").getParameter("Id"); + functionImportParams.put(param1, 1); + EdmParameter param2 = edm.getEntityContainer("Container2").getFunctionImport("PhotoSearch").getParameter("Type"); + functionImportParams.put(param2, "Internal"); + try { + new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment(functionImport). + appendKeySegment(keySegments). + appendFunctionImportParameters(functionImportParams).build(); + } catch (RuntimeException e) { + assertEquals(e.getMessage(), "Can't specify a key at this position"); + } + } + + @Test + public void testCustomQueryWithSystemQuery() throws Exception { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI).appendEntitySetSegment(entitySet). + addCustomQueryOption("x", "y").filter("EmployeeName eq 'Walter Winter'").build(); + assertEquals("http://host:80/service/Managers?$filter=" + + "EmployeeName%20eq%20'Walter%20Winter'&x=y", uri.toASCIIString()); + } + + @Test + public void testServiceDocument() throws Exception { + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI).build(); + assertEquals("http://host:80/service/", uri.toASCIIString()); + } + + @Test + public void testServiceDocument1() throws Exception { + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI_1).build(); + assertEquals("http://host:80/service/", uri.toASCIIString()); + } + + @Test + public void addSameFilterOptionTwice() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Managers"); + URI uri = new EdmURIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("nm_Employees")). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$filter=TeamId%20eq%20'1'%2CTeamId%20eq%20'1'&$top=2", uri.toASCIIString()); + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4261deb7/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderIntegrationTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderIntegrationTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderIntegrationTest.java new file mode 100644 index 0000000..363ebaf --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderIntegrationTest.java @@ -0,0 +1,51 @@ +package org.apache.olingo.odata2.client.core.uri; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.URI; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.edm.EdmNavigationProperty; +import org.apache.olingo.odata2.api.edm.EdmProperty; +import org.apache.olingo.odata2.api.exception.ODataException; +import org.apache.olingo.odata2.client.api.ODataClient; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.junit.Before; +import org.junit.Test; + +public class UriBuilderIntegrationTest { + + protected static final String SERVICE_ROOT_URI = "http://host:80/service/"; + private Edm edm; + + @Before + public void getEdm() throws ODataException { + edm = MockFacade.getMockEdm(); + } + + @Test + public void constructEdmUri() throws EdmException { + EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Employees"); + URI uri = ODataClient.newInstance().edmUriBuilder(SERVICE_ROOT_URI). + appendEntitySetSegment(entitySet). + appendKeySegment((EdmProperty)entitySet.getEntityType().getProperty("EmployeeId"), "1"). + appendNavigationSegment((EdmNavigationProperty)entitySet.getEntityType().getProperty("ne_Team")). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/ne_Team", uri.toASCIIString()); + } + + @Test + public void constructUri() { + URI uri = ODataClient.newInstance().uriBuilder(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment("1"). + appendNavigationSegment("ne_Team"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/ne_Team", uri.toASCIIString()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4261deb7/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderTest.java new file mode 100644 index 0000000..c9c239e --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriBuilderTest.java @@ -0,0 +1,586 @@ +package org.apache.olingo.odata2.client.core.uri; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.URI; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.client.api.uri.QueryOption; +import org.junit.Test; + +public class UriBuilderTest { + protected static final String SERVICE_ROOT_URI = "http://host:80/service/"; + protected static final String SERVICE_ROOT_URI_1 = "http://host:80/service"; + + @Test + public void testUriSimpleES() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithCountUri1() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendCountSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees/$count", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithCountUri2() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + appendCountSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees/$count", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithCountAndFilter() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + appendCountSegment(). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees/" + + "$count?$filter=TeamId%20eq%20'1'", uri.toASCIIString()); + } + + @Test + public void testMetadataUri() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendMetadataSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/$metadata", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithKeyUri() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment("1"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithKeyUri1() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment(1). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees(1)", uri.toASCIIString()); + } + + @Test + public void testCompositeKeysUri() { + Map<String, Object> keyMap = new LinkedHashMap<String,Object>(); + keyMap.put("Id", 4); + keyMap.put("Type", "foo"); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Photos"). + appendKeySegment(keyMap). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos(Id=4,Type='foo')", uri.toASCIIString()); + } + + @Test + public void testFilterUri() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Photos"). + filter("Name eq 'Photo 1'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$filter=Name%20eq%20'Photo%201'", uri.toASCIIString()); + } + + @Test + public void testTopUri1() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Photos"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$top=2", uri.toASCIIString()); + } + + @Test + public void testTopUri2() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$filter=TeamId%20eq%20'1'&$top=2", uri.toASCIIString()); + } + + @Test + public void testSkipUri() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Photos"). + skip(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$skip=2", uri.toASCIIString()); + } + + @Test + public void testUriWithQueryOptions() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Photos"). + filter("Name eq 'Photo 1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos?$filter=Name%20eq%20'Photo%201'&$top=2", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationSegment2() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + appendKeySegment("1"). + appendNavigationSegment("ne_Team"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees('1')/ne_Team", uri.toASCIIString()); + } + @Test + public void testUriWithSimplePropertySegment() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment("1"). + appendPropertySegment("EmployeeName"). + appendValueSegment(). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/EmployeeName/$value", uri.toASCIIString()); + } + + @Test + public void testUriWithComplexPropertySegment() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment("1"). + appendPropertySegment("Location"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/Location", uri.toASCIIString()); + } + + @Test + public void testUriWithComplexPropertySegment1() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment("1"). + appendPropertySegment("Location"). + appendPropertySegment("City"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('1')/Location/City", uri.toASCIIString()); + } + + @Test + public void testUriWithOrderby() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + orderBy("EmployeeId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees?$orderby=EmployeeId", uri.toASCIIString()); + } + + @Test + public void testUriWithOrderbyAndFormat() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + orderBy("EmployeeId"). + format("application/json"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$orderby=EmployeeId&$format=application%2Fjson", uri.toASCIIString()); + } + + @Test + public void testUriWithSelect() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$select=EmployeeId%2CEmployeeName%2CRoomId%2CTeamId", uri.toASCIIString()); + } + + @Test + public void testUriWithSelectAndFilter() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + filter("EmployeeId eq 1"). + select("EmployeeId", "EmployeeName", "RoomId", "TeamId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees?$filter=" + + "EmployeeId%20eq%201&$select=EmployeeId%2CEmployeeName%2CRoomId%2CTeamId", uri.toASCIIString()); + } + + @Test + public void testUriWithExpand() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + expand("nm_Employees"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')?$expand=nm_Employees", uri.toASCIIString()); + } + + @Test + public void testUriWithExpandAndFilter() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + expand("nm_Employees"). + filter("EmployeeName eq 'Walter Winter'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$expand=nm_Employees&$filter=" + + "EmployeeName%20eq%20'Walter%20Winter'", uri.toASCIIString()); + } + + @Test + public void testUriWithCustomQueryOption() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + top(2). + addCustomQueryOption("x", "y"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$top=2&x=y", uri.toASCIIString()); + } + + @Test + public void testUriWithCustomQueryOptionWithFormat() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + top(2). + addCustomQueryOption("x", "y"). + format("application/json"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$top=2&$format=application%2Fjson&x=y", uri.toASCIIString()); + } + + @Test + public void testUriWithFilters() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + filter("EmployeeId ge '1' and EmployeeId le '10'"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$filter=EmployeeId%20ge%20'1'%20" + + "and%20EmployeeId%20le%20'10'", uri.toASCIIString()); + } + + @Test + public void testUriWithDuplicateExpands() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + expand("nm_Employees"). + expand("nm_Employees"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$expand=nm_Employees%2Cnm_Employees", uri.toASCIIString()); + } + + @Test + public void testUriWithTwoCustomQueryOptions() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + top(2). + addCustomQueryOption("x", "y"). + addCustomQueryOption("z", "y"). + format("application/json"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$top=2&$" + + "format=application%2Fjson&x=y&z=y", uri.toASCIIString()); + } + + @Test + public void testUriWithDuplicateOrderby() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + orderBy("EmployeeId"). + orderBy("EmployeeId"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers?$orderby=EmployeeId%2CEmployeeId", uri.toASCIIString()); + } + + @Test + public void testUriWithTwoOrderby() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + orderBy("EmployeeId"). + orderBy("EmployeeName desc"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees?$orderby=EmployeeId%2CEmployeeName%20desc", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationToManyWithKeyWithSimpleProperty() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + appendKeySegment("1"). + appendPropertySegment("EmployeeName"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees('1')/EmployeeName", uri.toASCIIString()); + } + + @Test + public void testSimpleESWithEncodedKeyUri() { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Employees"). + appendKeySegment("abc/def"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Employees('abc%2Fdef')", uri.toASCIIString()); + } + + @Test + public void testCompositeKeysEncodedUri() { + Map<String, Object> keyMap = new LinkedHashMap<String,Object>(); + keyMap.put("Id", 4); + keyMap.put("Type", "foo,foo;"); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Photos"). + appendKeySegment(keyMap). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Photos(Id=4,Type='foo%2Cfoo%3B')", uri.toASCIIString()); + } + + @Test + public void testUriWithNavigationToManyWithKeyEncoded() throws EdmException { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1()*;"). + appendNavigationSegment("nm_Employees"). + appendKeySegment("@#$%"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1%28%29%2A%3B')/nm_Employees('%40%23%24%25')", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithEmptyParams() throws EdmException { + Map<String, Object> functionImportParams = new HashMap<String, Object>(); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("AllLocations").appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/AllLocations", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithNullParams() throws EdmException { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("AllLocations").appendFunctionImportParameters(null).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/AllLocations", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithParams() throws Exception { + Map<String, Object> functionImportParams = new HashMap<String, Object>(); + functionImportParams.put("q", "Emp1"); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("EmployeeSearch").appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/EmployeeSearch?q='Emp1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithParamsWithNullFacets() throws Exception { + Map<String, Object> functionImportParams = new HashMap<String, Object>(); + functionImportParams.put("Id", "1"); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("FINullableParameter").appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/FINullableParameter?Id='1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithParamsWithFalseNullFacets() throws Exception { + Map<String, Object> functionImportParams = new HashMap<String, Object>(); + functionImportParams.put("Id", "1"); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("ManagerPhoto").appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/ManagerPhoto?Id='1'", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParams() throws Exception { + Map<String, Object> functionImportParams = new LinkedHashMap<String, Object>(); + functionImportParams.put("q", "1"); + functionImportParams.put("r", 1); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("BuildingSearch").appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParamsWithMoreSegments() throws Exception { + Map<String, Object> functionImportParams = new LinkedHashMap<String, Object>(); + functionImportParams.put("q", "1"); + functionImportParams.put("r", 1); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("BuildingSearch").appendKeySegment("1"). + appendNavigationSegment("nb_Rooms"). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch('1')/nb_Rooms?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParamsWithCount() throws Exception { + Map<String, Object> functionImportParams = new LinkedHashMap<String, Object>(); + functionImportParams.put("q", "1"); + functionImportParams.put("r", 1); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("BuildingSearch").appendKeySegment("1"). + appendNavigationSegment("nb_Rooms"). + appendCountSegment(). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch('1')/nb_Rooms/$count?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithCount() throws Exception { + Map<String, Object> functionImportParams = new LinkedHashMap<String, Object>(); + functionImportParams.put("q", "1"); + functionImportParams.put("r", 1); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("BuildingSearch"). + appendCountSegment(). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch/$count?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithMultipleParamsWithPropertySegment() throws Exception { + Map<String, Object> functionImportParams = new LinkedHashMap<String, Object>(); + functionImportParams.put("q", "1"); + functionImportParams.put("r", 1); + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("BuildingSearch").appendKeySegment("1"). + appendNavigationSegment("nb_Rooms").appendKeySegment("1"). + appendPropertySegment("Name"). + appendFunctionImportParameters(functionImportParams).build(); + assertNotNull(uri); + assertEquals("http://host:80/service/BuildingSearch('1')/nb_Rooms('1')/Name?q='1'&r=1", uri.toASCIIString()); + } + + @Test + public void testFunctionImportWithKeySegment() throws Exception { + Map<String, Object> keySegments = new LinkedHashMap<String, Object>(); + keySegments.put("Id", "1"); + keySegments.put("Type", "Internal"); + Map<String, Object> functionImportParams = new LinkedHashMap<String, Object>(); + functionImportParams.put("Id", 1); + functionImportParams.put("Type", "Internal"); + try { + new URIBuilderImpl(SERVICE_ROOT_URI). + appendFunctionImportSegment("PhotoSearch"). + appendKeySegment(keySegments). + appendFunctionImportParameters(functionImportParams).build(); + } catch (RuntimeException e) { + assertEquals(e.getMessage(), "Can't specify a key at this position"); + } + } + + @Test + public void testCustomQueryWithSystemQuery() throws Exception { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI).appendEntitySetSegment("Managers"). + addCustomQueryOption("x", "y").filter("EmployeeName eq 'Walter Winter'").build(); + assertEquals("http://host:80/service/Managers?$filter=" + + "EmployeeName%20eq%20'Walter%20Winter'&x=y", uri.toASCIIString()); + } + + @Test + public void testUriWithOnlyCustomQueryOption() throws EdmException { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI_1). + addCustomQueryOption("x", "y"). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service?x=y", uri.toASCIIString()); + } + + @Test + public void testServiceDocument() throws Exception { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI).build(); + assertEquals("http://host:80/service/", uri.toASCIIString()); + } + + @Test + public void testServiceDocument1() throws Exception { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI_1).build(); + assertEquals("http://host:80/service/", uri.toASCIIString()); + } + + @Test + public void addSameFilterOptionTwice() throws EdmException { + URI uri = new URIBuilderImpl(SERVICE_ROOT_URI). + appendEntitySetSegment("Managers"). + appendKeySegment("1"). + appendNavigationSegment("nm_Employees"). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + addQueryOption(QueryOption.FILTER, "TeamId eq '1'"). + top(2). + build(); + assertNotNull(uri); + assertEquals("http://host:80/service/Managers('1')/nm_Employees" + + "?$filter=TeamId%20eq%20'1'%2CTeamId%20eq%20'1'&$top=2", uri.toASCIIString()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4261deb7/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriInfoTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriInfoTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriInfoTest.java new file mode 100644 index 0000000..e515206 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/uri/UriInfoTest.java @@ -0,0 +1,168 @@ +package org.apache.olingo.odata2.client.core.uri; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.exception.ODataException; +import org.apache.olingo.odata2.api.uri.PathSegment; +import org.apache.olingo.odata2.api.uri.UriInfo; +import org.apache.olingo.odata2.api.uri.UriNotMatchingException; +import org.apache.olingo.odata2.api.uri.UriSyntaxException; +import org.apache.olingo.odata2.client.api.ODataClient; +import org.apache.olingo.odata2.core.ODataPathSegmentImpl; +import org.apache.olingo.odata2.core.uri.UriInfoImpl; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UriInfoTest { + + private static Edm edm; + + @BeforeClass + public static void getEdm() throws ODataException, EdmException { + edm = MockFacade.getMockEdm(); + } + + @Test + public void everythingInitial() throws Exception { + UriInfoImpl result = parse("/"); + + assertEquals(Collections.emptyList(), result.getKeyPredicates()); + assertEquals(Collections.emptyList(), result.getTargetKeyPredicates()); + assertEquals(Collections.emptyList(), result.getNavigationSegments()); + assertEquals(Collections.emptyList(), result.getPropertyPath()); + assertEquals(Collections.emptyList(), result.getExpand()); + assertEquals(Collections.emptyList(), result.getSelect()); + assertEquals(Collections.emptyMap(), result.getFunctionImportParameters()); + assertEquals(Collections.emptyMap(), result.getCustomQueryOptions()); + assertNull(result.getEntityContainer()); + assertNull(result.getStartEntitySet()); + assertNull(result.getTargetEntitySet()); + assertNull(result.getFunctionImport()); + assertNull(result.getTargetType()); + assertNull(result.getFormat()); + assertNull(result.getFilter()); + assertNull(result.getInlineCount()); + assertNull(result.getOrderBy()); + assertNull(result.getSkipToken()); + assertNull(result.getSkip()); + assertNull(result.getTop()); + } + + @Test + public void allInitial() throws Exception { + UriInfoImpl result = parse("/Employees"); + + assertEquals(Collections.emptyList(), result.getKeyPredicates()); + assertEquals(Collections.emptyList(), result.getTargetKeyPredicates()); + assertEquals(Collections.emptyList(), result.getNavigationSegments()); + assertEquals(Collections.emptyList(), result.getPropertyPath()); + assertEquals(Collections.emptyList(), result.getExpand()); + assertEquals(Collections.emptyList(), result.getSelect()); + assertEquals(Collections.emptyMap(), result.getFunctionImportParameters()); + assertEquals(Collections.emptyMap(), result.getCustomQueryOptions()); + } + + @Test + public void someInitial() throws Exception { + UriInfoImpl result = parse("/Employees('1')"); + + assertNotSame(Collections.emptyList(), result.getKeyPredicates()); + assertNotSame(Collections.emptyList(), result.getTargetKeyPredicates()); + + assertEquals(Collections.emptyList(), result.getNavigationSegments()); + assertEquals(Collections.emptyList(), result.getPropertyPath()); + assertEquals(Collections.emptyList(), result.getExpand()); + assertEquals(Collections.emptyList(), result.getSelect()); + assertEquals(Collections.emptyMap(), result.getFunctionImportParameters()); + assertEquals(Collections.emptyMap(), result.getCustomQueryOptions()); + } + + @Test + public void someInitial2() throws Exception { + UriInfoImpl result = parse("/Employees('1')/ne_Manager"); + + assertNotSame(Collections.emptyList(), result.getKeyPredicates()); + assertNotSame(Collections.emptyList(), result.getNavigationSegments()); + + assertEquals(Collections.emptyList(), result.getTargetKeyPredicates()); + assertEquals(Collections.emptyList(), result.getPropertyPath()); + assertEquals(Collections.emptyList(), result.getExpand()); + assertEquals(Collections.emptyList(), result.getSelect()); + assertEquals(Collections.emptyMap(), result.getFunctionImportParameters()); + assertEquals(Collections.emptyMap(), result.getCustomQueryOptions()); + } + + /** + * Parse the URI part after an OData service root, given as string. + * Query parameters can be included. + * @param uri the URI part + * @return a {@link UriInfoImpl} instance containing the parsed information + */ + private UriInfoImpl parse(final String uri) throws UriSyntaxException, UriNotMatchingException, EdmException { + final String[] path = uri.split("\\?", -1); + if (path.length > 2) { + throw new UriSyntaxException(UriSyntaxException.URISYNTAX); + } + + final List<PathSegment> pathSegments = getPathSegments(path[0]); + Map<String, List<String>> queryParameters; + if (path.length == 2) { + queryParameters = getQueryParameters(unescape(path[1])); + } else { + queryParameters = new HashMap<String, List<String>>(); + } + + UriInfo result = ODataClient.newInstance().parseUri(edm, pathSegments, queryParameters); + + return (UriInfoImpl) result; + } + + private List<PathSegment> getPathSegments(final String uri) throws UriSyntaxException { + List<PathSegment> pathSegments = new ArrayList<PathSegment>(); + for (final String segment : uri.split("/", -1)) { + final String unescapedSegment = unescape(segment); + PathSegment oDataSegment = new ODataPathSegmentImpl(unescapedSegment, null); + pathSegments.add(oDataSegment); + } + return pathSegments; + } + + private Map<String, List<String>> getQueryParameters(final String uri) { + Map<String, List<String>> allQueryParameters = new HashMap<String, List<String>>(); + + for (final String option : uri.split("&")) { + final String[] keyAndValue = option.split("="); + List<String> list = allQueryParameters.containsKey(keyAndValue[0]) ? + allQueryParameters.get(keyAndValue[0]) : new LinkedList<String>(); + + list.add(keyAndValue.length == 2 ? keyAndValue[1] : ""); + + allQueryParameters.put(keyAndValue[0], list); + } + + return allQueryParameters; + } + + private String unescape(final String s) throws UriSyntaxException { + try { + return new URI(s).getPath(); + } catch (URISyntaxException e) { + throw new UriSyntaxException(UriSyntaxException.NOTEXT); + } + } + +}