Package com.cloudseal.rest

Source Code of com.cloudseal.rest.UserServiceTest

/* Copyright 2012 Cloudseal Ltd
*
* 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.cloudseal.rest;

import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import com.cloudseal.rest.exception.InvalidPasswordException;
import com.cloudseal.rest.jaxb.CloudsealUser;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.cloudseal.rest.client.RESTClientImpl;
import com.cloudseal.rest.client.UserServiceImpl;
import com.cloudseal.rest.exception.UserNotFoundException;

@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {

  private UserServiceImpl classUnderTest;
 
  @Mock private HttpClient httpClient;
 
  @Before
  public void setup() {
    RESTClientImpl restClient = new RESTClientImpl("test", "a", "b", httpClient);
    classUnderTest = new UserServiceImpl(restClient);
  }
 
  @Test
  public void testGetUser() throws IOException {
    String responseXml = getXml("xml/GetUser.xml");
    HttpEntity responseBody = new StringEntity(responseXml);
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, null);
    HttpResponse response = mock(HttpResponse.class);
    when(response.getEntity()).thenReturn(responseBody);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getAllHeaders()).thenReturn(buildHeaders());
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
   
    CloudsealUser user = classUnderTest.getUser("test");
    assertEquals("test", user.getUsername());
    assertEquals("test", user.getFirstName());
    assertEquals("user", user.getLastName());
    assertEquals("password", user.getPassword());
  }
 
  @Test
  public void testGetNullUser() throws IOException {
    String responseXml = getXml("xml/GetUser.xml");
    HttpEntity responseBody = new StringEntity(responseXml);
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "USER_NOT_FOUND");
    HttpResponse response = mock(HttpResponse.class);
    when(response.getEntity()).thenReturn(responseBody);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getAllHeaders()).thenReturn(buildHeaders());
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
   
    CloudsealUser user = classUnderTest.getUser("null");
    assertNull(user);
  }
 
  @Test
  public void testPostUser() throws IOException {
    String responseXml = getXml("xml/GetUser.xml");
    HttpEntity responseBody = new StringEntity(responseXml);
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, null);
    HttpResponse response = mock(HttpResponse.class);
    when(response.getEntity()).thenReturn(responseBody);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getAllHeaders()).thenReturn(buildHeaders());
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
   
    CloudsealUser newUser = new CloudsealUser();
    newUser.setUsername("test");
    newUser.setFirstName("test");
    newUser.setLastName("user");
    newUser.setEmail("test@test.com");
   
    CloudsealUser user = classUnderTest.addUser(newUser);
    assertEquals("test", user.getUsername());
    assertEquals("test", user.getFirstName());
    assertEquals("user", user.getLastName());
    assertEquals("password", user.getPassword());
  }

  @Test
  public void testUpdateNullUser() throws IOException {
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "USER_NOT_FOUND");
    HttpResponse response = mock(HttpResponse.class);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getAllHeaders()).thenReturn(buildHeaders());
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
   
    CloudsealUser newUser = new CloudsealUser();
    newUser.setUsername("null");
    newUser.setFirstName("test");
    newUser.setLastName("user");
    newUser.setEmail("test@test.com");
   
    try {
      classUnderTest.updateUser(newUser);
            fail();
    } catch (UserNotFoundException ex) {
      String message = ex.getMessage();
      assertEquals("USER_NOT_FOUND", message);
    }
  }
 
  @Test
  public void testDeleteUser() throws IOException {
    String responseXml = getXml("xml/GetUser.xml");
    HttpEntity responseBody = new StringEntity(responseXml);
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, null);
    HttpResponse response = mock(HttpResponse.class);
    when(response.getEntity()).thenReturn(responseBody);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getAllHeaders()).thenReturn(buildHeaders());
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
   
    classUnderTest.deleteUser("test");
  }
 
  @Test
  public void testDeleteNullUser() throws IOException {
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "USER_NOT_FOUND");
    HttpResponse response = mock(HttpResponse.class);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getAllHeaders()).thenReturn(buildHeaders());
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
   
    try {
      classUnderTest.deleteUser("null");
            fail();
    } catch (UserNotFoundException ex) {
      String message = ex.getMessage();
      assertEquals("USER_NOT_FOUND", message);
    }
  }

    @Test
    public void testChangePassword() throws IOException {
        setChangePasswordResponse();
        classUnderTest.changePassword("test_user", "new password");
    }

    @Test
    public void testChangeExistingPassword() throws IOException {
        setChangePasswordResponse();
        classUnderTest.changePassword("test_user", "current password", "new password");
    }

    @Test
    public void testChangeInvalidPassword() throws IOException {
        StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 501, "INVALID_PASSWORD");
        HttpResponse response = mock(HttpResponse.class);
        when(response.getStatusLine()).thenReturn(statusLine);
        when(response.getAllHeaders()).thenReturn(buildHeaders());
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        try {
            classUnderTest.changePassword("test_user", "current password", "new password");
            fail();
        } catch (InvalidPasswordException ex) {
            String message = ex.getMessage();
            assertEquals("INVALID_PASSWORD", message);
        }
    }

    @Test
    public void testChangePasswordNullUser() throws IOException {
        StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "USER_NOT_FOUND");
        HttpResponse response = mock(HttpResponse.class);
        when(response.getStatusLine()).thenReturn(statusLine);
        when(response.getAllHeaders()).thenReturn(buildHeaders());
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        try {
            classUnderTest.changePassword("test_user", "new password");
            fail();
        } catch (UserNotFoundException ex) {
            String message = ex.getMessage();
            assertEquals("USER_NOT_FOUND", message);
        }
    }

    private void setChangePasswordResponse() throws IOException {
        String responseXml = getXml("xml/status.xml");
        HttpEntity responseBody = new StringEntity(responseXml);
        StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, null);
        HttpResponse response = mock(HttpResponse.class);
        when(response.getEntity()).thenReturn(responseBody);
        when(response.getStatusLine()).thenReturn(statusLine);
        when(response.getAllHeaders()).thenReturn(buildHeaders());
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
    }
 
  private String getXml(String path) throws IOException {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
    StringWriter out = new StringWriter();
    IOUtils.copy(in, out);
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
    String xml = out.toString();
    return xml;
  }
 
  private Header[] buildHeaders() {
    Header lang = new BasicHeader("Content-Language", "en-US");
    Header type = new BasicHeader("Content-Type", "application/xml");
    Header[] headers = new Header[] { lang, type };
    return headers;
  }
}
TOP

Related Classes of com.cloudseal.rest.UserServiceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.