blob: 39ed4cede64d15df1765fd82410807c47eeac455 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.fit.tecsvc.client;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.client.api.communication.ODataClientErrorException;
import org.apache.olingo.client.api.communication.ODataServerErrorException;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest;
import org.apache.olingo.client.api.communication.request.retrieve.EdmMetadataRequest;
import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
import org.apache.olingo.client.api.communication.request.retrieve.ODataEntitySetRequest;
import org.apache.olingo.client.api.communication.request.retrieve.ODataServiceDocumentRequest;
import org.apache.olingo.client.api.communication.request.retrieve.XMLMetadataRequest;
import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse;
import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse;
import org.apache.olingo.client.api.edm.xml.XMLMetadata;
import org.apache.olingo.client.api.edm.xml.v4.Reference;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.client.core.ODataClientFactory;
import org.apache.olingo.commons.api.domain.ODataError;
import org.apache.olingo.commons.api.domain.ODataServiceDocument;
import org.apache.olingo.commons.api.domain.v4.ODataAnnotation;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataEntitySet;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.domain.v4.ODataValue;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.fit.AbstractBaseTestITCase;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Test;
public class BasicITCase extends AbstractBaseTestITCase {
private static final String SERVICE_URI = TecSvcConst.BASE_URI;
@Test
public void readServiceDocument() {
ODataServiceDocumentRequest request = getClient().getRetrieveRequestFactory()
.getServiceDocumentRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<ODataServiceDocument> response = request.execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
ODataServiceDocument serviceDocument = response.getBody();
assertNotNull(serviceDocument);
assertThat(serviceDocument.getEntitySetNames(), hasItem("ESAllPrim"));
assertThat(serviceDocument.getFunctionImportNames(), hasItem("FICRTCollCTTwoPrim"));
assertThat(serviceDocument.getSingletonNames(), hasItem("SIMedia"));
}
@Test
public void readMetadata() {
EdmMetadataRequest request = getClient().getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<Edm> response = request.execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
Edm edm = response.getBody();
assertNotNull(edm);
assertEquals(2, edm.getSchemas().size());
assertEquals("olingo.odata.test1", edm.getSchema("olingo.odata.test1").getNamespace());
assertEquals("Namespace1_Alias", edm.getSchema("olingo.odata.test1").getAlias());
assertEquals("Org.OData.Core.V1", edm.getSchema("Org.OData.Core.V1").getNamespace());
assertEquals("Core", edm.getSchema("Org.OData.Core.V1").getAlias());
}
@Test
public void readViaXmlMetadata() {
XMLMetadataRequest request = getClient().getRetrieveRequestFactory().getXMLMetadataRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<XMLMetadata> response = request.execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
XMLMetadata xmlMetadata = response.getBody();
assertNotNull(xmlMetadata);
assertTrue(xmlMetadata instanceof org.apache.olingo.client.api.edm.xml.v4.XMLMetadata);
assertEquals(2, xmlMetadata.getSchemas().size());
assertEquals("olingo.odata.test1", xmlMetadata.getSchema("olingo.odata.test1").getNamespace());
final List<Reference> references =
((org.apache.olingo.client.api.edm.xml.v4.XMLMetadata) xmlMetadata).getReferences();
assertEquals(1, references.size());
assertThat(references.get(0).getUri().toASCIIString(), containsString("vocabularies/Org.OData.Core.V1"));
}
@Test
public void readEntitySet() {
final ODataEntitySetRequest<ODataEntitySet> request = getClient().getRetrieveRequestFactory()
.getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESMixPrimCollComp").build());
assertNotNull(request);
final ODataRetrieveResponse<ODataEntitySet> response = request.execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
assertThat(response.getContentType(), containsString(ContentType.APPLICATION_JSON.toContentTypeString()));
final ODataEntitySet entitySet = response.getBody();
assertNotNull(entitySet);
assertNull(entitySet.getCount());
assertNull(entitySet.getNext());
assertEquals(Collections.<ODataAnnotation> emptyList(), entitySet.getAnnotations());
assertNull(entitySet.getDeltaLink());
final List<ODataEntity> entities = entitySet.getEntities();
assertNotNull(entities);
assertEquals(3, entities.size());
final ODataEntity entity = entities.get(2);
assertNotNull(entity);
final ODataProperty property = entity.getProperty("PropertyInt16");
assertNotNull(property);
assertNotNull(property.getPrimitiveValue());
assertEquals(0, property.getPrimitiveValue().toValue());
}
@Test
public void readException() throws Exception {
final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESMixPrimCollComp").appendKeySegment("42").build());
assertNotNull(request);
try {
request.execute();
fail("Expected Exception not thrown!");
} catch (final ODataClientErrorException e) {
assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), e.getStatusLine().getStatusCode());
final ODataError error = e.getODataError();
assertThat(error.getMessage(), containsString("key property"));
}
}
@Test
public void readEntity() throws IOException {
final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1).build());
assertNotNull(request);
final ODataRetrieveResponse<ODataEntity> response = request.execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
assertThat(response.getContentType(), containsString(ContentType.APPLICATION_JSON.toContentTypeString()));
final ODataEntity entity = response.getBody();
assertNotNull(entity);
final ODataProperty property = entity.getProperty("CollPropertyInt16");
assertNotNull(property);
assertNotNull(property.getCollectionValue());
assertEquals(3, property.getCollectionValue().size());
Iterator<ODataValue> iterator = property.getCollectionValue().iterator();
assertEquals(1000, iterator.next().asPrimitive().toValue());
assertEquals(2000, iterator.next().asPrimitive().toValue());
assertEquals(30112, iterator.next().asPrimitive().toValue());
}
/**
* Actual an create request for an entity will lead to an "501 - Not Implemented" response
* and hence to an ODataServerErrorException
*/
@Test(expected = ODataServerErrorException.class)
public void createEntity() throws IOException {
final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1).build());
assertNotNull(request);
final ODataRetrieveResponse<ODataEntity> response = request.execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
assertThat(response.getContentType(), containsString(ContentType.APPLICATION_JSON.toContentTypeString()));
final ODataEntity entity = response.getBody();
assertNotNull(entity);
final ODataEntityCreateRequest<ODataEntity> createRequest = getClient().getCUDRequestFactory()
.getEntityCreateRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESCollAllPrim").build(), entity);
assertNotNull(createRequest);
ODataEntityCreateResponse<ODataEntity> createResponse = createRequest.execute();
final ODataEntity createdEntity = createResponse.getBody();
assertNotNull(createdEntity);
final ODataProperty property = createdEntity.getProperty("CollPropertyInt16");
assertNotNull(property);
assertNotNull(property.getCollectionValue());
assertEquals(3, property.getCollectionValue().size());
Iterator<ODataValue> iterator = property.getCollectionValue().iterator();
assertEquals(1000, iterator.next().asPrimitive().toValue());
assertEquals(2000, iterator.next().asPrimitive().toValue());
assertEquals(30112, iterator.next().asPrimitive().toValue());
}
@Override
protected CommonODataClient<?> getClient() {
ODataClient odata = ODataClientFactory.getV4();
odata.getConfiguration().setDefaultPubFormat(ODataFormat.JSON);
return odata;
}
}