private void wrongProperty( final ODataHttpMethod method, final boolean ofComplex, final boolean key, final boolean nullable) { EdmProperty property = null; try { if (ofComplex) { property = (EdmProperty) edm.getEntityType("RefScenario", "Employee").getProperty("Age"); } else { property = (EdmProperty) edm.getComplexType("RefScenario", "c_Location").getProperty("Country"); } EdmFacets facets = mock(EdmFacets.class); when(facets.isNullable()).thenReturn(nullable); when(property.getFacets()).thenReturn(facets); } catch (final EdmException e) { fail(); } List<PathSegment> pathSegments = new ArrayList<PathSegment>(); pathSegments.add(mockPathSegment("Employees('1')")); if (ofComplex) { pathSegments.add(mockPathSegment("Location")); } if (ofComplex) { pathSegments.add(mockPathSegment("Country")); } else if (key) { pathSegments.add(mockPathSegment("EmployeeId")); } else { pathSegments.add(mockPathSegment("Age")); } wrongRequest(method, pathSegments, null); }
@Test public void serviceDocumentEmpty() throws Exception { Edm edm = mock(Edm.class); EdmServiceMetadata metadata = mock(EdmServiceMetadata.class); when(edm.getServiceMetadata()).thenReturn(metadata); final ODataResponse response = new JsonEntityProvider().writeServiceDocument(edm, "http://host:80/service/"); assertNotNull(response); assertNotNull(response.getEntity()); assertEquals(HttpContentType.APPLICATION_JSON, response.getContentHeader()); assertEquals(ODataServiceVersion.V10, response.getHeader(ODataHttpHeaders.DATASERVICEVERSION)); final String json = StringHelper.inputStreamToString((InputStream) response.getEntity()); assertNotNull(json); assertEquals("{\"d\":{\"EntitySets\":[]}}", json); }
@Test public void serviceDocument() throws Exception { Edm edm = mock(Edm.class); EdmServiceMetadata metadata = mock(EdmServiceMetadata.class); EdmEntitySetInfo entitySetInfo1 = mock(EdmEntitySetInfo.class); when(entitySetInfo1.getEntitySetUri()).thenReturn(URI.create("EntitySet")); EdmEntitySetInfo entitySetInfo2 = mock(EdmEntitySetInfo.class); when(entitySetInfo2.getEntitySetUri()).thenReturn(URI.create("Container2.EntitySet2")); when(metadata.getEntitySetInfos()).thenReturn(Arrays.asList(entitySetInfo1, entitySetInfo2)); when(edm.getServiceMetadata()).thenReturn(metadata); final ODataResponse response = new JsonEntityProvider().writeServiceDocument(edm, "http://host:80/service/"); assertNotNull(response); assertNotNull(response.getEntity()); assertEquals(HttpContentType.APPLICATION_JSON, response.getContentHeader()); assertEquals(ODataServiceVersion.V10, response.getHeader(ODataHttpHeaders.DATASERVICEVERSION)); final String json = StringHelper.inputStreamToString((InputStream) response.getEntity()); assertNotNull(json); assertEquals("{\"d\":{\"EntitySets\":[\"EntitySet\",\"Container2.EntitySet2\"]}}", json); }
@Test public void wrongRequestContentType() throws Exception { wrongRequestContentType(ODataHttpMethod.PUT, UriType.URI2, ContentType.APPLICATION_ATOM_SVC); wrongRequestContentType( ODataHttpMethod.PUT, UriType.URI2, ContentType.APPLICATION_ATOM_SVC_CS_UTF_8); wrongRequestContentType(ODataHttpMethod.PUT, UriType.URI2, ContentType.APPLICATION_ATOM_SVC); wrongRequestContentType( ODataHttpMethod.PUT, UriType.URI2, ContentType.APPLICATION_ATOM_SVC_CS_UTF_8); ODataHttpMethod[] methodsToTest = { ODataHttpMethod.PUT, ODataHttpMethod.PATCH, ODataHttpMethod.MERGE }; for (ODataHttpMethod oDataHttpMethod : methodsToTest) { wrongRequestContentType( oDataHttpMethod, UriType.URI2, false, ContentType.create("image/jpeg")); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_ATOM_SVC); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_ATOM_SVC_CS_UTF_8); wrongRequestContentType(oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_XML); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_XML_CS_UTF_8); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_ATOM_XML); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_ATOM_XML_CS_UTF_8); wrongRequestContentType(oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_JSON); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.APPLICATION_JSON_CS_UTF_8); wrongRequestContentType( oDataHttpMethod, UriType.URI5, true, ContentType.create("image/jpeg")); } EdmEntityType entityType = edm.getDefaultEntityContainer().getEntitySet("Employees").getEntityType(); when(entityType.hasStream()).thenReturn(false); wrongRequestContentType(ODataHttpMethod.POST, UriType.URI1, ContentType.APPLICATION_ATOM_SVC); wrongRequestContentType( ODataHttpMethod.POST, UriType.URI1, ContentType.APPLICATION_ATOM_SVC_CS_UTF_8); }