CXF In interceptor get reference to service object - web-services

I am extending AbstractPhaseInterceptor, and I want to get hold of the JAXWS web service object, in INVOKE or PRE_INVOKE phase. How do I do this?
To be clear, I need to get a reference to the object that implements the web service methods, so:
#WebService(...)
public class ExampleWebService
{
#WebMethod(...)
public void doSomething(...)
{
}
}
public class MyInterceptor
extends AbstractPhaseInterceptor<Message>
{
public MyInterceptor()
{
super(Phase.INVOKE);
}
#Override
public void handleMessage(Message message)
throws Fault
{
ExampleWebService serviceObject = getServiceObject(message);
}
private static ExampleWebService getServiceObject(Message messsage)
{
// how do I implement this?
}
}

I do not test the code but something like that probably works.
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.frontend.ServerFactoryBean;
...
Server server = serverFactoryBean.create();
MyInterceptor myInterceptor = new MyInterceptor(server.getEndpoint());
server.getEndpoint().getInInterceptor().add(myInterceptor);

Related

How to mock web service call without passing mocked class as parameter

I have a web serivce class which need to unit tested.
Here is class which is under test
public class ValidatePaymentMessage {
public CsmValidationResultX validatePaymentmsg(String csmName, String base64PayloadXML){
//Call Web Service to validate Payment
CsmValidationResultX responseMsg=null;
PaymentManagerWebService paymentManagerWebService = new PaymentManagerWebService();
PaymentManagerWebServiceImpl serviceAddrs = paymentManagerWebService.getPaymentManagerWebServicePort();
try {
responseMsg = serviceAddrs.validatePayment(csmName, base64PayloadXML);
} catch (MPMWebServiceException e) {
e.printStackTrace();
}
return responseMsg;
}
}
Here is my Junit class
public class ValidatePaymentMessageTest {
#Test
public void testValidatePaymentmsg() throws MPMWebServiceException {
CsmValidationResultX csmResult= EasyMock.createMock(CsmValidationResultX.class);
PaymentManagerWebServiceImpl paymentManagerImpl = EasyMock.createMock(PaymentManagerWebServiceImpl.class);
EasyMock.expect(paymentManagerImpl.validatePayment("SEPA","BASE64XML")).andReturn(csmResult).anyTimes();
PaymentManagerWebService paymentManager = EasyMock.createMock(PaymentManagerWebService.class);
EasyMock.expect(paymentManager.getPaymentManagerWebServicePort()).andReturn(paymentManagerImpl).anyTimes();
ValidatePaymentMessage validatePayment=new ValidatePaymentMessage();
CsmValidationResultX response = validatePayment.validatePaymentmsg("SEPA", "base64PayloadXML");
System.out.println(response.getCsmValidationResult().isValid());
}
}
When I run this Junit it is calling actual web service instead of mocked one's.So Please let me know how can i resolve this problem.
You are still instantiating a real PaymentManagerWebService in validatePaymentmsg(), so the mocks do not help. You can't mock construction of local variables with EasyMock, but you can with PowerMock. So if changing the code to receive and instance of PaymentManagerWebService is not an option, mock its construction with PowerMock.
#RunWith(PowerMockRunner.class)
#PrepareForTest(ValidatePaymentMessage.class)
public class ValidatePaymentMessageTest {
#Test
public void testValidatePaymentmsg() throws MPMWebServiceException {
// .....
PowerMock.expectNew(PaymentManagerWebService.class).andReturn(paymentManager);
//....
}
}

How can I test RESTful methods with Arquillian?

I have a set of classes to work with REST methods in project. They look like this:
#Path("customer/")
#RequestScoped
public class CustomerCollectionResource {
#EJB
private AppManager manager; // working with DB
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response list(#QueryParam("email") String email) {
final List<Customer> entities = manager.listCustomers(email);
// adding customers to result
return Response.ok(result).build();
}
}
After that I've wrote test method:
#RunWith(Arquillian.class)
public class CustomerResourceTest {
#Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test.war")
// Adding omitted
//.addClasses(....)
}
#Test #GET #Path("projectName/customer") #Consumes(MediaType.APPLICATION_JSON)
public void test(ClientResponse<List<Customer>> response) throws Exception {
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
}
And I get NullPointerException when trying to run this test. It's because of empty response in test case. Why is this happens? DB is configured properly.
There are two modes an arquillian test can run: in-container and client mode. HTTP interfaces can be tested only in client mode (never tried the extensions, only used vanilla Arquillian for this).
By default the test methods executed in the context of the container, called by the arquillian test runner servlet.
#RunWith(Arquillian.class)
public class CustomerResourceTest {
#EJB SomeBean bean; // EJBs can be injected, also CDI beans,
// PersistenceContext, etc
#Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test.war")
// Adding omitted
//.addClasses(....)
}
#Test
public void some_test() {
bean.checkSomething();
}
}
In client mode, the test methods are running outside of the container, so you don't have access to EJBs, EntityManager, etc injected into the test class, but you can inject an URL parameter for the test method.
#RunWith(Arquillian.class)
public class CustomerResourceTest {
// testable = false here means all the tests are running outside of the container
#Deployment(testable = false)
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test.war")
// Adding omitted
//.addClasses(....)
}
// baseURI is the applications baseURI.
#Test
public void create_account_validation_test (#ArquillianResource URL baseURI) {
}
You can use this URL parameter to build URLs to call your HTTP service using whatever method you have, like the new JAX-RS client API.
You can also mix the two modes:
#RunWith(Arquillian.class)
public class CustomerResourceTest {
#EJB SomeBean bean;
#Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test.war")
}
#Test
#InSequence(0)
public void some_test() {
bean.checkSomething();
}
#Test
#RunAsClient // <-- this makes the test method run in client mode
#InSequence(1)
public void test_from_client_side() {
}
}
This is sometimes even necessary, because some extensions, like persistence cannot run in client mode.

Nhibernate unit testing. Child class cannot use Base class property

I am using Fluent NHibernate and trying to do unit testing. Now I have a base test class which looks as follows:
[TestClass]
public abstract class BaseTest<TEntity> where TEntity : IBaseModel
{
private const string testDbFile = "test.db";
private static ISessionFactory sessionFactory;
protected static ISession session;
[TestMethod]
public void configureDB()
{
try
{
if (sessionFactory == null)
{
sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard
.UsingFile(testDbFile))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AdminTest>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
}
}
private static void BuildSchema(Configuration config)
{
new SchemaUpdate(config).Execute(false, true);
}
[TestMethod]
public void sessionCreated()
{
session = sessionFactory.OpenSession();
}
[TestMethod]
public virtual void AddEntity_EntityWasAdded()
{
var entity = BuildEntity();
InsertEntity(entity);
session.Evict(entity);
var reloadedEntity = session.Get<TEntity>(entity.Id);
Assert.IsNotNull(reloadedEntity);
AssertAreEqual(entity, reloadedEntity);
AssertValidId(reloadedEntity);
}
There are also other methods which update and delete an entity. And I have AdminTest class which inherits BaseTest. In AdminTest I have following method:
[TestClass]
public class AdminTest : BaseTest<Admin>
{
[TestMethod]
public void SelectWorks()
{
IList<Admin> admins = session.QueryOver<Admin>().List();
Assert.AreNotEqual(0, admins.Count);
}
}
Here I always have exception, because session is null. Maybe I am wrong in the way of thinking how visual studio performs unit tests (I am newbie in it)?
Now I think like that, visual studio works in the following way
runs test-methods from BaseTest (there it configures database and creates session)
runs selectWorks method. Here I was thinking it should use session from BaseTest
Could you explain what is wrong in my way of thinking? And I want to be able to query from child classes, what is the right way of doing it?
Thanks, any help is appreciated, .
I would suggest using [TestInitialize] and [TestCleanup] in your abstract base class and doing something like the following:
[TestInitialize]
public void TestInitialize()
{
Console.Out.WriteLine("Get a ISession object");
}
[TestCleanup]
public void TestCleanup()
{
Console.Out.WriteLine("Dispose ISession");
}
Then in your child classes continue to do what you are doing:
[TestMethod]
public void DoDbWork()
{
Console.Out.WriteLine("Running a query via nhibernate");
}
You really just want to ensure you have a clean session for each test. The attributes for TestInitialize and TestCleanup will run before and after each unit test. Here is the documentation for those attributes.
To ensure your ISession is in the right state,follow something like this.

Mocking digest authentication in RestEasy

I am using RestEasy to develop a REST server and using the mock dispatcher (org.jboss.resteasy.mockMockDispatcherFactory) for testing the service in my unit tests. My service requires digest authentication and I would to make that part of my testing.
Each of my services accepts a #Context SecurityContext securityContext parameter.
Is there any way is inject a fake SecurityContext in the dispatcher so that I can test that my security methods function properly?
You have to add the SecurityContext into the context data map in ResteasyProviderFactory.
public class SecurityContextTest {
#Path("/")
public static class Service {
#Context
SecurityContext context;
#GET
public String get(){
return context.getAuthenticationScheme();
}
}
public static class FakeSecurityContext extends ServletSecurityContext {
public FakeSecurityContext() {
super(null);
}
#Override
public String getAuthenticationScheme() {
return "unit-test-scheme";
}
}
#Test
public void securityContextTest() throws Exception {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(new Service());
ResteasyProviderFactory.getContextDataMap().put(SecurityContext.class, new FakeSecurityContext());
MockHttpRequest request = MockHttpRequest.get("/");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals("unit-test-scheme", response.getContentAsString());
}
}
For those coming across this issue today, adding Contexts has been moved from the RestEasyProviderFactory class into the Dispatcher class using getDefaultContextObjects() method.
I've edited the old answer with the new call:
public class SecurityContextTest {
#Path("/")
public static class Service {
#Context
SecurityContext context;
#GET
public String get(){
return context.getAuthenticationScheme();
}
}
public static class FakeSecurityContext extends ServletSecurityContext {
public FakeSecurityContext() {
super(null);
}
#Override
public String getAuthenticationScheme() {
return "unit-test-scheme";
}
}
#Test
public void securityContextTest() throws Exception {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(new Service());
dispatcher.getDefaultContextObjects().put(SecurityContext.class, new FakeSecurityContext());
MockHttpRequest request = MockHttpRequest.get("/");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals("unit-test-scheme", response.getContentAsString());
}
}

How to write a Mock Repository to test WCF RIA Domain Service build on top of Entity Framework Context

I need to write a test layer to Test my WCF RIA Domain Service layer which is build on top of Entity Framework context. I have come across some patterns which suggest to use a repository and then use the Domain Service factory to intilize the domain service with a repository instance to use. One of the sample which fits the requirement is explained here on Vijay's blog(http://blogs.msdn.com/vijayu/archive/2009/06/08/unit-testing-business-logic-in-net-ria-services.aspx). The problem with this implementation is that it initilize the repository only for a specific Domain Object e.g. Customer/Product but it provides no way to create a repository which can return any object which i would like to return.
Please suggest what is the right way of doing this and whether it is possible or not.
Thanks in advance,
Manoj
I got around this issue by extending the sample with a RepositoryCollection object, which automatically instantiates LinqToSqlRepositories as needed, and also allows the insertion of mock/stub repositories manually for unit testing.
public class RepositoryCollection : IDisposable
{
private Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
private DataContext _context;
public RepositoryCollection() { }
public RepositoryCollection(DataContext context)
{
_context = context;
}
public IRepository<T> Get<T>() where T : class
{
if(!_repositories.ContainsKey(typeof(T)))
_repositories.Add(typeof(T), new LinqToSqlRepository<T>(_context));
return _repositories[typeof(T)] as IRepository<T>;
}
public RepositoryCollection Insert<T>(IRepository<T> repository) where T : class
{
_repositories[typeof(T)] = repository;
return this;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void SubmitChanges()
{
if (_context != null)
_context.SubmitChanges();
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if(_context != null)
_context.Dispose();
}
}
}
Then, in your domain service, you use it like so:
private RepositoryCollection _repositoryCollection;
public MyDomainService(RepositoryCollection repositoryCollection = null)
{
_repositoryCollection = repositoryCollection ?? new RepositoryCollection(new MyDataContext());
}
public IQueryable<Customer> GetCustomers()
{
return _repositoryCollection.Get<Customer>().Query();
}
public IQueryable<Product> GetProducts()
{
return _repositoryCollection.Get<Product>().Query();
}
.. other methods go here ...