soa.governance.chapter2.traffic.services.ws.WSServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for soa.governance.chapter2.traffic.services.ws.WSServiceTest.java

Source

package soa.governance.chapter2.traffic.services.ws;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import soa.governance.chapter2.traffic.services.AccountService;
import trafficavoidance.wsdl.account.service_1_0.AccountServicePortType;
import trafficavoidance.xsd.account.types_1.Account;
import trafficavoidance.xsd.account.types_1.Address;
import trafficavoidance.xsd.account.types_1.ListOfPlates;

public class WSServiceTest {

    private static ClassPathXmlApplicationContext appContext;
    private static AccountServicePortType client;
    private static AccountService accountServiceMock;

    @BeforeClass
    public static void setup() throws Exception {
        appContext = new ClassPathXmlApplicationContext("applicationcontext-test-ws.xml");
        client = (AccountServicePortType) appContext.getBean("accountClient");
        accountServiceMock = (AccountService) appContext.getBean("accountServiceImpl");
    }

    @Test
    public void testValidCall() throws Exception {
        reset(accountServiceMock);
        expect(accountServiceMock.createAccount(isA(soa.governance.chapter2.traffic.model.Account.class)))
                .andReturn(getModelAccount());
        replay(accountServiceMock);
        String response = client.createAccount(getTestAccount());
        assertEquals(getTestAccount().getSsn(), response);
        verify(accountServiceMock);
    }

    @Test
    public void testInValidCall() throws Exception {
        boolean thrown = false;
        try {
            client.createAccount(new Account());
        } catch (Exception e) {
            thrown = true;
        }

        assertTrue(thrown);
    }

    private soa.governance.chapter2.traffic.model.Account getModelAccount() {
        soa.governance.chapter2.traffic.model.Address address = new soa.governance.chapter2.traffic.model.Address(
                "Street", 10, "Shortsight");
        List<String> plates = new ArrayList<String>();
        plates.add("PPOOLL");
        plates.add("LLKKJJ");
        soa.governance.chapter2.traffic.model.Account result = new soa.governance.chapter2.traffic.model.Account(
                plates, "2345678", address, "Sophie Anna");
        return result;
    }

    private Account getTestAccount() {
        Address address = new Address();
        address.setCity("Shortsight");
        address.setNumber(new BigInteger("10"));
        address.setStreet("Street");
        Account account = new Account();
        account.setAddress(address);
        ListOfPlates plates = new ListOfPlates();
        plates.getLicensePlate().add("PPOOLL");
        plates.getLicensePlate().add("LLKKJJ");
        account.setLicensePlates(plates);
        account.setName("Sophie Anna");
        account.setSsn("2345678");

        return account;
    }

}