/*
 * Copyright 2014 Stackify
 *
 * 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.stackify.api.common;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.stackify.api.AppIdentity;
import com.stackify.api.EnvironmentDetail;
import com.stackify.api.common.http.HttpClient;

/**
 * AppIdentityService JUnit Test
 * @author Eric Martin
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({AppIdentityService.class, HttpClient.class})
public class AppIdentityServiceTest {

	/**
	 * testGetAppIdentity
	 * @throws Exception 
	 */
	@Test
	public void testGetAppIdentity() throws Exception {		

		EnvironmentDetail envDetail = EnvironmentDetail.newBuilder().deviceName("device").appName("app").build();

		AppIdentity appIdentity = AppIdentity.newBuilder().deviceId(Integer.valueOf(123)).appNameId("456").build();

		ObjectMapper objectMapper = new ObjectMapper();
		
		String appIdentityResponse = objectMapper.writer().writeValueAsString(appIdentity);
		
		HttpClient httpClient = PowerMockito.mock(HttpClient.class);
		PowerMockito.whenNew(HttpClient.class).withAnyArguments().thenReturn(httpClient);
		PowerMockito.when(httpClient.post(Mockito.anyString(), (byte[]) Mockito.any())).thenReturn(appIdentityResponse);
		
		ApiConfiguration apiConfig = ApiConfiguration.newBuilder().apiUrl("url").apiKey("key").envDetail(envDetail).build();
		
		AppIdentityService service = new AppIdentityService(apiConfig, objectMapper);

		Optional<AppIdentity> rv = service.getAppIdentity();
		
		Assert.assertNotNull(rv);
		Assert.assertTrue(rv.isPresent());
		Assert.assertEquals(Integer.valueOf(123), rv.get().getDeviceId());
		Assert.assertEquals("456", rv.get().getAppNameId());
	}
}

@@ -763,32 +763,88 @@
 o.PowerMockito;%0A
+import static org.powermock.api.mockito.PowerMockito.*;%0A
 import org.power
@@ -1414,17 +1414,16 @@
 xception
- 
 %0A%09 */%0A%09@
@@ -1483,14 +1483,18 @@
 on %7B
-%09%09
 %0A%0A%09%09
+final 
 Envi
@@ -1518,17 +1518,20 @@
 Detail =
- 
+%0A%09%09%09
 Environm
@@ -1601,16 +1601,22 @@
 d();%0A%0A%09%09
+final 
 AppIdent
@@ -1632,17 +1632,20 @@
 entity =
- 
+%0A%09%09%09
 AppIdent
@@ -1674,29 +1674,12 @@
 eId(
-Integer.valueOf(
 123)
-)
 .app
@@ -1692,16 +1692,31 @@
 (%22456%22).
+appName(%22app%22).
 build();
@@ -1719,16 +1719,22 @@
 d();%0A%0A%09%09
+final 
 ObjectMa
@@ -1773,18 +1773,16 @@
 pper();%0A
-%09%09
 %0A%09%09Strin
@@ -1860,21 +1860,25 @@
 ntity);%0A
-%09%09
 %0A%09%09
+final 
 HttpClie
@@ -1893,29 +1893,16 @@
 lient = 
-PowerMockito.
 mock(Htt
@@ -1915,37 +1915,24 @@
 t.class);%0A%09%09
-PowerMockito.
 whenNew(Http
@@ -1990,29 +1990,16 @@
 ent);%0A%09%09
-PowerMockito.
 when(htt
@@ -2090,18 +2090,16 @@
 ponse);%0A
-%09%09
 %0A%09%09ApiCo
@@ -2121,17 +2121,20 @@
 Config =
- 
+%0A%09%09%09
 ApiConfi
@@ -2183,16 +2183,35 @@
 (%22key%22).
+application(%22app%22).
 envDetai
@@ -2228,26 +2228,24 @@
 l).build();%0A
-%09%09
 %0A%09%09AppIdenti
@@ -2369,18 +2369,16 @@
 tity();%0A
-%09%09
 %0A%09%09Asser

test was fixed