/*
* 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.shindig.social.opensocial.service;
import java.util.Arrays;
import java.util.List;
import org.apache.shindig.common.EasyMockTestCase;
import org.apache.shindig.common.testing.FakeGadgetToken;
import org.apache.shindig.config.ContainerConfig;
import org.apache.shindig.config.JsonContainerConfig;
import org.apache.shindig.expressions.Expressions;
import org.apache.shindig.protocol.DefaultHandlerRegistry;
import org.apache.shindig.protocol.HandlerExecutionListener;
import org.apache.shindig.protocol.HandlerRegistry;
import org.apache.shindig.protocol.RestHandler;
import org.apache.shindig.protocol.conversion.BeanJsonConverter;
import org.apache.shindig.social.opensocial.spi.CollectionOptionsFactory;
import org.apache.shindig.social.opensocial.spi.MediaItemService;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.inject.Guice;
public class MediaItemHandlerTest extends EasyMockTestCase {
private MediaItemService mediaService;
private MediaItemHandler handler;
private ContainerConfig containerConfig;
private FakeGadgetToken token;
protected HandlerRegistry registry;
private BeanJsonConverter converter;
protected SocialRequestItem request;
@Before
public void setUp() throws Exception {
token = new FakeGadgetToken();
converter = new BeanJsonConverter(Guice.createInjector());
mediaService = mock(MediaItemService.class);
JSONObject config = new JSONObject('{' + ContainerConfig.DEFAULT_CONTAINER + ':' +
"{'gadgets.container': ['default']," +
"'gadgets.features':{opensocial:" +
"{supportedFields: {mediaItem: ['id', 'language', 'title']}}" +
"}}}");
containerConfig = new JsonContainerConfig(config, Expressions.forTesting());
handler = new MediaItemHandler(mediaService, containerConfig, new CollectionOptionsFactory());
registry = new DefaultHandlerRegistry(null, converter,
new HandlerExecutionListener.NoOpHandler());
registry.addHandlers(ImmutableSet.<Object>of(handler));
request = new SocialRequestItem(
Maps.<String, String[]>newHashMap(),
token, converter, converter);
}
@Test
public void testCreate() throws Exception {
// TODO
}
@Test
public void testGet() throws Exception {
// TODO
}
@Test
public void testUpdate() throws Exception {
// TODO
}
@Test
public void testDelete() throws Exception {
// TODO
}
@Test
public void testSupportedFields() throws Exception {
String path = "/mediaItems/@supportedFields";
RestHandler operation = registry.getRestHandler(path, "GET");
replay();
@SuppressWarnings("unchecked")
List<Object> received = (List<Object>) operation.execute(Maps.<String, String[]>newHashMap(),
null, token, converter).get();
assertEquals(3, received.size());
assertEquals("id", received.get(0).toString());
assertEquals("language", received.get(1).toString());
assertEquals("title", received.get(2).toString());
verify();
}
@Test
public void testGetRequestMediaItemIds() {
List<String> expectedMediaItemIds = Arrays.asList("mediaItemId1");
request.setParameter("mediaItemId", expectedMediaItemIds);
assertEquals(expectedMediaItemIds, handler.getRequestMediaItemIds(request));
List<String> expectedIds = Arrays.asList("id1");
request.setParameter("id", expectedIds);
assertEquals(expectedIds, handler.getRequestMediaItemIds(request));
}
@Test
public void testGetRequestMediaItem() {
String mediaItemJson = "{\"id\":\"mediaItem\"}";
request.setParameter("mediaItem", mediaItemJson);
assertEquals("mediaItem", handler.getRequestMediaItem(request).getId());
String dataJson = "{\"id\":\"data\"}";
request.setParameter("data", dataJson);
assertEquals("data", handler.getRequestMediaItem(request).getId());
}
}