Friday, July 26, 2013

Using SOAP UI vs RestClient

I-Using RestClient tool:
II-Using SOAP UI tool:
- I have just uploaded TestCases for using SOAP UI. And, i have added GET/POST testCases,but not DELETE/PUT method.
- Dowload SOAP UI tool
- File/Import Project
- Hopefully, you enjoy.
SOAP UI Test Cases

Friday, July 12, 2013

Spring Restful: Get,Add,Update,Delete Restfull WS at Server Side

package com.fpt.family.controler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fpt.family.util.DateConverter;
import com.jaxb.family.Children;
import com.jaxb.family.Family;
import com.jaxb.family.Father;
import com.jaxb.family.Mother;

@Controller
@RequestMapping(\"fpt/family\")
public class FamilyController {

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/get
    @RequestMapping(value = \"/get\", method = RequestMethod.GET)
    public @ResponseBody
    Family getAllFamily() {

        Children children01 = new Children();
        children01.setFullName(\"Nguyen Trung Hieu Brother\");
        children01.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        children01.setJob(\"Java Engineer\");
        children01.setCompany(\"Fsoft Company\");

        Children children02 = new Children();
        children02.setFullName(\"Nguyen Trung Hieu Brother\");
        children02.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        children02.setJob(\"Java Engineer\");
        children02.setCompany(\"Fsoft Company\");

        Father father = new Father();
        father.setCompany(\"Fsoft Company\");
        father.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        father.setFatherName(\"Nguyen Trung Hieu Father\");
        father.setJob(\"Java Engineer\");

        Mother mother = new Mother();
        mother.setCompany(\"Fsoft Company\");
        mother.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        mother.setMotherName(\"Nguyen Trung Hieu Mother\");
        mother.setJob(\"Java Engineer\");

        Family family = new Family();

        family.setMother(mother);
        family.setFather(father);
        family.getChildren().add(children01);
        family.getChildren().add(children02);

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/get/true
    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/get/false
    /**
     * Get Family
     * @param flag
     * @return
     */

    @RequestMapping(value = \"/get/{flag}\", method = RequestMethod.GET)
    public @ResponseBody
    Family getFamily(@PathVariable(\"flag\") Boolean flag) {
        Family family = null;
        if (flag) {
            Children children01 = new Children();
            children01.setFullName(\"Nguyen Trung Hieu Brother\");
            children01.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children01.setJob(\"Java Engineer\");
            children01.setCompany(\"Fsoft Company\");

            Children children02 = new Children();
            children02.setFullName(\"Nguyen Trung Hieu Brother\");
            children02.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children02.setJob(\"Java Engineer\");
            children02.setCompany(\"Fsoft Company\");

            Father father = new Father();
            father.setCompany(\"Fsoft Company\");
            father.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            father.setFatherName(\"Nguyen Trung Hieu Father\");
            father.setJob(\"Java Engineer\");

            Mother mother = new Mother();
            mother.setCompany(\"Fsoft Company\");
            mother.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            mother.setMotherName(\"Nguyen Trung Hieu Mother\");
            mother.setJob(\"Java Engineer\");

            family = new Family();

            family.setMother(mother);
            family.setFather(father);
            family.getChildren().add(children01);
            family.getChildren().add(children02);
        } else {
            Children children01 = new Children();
            children01.setFullName(\"Nguyen Trung Hieu Brother\");
            children01.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children01.setJob(\"Java Engineer\");
            children01.setCompany(\"Fsoft Company\");

            Children children02 = new Children();
            children02.setFullName(\"Nguyen Trung Hieu Brother\");
            children02.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children02.setJob(\"Java Engineer\");
            children02.setCompany(\"Fsoft Company\");

            Father father = new Father();
            father.setCompany(\"Fsoft Company\");
            father.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            father.setFatherName(\"Nguyen Trung Hieu Father\");
            father.setJob(\"Java Engineer\");

            Mother mother = new Mother();
            mother.setCompany(\"Fsoft Company\");
            mother.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            mother.setMotherName(\"Nguyen Trung Hieu Mother\");
            mother.setJob(\"Java Engineer\");

            family = new Family();
            family.setMother(mother);
            family.setFather(father);
            family.getChildren().add(children01);
            family.getChildren().add(children02);
        }

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/
    /**
     * For updating family
     * 
     * @param familyRequest
     * @return
     */

    @RequestMapping(value = \"/\", method = RequestMethod.PUT)
    public @ResponseBody
    Family updateFamily(@RequestBody Family familyRequest) {

        Family family = new Family();
        family.setFather(familyRequest.getFather());
        family.setMother(familyRequest.getMother());

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/
    /**
     * For add new family
     * 
     * @param familyRequest
     * @return
     */

    @RequestMapping(value = \"/\", method = RequestMethod.POST)
    public @ResponseBody
    Family addFamily(@RequestBody Family familyRequest) {

        Family family = new Family();
        family.setFather(familyRequest.getFather());
        family.setMother(familyRequest.getMother());

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/delete/true
    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/delete/false
    /**
     * For Delete family: True delete All
     * 
     * @param flag
     * @param familyRequest
     * @return
     */

    @RequestMapping(value = \"/delete/{flag}\", method = RequestMethod.DELETE)
    public @ResponseBody
    Family deleteFamily(@PathVariable(\"flag\") Boolean flag,
            @RequestBody Family familyRequest) {

        Family family = null;

        if (flag) {
            family = new Family();
            family.setFather(familyRequest.getFather());
            family.setMother(familyRequest.getMother());
        } else {
            family = new Family();
            family.setFather(familyRequest.getFather());
            family.setMother(familyRequest.getMother());
        }

        return family;
    }
}
Source Code

Saturday, July 6, 2013

Spring Testing 3.1: Context Configuration Inheritance For XML

Step 01:

package com.config.inheritance.xml;

public class Department {
    private String departmentId;
    private String departmentName;

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

Step 02:

package com.config.inheritance.xml;

public class User {
    private String userName;
    private Long salary;

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Step 03: src/main/resources

"http://www.springframework.org/schema/beans\"
    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
    xmlns:context=\"http://www.springframework.org/schema/context\"
    xsi:schemaLocation=\"http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd\">
 
    "departmentBean\"
 class=\"com.config.inheritance.xml.Department\">
        "departmentId\"
 value=\"ID-01\"/>
        "departmentName\"
 value=\"ACCOUNT\"/>
    
    


Step 04: src/main/resources

"http://www.springframework.org/schema/beans\"
    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
    xmlns:context=\"http://www.springframework.org/schema/context\"
    xsi:schemaLocation=\"http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd\">
 
    "userBean\"
 class=\"com.config.inheritance.xml.User\">
        "userName\"
 value=\"Nguyen Trung Hieu\"/>
        "salary\"
 value=\"15000000\"/>
    
    


Step 05:src/test/java

package com.config.inheritance.xml.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.inheritance.xml.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = \"classpath:userContext.xml\")
public class UserBeanTest {

    @Autowired
    User userBean;

    @Test
    public void testAddUser() {
        assertThat(userBean.getUserName(), equalTo(\"Nguyen Trung Hieu\"));
        assertThat(userBean.getSalary(), equalTo(15000000L));
    }
}

Step 06:src/test/java

package com.config.inheritance.xml.TEST;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.config.inheritance.xml.Department;

@ContextConfiguration(locations = \"classpath:departmentContext.xml\")
public class DepartmentBeanTest extends UserBeanTest {
    @Autowired
    Department departmentBean;

    @Test
    public void testAddDepartment() {
        assertThat(departmentBean.getDepartmentId(), equalTo(\"ID-01\"));
        assertThat(departmentBean.getDepartmentName(), equalTo(\"ACCOUNT\"));
    }
}

Source Code

JAXB: Using Maven Plugin XJC tool to generate POJO objects

Step 01: Family.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- (c) 2010 DIRECTV, Inc. All rights reserved. -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">

    <xsd:complexType name="father">
            <xsd:sequence>
                <xsd:element name="fatherName" type="xsd:string"
                    minOccurs="0" />
                <xsd:element name="dateOfBirth" type="xsd:date"
                    minOccurs="0" />
                <xsd:element name="job" type="xsd:string" minOccurs="0" />
                <xsd:element name="age" type="xsd:int" minOccurs="0" />
            </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="mother">
        <xsd:sequence>
            <xsd:element name="motherName" type="xsd:string"/>
            <xsd:element name="dateOfBirth" type="xsd:date"/>
            <xsd:element name="job" type="xsd:string"/>
            <xsd:element name="age" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="children">
        <xsd:sequence>
            <xsd:element name="fullName" type="xsd:string"
                minOccurs="0" />
            <xsd:element name="dateOfBirth" type="xsd:date"
                minOccurs="0" />
            <xsd:element name="job" type="xsd:string" minOccurs="0" />
            <xsd:element name="age" type="xsd:int" minOccurs="0" />
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:element name="family">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="father" type="father"/>
                <xsd:element name="mother" type="mother"/>
                <xsd:element name="children" type="children" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    
</xsd:schema>
Step 02: Pom.xsd
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.common</groupId>
    <artifactId>restfuljaxb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>restfuljaxb Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <properties>
        <jersey.version>1.16</jersey.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>restfuljaxb</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- Plugin to generate POJOs -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <packageName>com.jaxb.family</packageName>
                    <schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Step 03: Maven Build maven install
Step 04: Output

Tuesday, July 2, 2013

JAXB: Using Command Line XJC tool to generate POJO objects

xjc -p com.generated.xjc -d F:\PROJECT\UP\RESEARCH\_Jersey\restfuljaxb\src\main\resources family.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- (c) 2010 DIRECTV, Inc. All rights reserved. -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">

    <xsd:complexType name="father">
            <xsd:sequence>
                <xsd:element name="fatherName" type="xsd:string"
                    minOccurs="0" />
                <xsd:element name="dateOfBirth" type="xsd:date"
                    minOccurs="0" />
                <xsd:element name="job" type="xsd:string" minOccurs="0" />
                <xsd:element name="age" type="xsd:int" minOccurs="0" />
            </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="mother">
        <xsd:sequence>
            <xsd:element name="motherName" type="xsd:string"/>
            <xsd:element name="dateOfBirth" type="xsd:date"/>
            <xsd:element name="job" type="xsd:string"/>
            <xsd:element name="age" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="children">
        <xsd:sequence>
            <xsd:element name="fullName" type="xsd:string"
                minOccurs="0" />
            <xsd:element name="dateOfBirth" type="xsd:date"
                minOccurs="0" />
            <xsd:element name="job" type="xsd:string" minOccurs="0" />
            <xsd:element name="age" type="xsd:int" minOccurs="0" />
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:element name="family">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="father" type="father"/>
                <xsd:element name="mother" type="mother"/>
                <xsd:element name="children" type="children" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    

</xsd:schema>