Coverage Report - org.apache.shindig.social.opensocial.service.PersonHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
PersonHandler
79%
22/28
71%
10/14
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.opensocial.service;
 19  
 
 20  
 import org.apache.shindig.social.ResponseError;
 21  
 import org.apache.shindig.social.opensocial.model.Person;
 22  
 import org.apache.shindig.social.opensocial.spi.CollectionOptions;
 23  
 import org.apache.shindig.social.opensocial.spi.GroupId;
 24  
 import org.apache.shindig.social.opensocial.spi.PersonService;
 25  
 import org.apache.shindig.social.opensocial.spi.SocialSpiException;
 26  
 import org.apache.shindig.social.opensocial.spi.UserId;
 27  
 
 28  
 import com.google.common.collect.Sets;
 29  
 import com.google.inject.Inject;
 30  
 
 31  
 import java.util.Set;
 32  
 import java.util.concurrent.Future;
 33  
 
 34  
 
 35  
 public class PersonHandler extends DataRequestHandler {
 36  
 
 37  
   private final PersonService personService;
 38  
 
 39  
   private static final String PEOPLE_PATH = "/people/{userId}+/{groupId}/{personId}+";
 40  
 
 41  
   @Inject
 42  17
   public PersonHandler(PersonService personService) {
 43  17
     this.personService = personService;
 44  17
   }
 45  
 
 46  
   @Override
 47  
   protected Future<?> handleDelete(RequestItem request) throws SocialSpiException {
 48  1
     throw new SocialSpiException(ResponseError.BAD_REQUEST, "You can't delete people.");
 49  
   }
 50  
 
 51  
   @Override
 52  
   protected Future<?> handlePut(RequestItem request) throws SocialSpiException {
 53  1
     throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, "You can't update right now.");
 54  
   }
 55  
 
 56  
   @Override
 57  
   protected Future<?> handlePost(RequestItem request) throws SocialSpiException {
 58  1
     throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, "You can't add people right now.");
 59  
   }
 60  
 
 61  
   /**
 62  
    * Allowed end-points /people/{userId}+/{groupId} /people/{userId}/{groupId}/{optionalPersonId}+
 63  
    *
 64  
    * examples: /people/john.doe/@all /people/john.doe/@friends /people/john.doe/@self
 65  
    */
 66  
   @Override
 67  
   protected Future<?> handleGet(RequestItem request) throws SocialSpiException {
 68  14
     request.applyUrlTemplate(PEOPLE_PATH);
 69  
 
 70  14
     GroupId groupId = request.getGroup();
 71  14
     Set<String> optionalPersonId = Sets.newLinkedHashSet(request.getListParameter("personId"));
 72  14
     Set<String> fields = request.getFields(Person.Field.DEFAULT_FIELDS);
 73  14
     Set<UserId> userIds = request.getUsers();
 74  
 
 75  
     // Preconditions
 76  14
     Preconditions.requireNotEmpty(userIds, "No userId specified");
 77  14
     if (userIds.size() > 1 && !optionalPersonId.isEmpty()) {
 78  0
       throw new IllegalArgumentException("Cannot fetch personIds for multiple userIds");
 79  
     }
 80  
 
 81  14
     CollectionOptions options = new CollectionOptions(request);
 82  
 
 83  14
     if (userIds.size() == 1) {
 84  13
       if (optionalPersonId.isEmpty()) {
 85  12
         if (groupId.getType() == GroupId.Type.self) {
 86  3
           return personService.getPerson(userIds.iterator().next(), fields, request.getToken());
 87  
         } else {
 88  9
           return personService.getPeople(userIds, groupId, options, fields, request.getToken());
 89  
         }
 90  1
       } else if (optionalPersonId.size() == 1) {
 91  
         // TODO: Add some crazy concept to handle the userId?
 92  1
         return personService.getPerson(new UserId(UserId.Type.userId,
 93  
             optionalPersonId.iterator().next()),
 94  
             fields, request.getToken());
 95  
       } else {
 96  0
         Set<UserId> personIds = Sets.newLinkedHashSet();
 97  0
         for (String pid : optionalPersonId) {
 98  0
           personIds.add(new UserId(UserId.Type.userId, pid));
 99  0
         }
 100  
         // Every other case is a collection response of optional person ids
 101  0
         return personService.getPeople(personIds, new GroupId(GroupId.Type.self, null),
 102  
             options, fields, request.getToken());
 103  
       }
 104  
     }
 105  
 
 106  
     // Every other case is a collection response.
 107  1
     return personService.getPeople(userIds, groupId, options, fields, request.getToken());
 108  
   }
 109  
 }