// Path: solutions/lab2/java/src/lab2/TokenRequest.java
// public class TokenRequest {
// 
//   public static final Logger log = LoggerFactory.getLogger(TokenRequest.class);
// 
//   protected String clientId;
//   protected String clientSecret;
// 
//   public TokenRequest(String clientId, String clientSecret)
//   {
//     this.clientId = clientId;
//     this.clientSecret = clientSecret;
//   }
// 
//   public String request() throws IOException {
//     log.debug("initiating request for token");
//     Credentials credentials = new Credentials();
//     URL url = new URL(credentials.getTokenServerUrl());
//     String postData = "grant_type=client_credentials";
//     String basicAuth = this.clientId + ":" + this.clientSecret;
//     HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
//     connection.setReadTimeout(10000);
//     connection.setConnectTimeout(10000);
//     connection.setRequestProperty("Authorization", "Basic " + new BASE64Encoder().encode(basicAuth.getBytes()));
//     connection.setRequestMethod("POST");
//     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//     connection.setDoOutput(true);
//     connection.getOutputStream().write(postData.getBytes());
// 
//     log.debug("HTTP response code "+connection.getResponseCode());
// 
//     if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
//       StringBuffer response = new StringBuffer();
//       BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
//       String line;
//       while ((line = reader.readLine()) != null) {
// 	if (line.length() > 0) {
// 	  response.append(line);
// 	}
//       }
//       if (connection != null) {
// 	connection.disconnect();
//       }
//       return response.toString();
//     }
// 
//     if (connection != null) {
//       connection.disconnect();
//     }
//     throw new IOException("unexpected response from " + credentials.getTokenServerUrl() + ":" + connection.getResponseCode() + ":" + connection.getResponseMessage());
//   }
// 
//   public String parseResponse(String response) throws IOException {
//     ObjectMapper mapper = new ObjectMapper();
//     Map<String, Object> jsonModel = mapper.readValue(response, Map.class);
//     String accessToken = (String)jsonModel.get("access_token");
//     if (accessToken == null) {
//       throw new IOException("access_token field not found in response body");
//     }
//     log.debug("access token: "+accessToken);
//     return accessToken;
//   }
// }


import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lab2.TokenRequest;
import lab.Credentials;
package lab2.test;





public class TokenRequestTest
{
  final Logger log = LoggerFactory.getLogger(TokenRequestTest.class);

  @BeforeClass
  public static void setUp()
  throws Exception
  {
  }

  @AfterClass
  public static void tearDown()
  throws Exception
  {
  }

  @Test
  public void testRequest()
  throws Exception
  {
    Credentials credentials = new Credentials();
    TokenRequest request = new TokenRequest(credentials.getClientId(), credentials.getClientSecret());
    String response = request.request();
    assertNotNull(response);
    log.info("response is:\n"+response);
  }
  
  @Test
  public void testParseResponse()
  throws Exception
  {
    Credentials credentials = new Credentials();
// Path: solutions/lab2/java/src/lab2/TokenRequest.java
// public class TokenRequest {
// 
//   public static final Logger log = LoggerFactory.getLogger(TokenRequest.class);
// 
//   protected String clientId;
//   protected String clientSecret;
// 
//   public TokenRequest(String clientId, String clientSecret)
//   {
//     this.clientId = clientId;
//     this.clientSecret = clientSecret;
//   }
// 
//   public String request() throws IOException {
//     log.debug("initiating request for token");
//     Credentials credentials = new Credentials();
//     URL url = new URL(credentials.getTokenServerUrl());
//     String postData = "grant_type=client_credentials";
//     String basicAuth = this.clientId + ":" + this.clientSecret;
//     HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
//     connection.setReadTimeout(10000);
//     connection.setConnectTimeout(10000);
//     connection.setRequestProperty("Authorization", "Basic " + new BASE64Encoder().encode(basicAuth.getBytes()));
//     connection.setRequestMethod("POST");
//     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//     connection.setDoOutput(true);
//     connection.getOutputStream().write(postData.getBytes());
// 
//     log.debug("HTTP response code "+connection.getResponseCode());
// 
//     if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
//       StringBuffer response = new StringBuffer();
//       BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
//       String line;
//       while ((line = reader.readLine()) != null) {
// 	if (line.length() > 0) {
// 	  response.append(line);
// 	}
//       }
//       if (connection != null) {
// 	connection.disconnect();
//       }
//       return response.toString();
//     }
// 
//     if (connection != null) {
//       connection.disconnect();
//     }
//     throw new IOException("unexpected response from " + credentials.getTokenServerUrl() + ":" + connection.getResponseCode() + ":" + connection.getResponseMessage());
//   }
// 
//   public String parseResponse(String response) throws IOException {
//     ObjectMapper mapper = new ObjectMapper();
//     Map<String, Object> jsonModel = mapper.readValue(response, Map.class);
//     String accessToken = (String)jsonModel.get("access_token");
//     if (accessToken == null) {
//       throw new IOException("access_token field not found in response body");
//     }
//     log.debug("access token: "+accessToken);
//     return accessToken;
//   }
// }


// Path: solutions/lab2/java/test/java/lab2/test/TokenRequestTest.java
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lab2.TokenRequest;
import lab.Credentials;

package lab2.test;





public class TokenRequestTest
{
  final Logger log = LoggerFactory.getLogger(TokenRequestTest.class);

  @BeforeClass
  public static void setUp()
  throws Exception
  {
  }

  @AfterClass
  public static void tearDown()
  throws Exception
  {
  }

  @Test
  public void testRequest()
  throws Exception
  {
    Credentials credentials = new Credentials();
    TokenRequest request = new TokenRequest(credentials.getClientId(), credentials.getClientSecret());
    String response = request.request();
    assertNotNull(response);
    log.info("response is:\n"+response);
  }
  
  @Test
  public void testParseResponse()
  throws Exception
  {
    Credentials credentials = new Credentials();
    TokenRequest request = new TokenRequest(credentials.getClientId(), credentials.getClientSecret());