/*
 * Copyright 2013 Agorava
 *
 * Licensed 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.agorava.twitter.cdi.test;

import org.agorava.api.atinject.Current;
import org.agorava.api.oauth.OAuthService;
import org.agorava.api.oauth.OAuthSession;
import org.agorava.api.oauth.Token;
import org.agorava.api.service.OAuthLifeCycleService;
import org.agorava.twitter.Twitter;
import org.agorava.twitter.TwitterTimelineService;
import org.agorava.twitter.TwitterUserService;
import org.agorava.twitter.model.SuggestionCategory;
import org.agorava.twitter.model.Tweet;
import org.agorava.twitter.model.TwitterProfile;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;

@RunWith(Arquillian.class)
public class TwitterTest extends TwitterTestDeploy {

    @Inject
    @Twitter
    TwitterTimelineService tl;

    @Inject
    @Twitter
    TwitterUserService userService;

    @Inject
    @Twitter
    OAuthService service;

    @Inject
    OAuthLifeCycleService OAuthLifeCycleService;

    @Inject
    @Twitter
    @Current
    OAuthSession sessionTest;

    @Before
    public void init() {
        Token token = new Token("334872715-u75bjYqWyQSYjFMnKeTDZUn8i0QAExjUQ4ENZXH3",
                "08QG7HVqDjkr1oH1YfBRWmd0n8EG73CuzJgTjFI0sk");
        //service.getSession().setAccessToken(token);
        sessionTest.setAccessToken(token);
        OAuthLifeCycleService.endDance();
    }

    @Test
    public void authorizationUrlTest() {
        Assert.assertTrue(service.getAuthorizationUrl().startsWith("http"));
    }

    @Test
    public void sendATweet() {
        Tweet tweet = tl.updateStatus("Tweet sent from JUnit in API 1.1 at " + new Date().toString());
        Assert.assertFalse(tweet.getId() == 0);

    }

    @Test
    public void searchUser() {
        List<TwitterProfile> res = userService.searchForUsers("antoine");
        Assert.assertFalse(res.isEmpty());

    }

    @Test
    public void suggestionCaegoriesNotEmpty() {
        List<SuggestionCategory> res = userService.getSuggestionCategories();
        Assert.assertFalse(res.isEmpty());

    }

    @Test
    public void testSessionUnicity() {
        Assert.assertEquals(sessionTest.getUserProfile(), service.getSession().getUserProfile());
    }

    @Test
    public void testProfileIsNotNull() {
        Assert.assertNotNull(service.getSession().getUserProfile());
        System.out.println(service.getSession().getUserProfile().getFullName());
    }
}

@@ -12,17 +12,17 @@
 ight 201
-3
+4
  Agorava
@@ -622,16 +622,97 @@
 .test;%0A%0A
+import org.agorava.TwitterLiteral;%0Aimport org.agorava.api.atinject.BeanResolver;%0A
 import o
@@ -912,32 +912,86 @@
 feCycleService;%0A
+import org.agorava.api.storage.UserSessionRepository;%0A
 import org.agora
@@ -1344,16 +1344,21 @@
 t.Before
+Class
 ;%0Aimport
@@ -1412,36 +1412,8 @@
 h;%0A%0A
-import javax.inject.Inject;%0A
 impo
@@ -1453,16 +1453,44 @@
 il.List;
+%0Aimport javax.inject.Inject;
 %0A%0A@RunWi
@@ -1727,32 +1727,33 @@
 rvice service;%0A%0A
+%0A
     @Inject%0A    
@@ -1751,83 +1751,8 @@
 ject
-%0A    OAuthLifeCycleService OAuthLifeCycleService;%0A%0A    @Inject%0A    @Twitter
 %0A   
@@ -1803,16 +1803,21 @@
  @Before
+Class
 %0A    pub
@@ -1816,24 +1816,31 @@
 %0A    public 
+static 
 void init() 
@@ -1837,24 +1837,25 @@
 id init() %7B%0A
+%0A
         Toke
@@ -1991,24 +1991,25 @@
 I0sk%22);%0A
+%0A
         
 //servic
@@ -2004,62 +2004,173 @@
     
-//service.getSession().setAccessToken(token
+OAuthLifeCycleService lfs = BeanResolver.getInstance().resolve(OAuthLifeCycleService.class);%0A%0A        UserSessionRepository r = lfs.getCurrentRepository(
 );%0A
+%0A
         
 sess
@@ -2169,24 +2169,153 @@
     
-sessionTest.setA
+OAuthSession session = new OAuthSession.Builder()%0A                .qualifier(TwitterLiteral.INSTANCE)%0A                .repo(r)%0A                .a
 cces
@@ -2327,17 +2327,16 @@
 n(token)
-;
 %0A       
@@ -2336,37 +2336,102 @@
         
-OAuthLifeCycleService
+        .build();%0A%0A        r.add(session);%0A        r.setCurrent(session);%0A%0A        lfs
 .endDanc

Update Twitter test to initialize OAuthSession once