JUnit Test Spring MVC Web Services

Spring MVC REST Controller

package com.example.controller.api;

import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.model.CustomerActivity;
import com.example.service.CustomerActivityRepository;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiError;
import com.wordnik.swagger.annotations.ApiErrors;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/api/v1/customeractivity")
@Api(value = "Customer operations", listingClass = "CustomerController", basePath = "/api/v1/customeractivity", description = "All operations for customers activity")
public class CustomerActivityController {
	
	@Autowired
	@Qualifier("mongoRepository")
	private CustomerActivityRepository customerService;
	
	 @ApiOperation(value = "Find customer by Id", notes = "Get customer by specifying Id", httpMethod = "GET", responseClass = "Customer", multiValueResponse = true)
	 @ApiErrors(value = { @ApiError(code = 400, reason = "Invalid ID supplied"), @ApiError(code = 404, reason = "Customer not found") })
	 @RequestMapping(value = "/find/{customerId}", method = RequestMethod.GET, produces = "application/json")
	 public @ResponseBody List<CustomerActivity> findCustomerById(@ApiParam(internalDescription = "java.lang.string", name = "customerId", required = true, value = "string") @PathVariable String customerId) {
		 	List<CustomerActivity> customer = customerService.findCustomerActivityByCustomerId(customerId);
	        return customer;
	 }
}

Spring MVC REST Junit Test

package com.example.controller.api;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Date;

import org.junit.Before;
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 org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.example.model.CustomerActivity;
import com.example.service.CustomerActivityRepositoryImpl;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={CustomerActivityController.class, CustomerActivityRepositoryImpl.class})
public class CustomerActivityControllerTest {
	
	private MockMvc mockMvc;
	
	@Autowired CustomerActivityController customerActivityController;
	
	@Before
	public void setup() {
		this.mockMvc =  MockMvcBuilders.standaloneSetup(customerActivityController).build();
	}
	
	@Test
	public void testCustomerActivityFindCustomerById() throws Exception {
		CustomerActivity customerActivity = new CustomerActivity("123", "SMS Received", new Date(), "SMS", "Workflow", "http://workflow.com");
		CustomerActivityController mock = org.mockito.Mockito.mock(CustomerActivityController.class);
		when(mock.findCustomerById("123")).thenReturn(Arrays.asList(customerActivity));
		
		this.mockMvc.perform(get("/api/v1/customeractivity/find/123"))
		.andExpect(status().isOk())
		.andExpect(content().contentType("application/json"))
		.andExpect(jsonPath("$[0].customerId").value("123"));
		verifyNoMoreInteractions(mock);
	}
}

My Dreamforce 2013 Developer Theater Presentation

Exposing Force.com RESTful Services using Swagger.

Try it live:
http://force-com-rest-swagger.herokuapp.com

Fork my project
https://github.com/thysmichels/force.com-swagger-rest-spring-mvc

Here are some photos from the event:

photo 1

photo 2

Presentation attached:
DF13_Exposing Salesforce REST Service using Swagger

Youtube video:

Web Service WS-I Compliance Check with SOAPUI

How to check if your Web Service is WS-I compliant. You can use SOAPUI which is an open source tool to test Web Services.

Step 1. Import your WSDL file into a new SOAPUI project.

Step 2: Right click on your Project Name and select ‘Check WSI Compliance’

Step 3: Download the report created by SOAPUI, an example is an WS-I compliant Web Service looking like the following:

WS-I Profile Conformance Report

Web Services from Stateless Session EJB3 with Websphere Integration Developer

How to create a bottoms-up Websphere with Websphere Integration Developer that is deployed to Websphere Application Server V7.

Below is the steps to create an simple Web Service using Websphere Integration Developer:

1. Open up Websphere Integration Developer and create an new EJB Project

Step 2: Click on the ‘New’ button to create a new EAR Membership.

The following 3 files will be created

Step 3: Create a new Stateless Session Bean

Step 4: Complete the following information below (Important to select Remote)

Step 5: Open the Bank interface object and add a method.

Step 6: Open your Stateless Session Bean File and right click and select ‘Override/Implementation Methods’, make sure the @Override notation is added to your code (see in red block)

Step 7: Add code inside your method

Step 8: Verify that there is no error.

Step 9: Add an Application Server to your WID IDE and start the server.

Step 10: Right click on your Application Server and click on ‘Add and Remove Projects’ and Select the BankAccount project

Step 11: Verify that the Application Server has successfully synchronized the new project and no errors occurred.

Step 12: Creating the Web Service is done in the following Steps, right click on your Stateless Session Bean and Select Web Service -> Create Web Service.  Your will see the following screens:

Confirm that the right Server type is selected, being WAS, WPS, WBM or any Application Server

For this example the binding type is HTTP, you can change it to JMS or EJB.

As we only have one method, select the method BankAccount. WS-Security can be added in this step to create a more secure Web Service.

Optional you can deploy your WSDL to the Websphere UDDI for Web Services discovery. Click Finish to complete. Confirm that Web Service creation was successful.

Step 13. Test your new Web Service by opening Web Services Explorer and browsing for the Bank.wsdl

Step 14: Enter the relevant information into the two fields provided and click ‘GO’

Web Service Completed successfully.  This Web Services can now be deployed to Websphere Registry and Repository to be discovered by all other systems in your environment.

Main building blocks in SOA approach for Web Services

Below is a good graphical view of the SOA approach to Web Services. For all of these blocks there is IBM Software to successfully implement a Web Service solution.

1. Create WSDL – Rational Application Developer

2. UDDI – Websphere Application Server

3. Web Service Registry – Websphere Registry and Repository