/*
 * Copyright (C) 2015 Orange
 * 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 com.orange.clara.cloud.services.sandbox.infrastructure;

import com.orange.clara.cloud.services.sandbox.domain.UserInfo;
import org.cloudfoundry.client.lib.CloudFoundryClient;
import org.cloudfoundry.client.lib.domain.CloudInfo;
import org.fest.assertions.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;

import java.security.Principal;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by sbortolussi on 05/10/2015.
 */
@RunWith(MockitoJUnitRunner.class)
public class CloudfoundryIdentityServiceTest {

    //access token containing "user_id: uaa-id-314"
    public static final String ACCESS_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidWFhLWlkLTMxNCIsImVtYWlsIjoiZW1haWwtMjA3QHNvbWVkb21haW4uY29tIiwic2NvcGUiOlsiY2xvdWRfY29udHJvbGxlci5hZG1pbiJdLCJhdWQiOlsiY2xvdWRfY29udHJvbGxlciJdLCJleHAiOjE0NDQyNjA2NTF9.UKdlvpim-D0p5bzKPeAHdpWOHJthtrfQpQAZzDtBO7o";
    @Mock
    CloudFoundryClient cloudFoundryClient;

    @Mock
    OAuth2ClientContext oauth2Context;

    @InjectMocks
    private CloudfoundryIdentityService identityService;

    @Test
    public void should_get_user_id() throws Exception {
        CloudInfo cloudInfo = new CloudInfo(Collections.unmodifiableMap(Stream.of(new AbstractMap.SimpleEntry<>("user", "myUsername")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
        Mockito.when(cloudFoundryClient.getCloudInfo()).thenReturn(cloudInfo);
        Mockito.when(oauth2Context.getAccessToken()).thenReturn(new DefaultOAuth2AccessToken(ACCESS_TOKEN));

        Principal user = new Principal() {
            @Override
            public String getName() {
                return "myUsername";
            }
        };

        UserInfo userInfo = identityService.getInfo(user);
        Assertions.assertThat(userInfo).isEqualTo(new UserInfo("myUsername", "uaa-id-314"));
    }


}
@@ -1188,16 +1188,159 @@
 ssToken;
+%0Aimport org.springframework.security.oauth2.common.OAuth2AccessToken;%0Aimport org.springframework.security.oauth2.provider.OAuth2Authentication;
 %0A%0Aimport
@@ -1494,16 +1494,57 @@
 tream;%0A%0A
+import static org.mockito.Mockito.when;%0A%0A
 /**%0A * C
@@ -2134,24 +2134,76 @@
 h2Context;%0A%0A
+    @Mock%0A    OAuth2AccessToken oAuth2AccessToken;%0A%0A
     @InjectM
@@ -2343,359 +2343,115 @@
     
-CloudInfo cloudInfo = new CloudInfo(Collections.unmodifiableMap(Stream.of(new AbstractMap.SimpleEntry%3C%3E(%22user%22, %22myUsername%22)).collect(Collectors.toMap((e) -%3E e.getKey(), (e) -%3E e.getValue()))));%0A        Mockito.when(cloudFoundryClient.getCloudInfo()).thenReturn(cloudInfo);%0A        Mockito.when(oauth2Context.getAccessToken()).thenReturn(new DefaultO
+when(oAuth2AccessToken.getValue()).thenReturn(ACCESS_TOKEN);%0A        identityService.setoAuth2AccessToken(o
 Auth
@@ -2462,30 +2462,16 @@
 essToken
-(ACCESS_TOKEN)
 );%0A%0A    
@@ -2495,146 +2495,26 @@
 r = 
-new Principal() %7B%0A            @Override%0A            public String getName() %7B%0A                return %22myUsername%22;%0A            %7D%0A        %7D
+() -%3E %22myUsername%22
 ;%0A%0A 

fix CloudfoundryIdentityService