ContextURLBuilder { public static URI create(final ContextURL contextURL) { StringBuilder result = new StringBuilder(); if (contextURL.getServiceRoot() != null) { result.append(contextURL.getServiceRoot()); } else if (contextURL.getODataPath() != null) { String oDataPath = contextURL.getODataPath(); char[] chars = oDataPath.toCharArray(); for (int i = 1; i < chars.length - 1; i++) { if (chars[i] == '/' && chars[i - 1] != '/') { result.append("../"); } } } result.append(Constants.METADATA); if (contextURL.getEntitySetOrSingletonOrType() != null) { result.append('#'); if (contextURL.isCollection()) { result.append("Collection(") .append(Encoder.encode(contextURL.getEntitySetOrSingletonOrType())) .append(")"); } else { result.append(Encoder.encode(contextURL.getEntitySetOrSingletonOrType())); } } if (contextURL.getDerivedEntity() != null) { if (contextURL.getEntitySetOrSingletonOrType() == null) { throw new IllegalArgumentException("ContextURL: Derived Type without anything to derive from!"); } result.append('/').append(Encoder.encode(contextURL.getDerivedEntity())); } if (contextURL.getKeyPath() != null) { result.append('(').append(contextURL.getKeyPath()).append(')'); } if (contextURL.getNavOrPropertyPath() != null) { if (contextURL.getServiceRoot() == null || !contextURL.getServiceRoot().isAbsolute()) { String[] paths = contextURL.getNavOrPropertyPath().split("/"); for (String path : paths) { result.insert(0, "../"); } } result.append('/').append(contextURL.getNavOrPropertyPath()); } if (contextURL.getSelectList() != null) { result.append('(').append(contextURL.getSelectList()).append(')'); } if (contextURL.isReference()) { if (contextURL.getServiceRoot() == null || !contextURL.getServiceRoot().isAbsolute()) { result.insert(0, "../"); } if (contextURL.getEntitySetOrSingletonOrType() != null) { throw new IllegalArgumentException("ContextURL: $ref with Entity Set"); } if (contextURL.isCollection()) { result.append('#') .append("Collection(") .append(ContextURL.Suffix.REFERENCE.getRepresentation()) .append(")"); } else { result.append('#').append(ContextURL.Suffix.REFERENCE.getRepresentation()); } } else if (contextURL.getSuffix() != null) { if (contextURL.getEntitySetOrSingletonOrType() == null) { throw new IllegalArgumentException("ContextURL: Suffix without preceding Entity Set!"); } result.append('/').append(contextURL.getSuffix().getRepresentation()); } return URI.create(result.toString()); } private  ContextURLBuilder(); static URI create(final ContextURL contextURL);  }
@Test public void buildWithComplexDerivedTypeInSelect1() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("ESCompCollDerived"); ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .selectList("PropertyCompAno/olingo.odata.test1.CTBaseAno/AdditionalPropString") .build(); assertEquals(serviceRoot + "$metadata#ESCompCollDerived(PropertyCompAno/" + "olingo.odata.test1.CTBaseAno/AdditionalPropString)", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildWithComplexDerivedTypeInSelect2() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("ESCompCollComp"); ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .selectList("PropertyComp/CollPropertyComp/olingo.odata.test1.CTBase/AdditionalPropString") .build(); assertEquals(serviceRoot + "$metadata#ESCompCollComp(PropertyComp/CollPropertyComp/" + "olingo.odata.test1.CTBase/AdditionalPropString)", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildWithNavPropertyInSelect() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("ESTwoKeyNav"); ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .selectList("CollPropertyCompNav/NavPropertyETTwoKeyNavMany") .build(); assertEquals(serviceRoot + "$metadata#ESTwoKeyNav(CollPropertyCompNav/NavPropertyETTwoKeyNavMany)", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildServiceDocument() { final ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot).build(); assertEquals(serviceRoot + "$metadata", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildRelative() { final ContextURL contextURL = ContextURL.with().build(); assertEquals("$metadata", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildEntitySet() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("Customers"); final ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .build(); assertEquals(serviceRoot + "$metadata#Customers", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildDerivedEntitySet() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("Customers"); EdmEntityType derivedType = Mockito.mock(EdmEntityType.class); Mockito.when(derivedType.getFullQualifiedName()).thenReturn(new FullQualifiedName("Model", "VipCustomer")); final ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .derived(derivedType) .build(); assertEquals(serviceRoot + "$metadata#Customers/Model.VipCustomer", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildEntitySetWithEntitySuffix() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("Customers"); final ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .suffix(Suffix.ENTITY) .build(); assertEquals(serviceRoot + "$metadata#Customers/$entity", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildEntity() { EdmEntityType entityType = mock(EdmEntityType.class); when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("namespace", "entityType")); ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .type(entityType) .build(); assertEquals(serviceRoot + "$metadata#namespace.entityType", ContextURLBuilder.create(contextURL).toASCIIString()); contextURL = ContextURL.with().serviceRoot(serviceRoot) .type(entityType) .asCollection() .build(); assertEquals(serviceRoot + "$metadata#Collection(namespace.entityType)", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test(expected = IllegalArgumentException.class) public void buildDerivedEntitySetWithoutEntitySet() { EdmEntityType derivedType = Mockito.mock(EdmEntityType.class); Mockito.when(derivedType.getFullQualifiedName()).thenReturn(new FullQualifiedName("Model", "VipCustomer")); ContextURLBuilder.create(ContextURL.with().derived(derivedType).build()); }
@Test public void buildDerivedEntity() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("Customers"); EdmEntityType derivedType = Mockito.mock(EdmEntityType.class); Mockito.when(derivedType.getFullQualifiedName()).thenReturn(new FullQualifiedName("Model", "VipCustomer")); final ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .derived(derivedType) .suffix(Suffix.ENTITY) .build(); assertEquals(serviceRoot + "$metadata#Customers/Model.VipCustomer/$entity", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildProperty() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("Customers"); ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .keyPath("1") .navOrPropertyPath("Name") .build(); assertEquals(serviceRoot + "$metadata#Customers(1)/Name", ContextURLBuilder.create(contextURL).toASCIIString()); contextURL = ContextURL.with().serviceRoot(serviceRoot) .entitySet(entitySet) .keyPath("one=1,two='two'") .navOrPropertyPath("ComplexName") .selectList("Part1") .build(); assertEquals(serviceRoot + "$metadata#Customers(one=1,two='two')/ComplexName(Part1)", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test public void buildPrimitiveType() { ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .type(OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)) .build(); assertEquals(serviceRoot + "$metadata#Edm.String", ContextURLBuilder.create(contextURL).toASCIIString()); contextURL = ContextURL.with().serviceRoot(serviceRoot) .type(OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String)).asCollection() .build(); assertEquals(serviceRoot + "$metadata#Collection(Edm.String)", ContextURLBuilder.create(contextURL).toString()); }
@Test public void buildComplexType() throws Exception { EdmComplexType baseType = mock(EdmComplexType.class); when(baseType.getFullQualifiedName()).thenReturn(new FullQualifiedName("namespace", "BaseTypeName")); ContextURL contextURL = ContextURL.with().serviceRoot(serviceRoot) .type(baseType) .build(); assertEquals(serviceRoot + "$metadata#namespace.BaseTypeName", ContextURLBuilder.create(contextURL).toASCIIString()); contextURL = ContextURL.with().serviceRoot(serviceRoot) .type(baseType) .asCollection() .build(); assertEquals(serviceRoot + "$metadata#Collection(namespace.BaseTypeName)", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test(expected = IllegalArgumentException.class) public void buildSuffixWithoutEntitySet() { ContextURLBuilder.create(ContextURL.with().suffix(Suffix.ENTITY).build()); }
@Test public void buildReference() { final ContextURL contextURL = ContextURL.with().suffix(Suffix.REFERENCE).build(); assertEquals("../$metadata#$ref", ContextURLBuilder.create(contextURL).toASCIIString()); }
@Test(expected = IllegalArgumentException.class) public void buildReferenceWithEntitySet() { EdmEntitySet entitySet = mock(EdmEntitySet.class); when(entitySet.getName()).thenReturn("Customers"); ContextURLBuilder.create(ContextURL.with().entitySet(entitySet).suffix(Suffix.REFERENCE).build()); }
@Test public void buildWithCharactersToBeEscaped() { EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); Mockito.when(entitySet.getName()).thenReturn("Entitäten"); EdmEntityType derivedType = Mockito.mock(EdmEntityType.class); Mockito.when(derivedType.getFullQualifiedName()).thenReturn( new FullQualifiedName("Namensräumchen", "UnüblicherName")); final ContextURL contextURL = ContextURL.with().entitySet(entitySet).derived(derivedType).build(); assertEquals("$metadata#Entit%C3%A4ten/Namensr%C3%A4umchen.Un%C3%BCblicherName", ContextURLBuilder.create(contextURL).toString()); }