/* * Copyright 2011 Research Studios Austria Forschungsgesellschaft mBH * * This file is part of easyrec. * * easyrec is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * easyrec is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with easyrec. If not, see <http://www.gnu.org/licenses/>. */ package org.easyrec.rest.api_v_1_0.itemtype; import com.sun.jersey.core.util.MultivaluedMapImpl; import com.sun.jersey.test.framework.spi.container.TestContainerException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.easyrec.rest.api_v_1_0.AbstractApiTest; import org.junit.Test; import org.junit.runner.RunWith; import org.unitils.UnitilsJUnit4TestClassRunner; import org.unitils.dbunit.annotation.DataSet; import javax.ws.rs.core.MultivaluedMap; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; /** * ${DESCRIPTION} * <p/> * <p><b>Company:</b> * SAT, Research Studios Austria</p> * <p/> * <p><b>Copyright:</b> * (c) 2011</p> * <p/> * <p><b>last modified:</b><br/> * $Author: $<br/> * $Date: $<br/> * $Revision: $</p> * * @author patrick */ @RunWith(UnitilsJUnit4TestClassRunner.class) @DataSet(AbstractApiTest.DATASET_BASE) public abstract class ItemTypesTest extends AbstractApiTest { public static class JSON extends ItemTypesTest { public JSON() throws TestContainerException { super(METHOD_JSON); } } public static class XML extends ItemTypesTest { public XML() throws TestContainerException { super(METHOD_XML); } } public ItemTypesTest(String method) throws TestContainerException { super("itemtypes", method); } @Test public void itemTypes_noParameters_shouldReturnError() { MultivaluedMap<String, String> params = new MultivaluedMapImpl(); JSONObject json = makeAPIRequest(params); assertThat(json, not(is(nullValue()))); assertThat(json.getJSONObject("error").getString("@code"), is("299")); assertThat(json.getJSONObject("error").getString("@message"), containsString("Wrong APIKey/Tenant combination")); } @Test public void itemTypes_wrongTenant_shouldReturnError() { MultivaluedMap<String, String> params = new MultivaluedMapImpl(); params.add("tenantid", "WRONG-TENANT"); params.add("apikey", API_KEY); JSONObject json = makeAPIRequest(params); assertThat(json, not(is(nullValue()))); assertThat(json.getJSONObject("error").getString("@code"), is("299")); assertThat(json.getJSONObject("error").getString("@message"), containsString("Wrong APIKey/Tenant combination")); } @Test public void itemTypes_wrongApiKey_shouldReturnError() { MultivaluedMap<String, String> params = new MultivaluedMapImpl(); params.add("tenantid", TENANT_ID); params.add("apikey", "0123456789abcdefedcba98765432100"); JSONObject json = makeAPIRequest(params); assertThat(json, not(is(nullValue()))); assertThat(json.getJSONObject("error").getString("@code"), is("299")); assertThat(json.getJSONObject("error").getString("@message"), containsString("Wrong APIKey/Tenant combination")); } @Test public void itemTypes_apiKeyTenant_shouldReturnItemTypes() { MultivaluedMap<String, String> params = new MultivaluedMapImpl(); params.add("tenantid", TENANT_ID); params.add("apikey", API_KEY); JSONObject json = makeAPIRequest(params); assertThat(json, not(is(nullValue()))); assertThat(json.getString("tenantId"), is(TENANT_ID)); JSONArray itemTypes = getSafeJSONArray(json, "itemTypes", "itemType"); // itemtypes where visible=0 are not shown assertThat(itemTypes.size(), is(2)); assertThat(itemTypes.getString(0), is("ITEM_TYPE")); assertThat(itemTypes.getString(1), is("OTHER_ITEM")); } }