Giter Site home page Giter Site logo

nirodg / easyrs Goto Github PK

View Code? Open in Web Editor NEW
6.0 6.0 1.0 155 KB

This library provides an easy way to test the basic CRUD operation for your endpoints. The tests can be executed within Arquillian or as a Singleton application

License: MIT License

Java 100.00%
arquillian crud-test javaee jax-rs junit singleton

easyrs's People

Contributors

gitter-badger avatar nirodg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gitter-badger

easyrs's Issues

Importing data

In order to test the Entity it must exist some data in order to proceed with the testing.
Would be a good approach to provide a JSON file for each Entity which will contain data ?

Implement Log4J during compile time

During the compile time it should be displayed relevant data regarding the annotated class.
Here is how it should look like:

[INFO] Compiling 1 source file to C:\code\github\earyrs-proc\mytestproject\target\test-classes
...
[EasyRS] Generating UserRestTestEnpoint.class [ GET_ALL, GET, PUT, POST, DELETE ]
[EasyRS] Generating BookRestTestEnpoint.class [ GET, DELETE ]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

JaxRs Client

A jaxrs client is needed in order to do

  • GET It should return single and multiple results

  • PUT It should return single result

  • POST It should return the update result

  • DELETE Shall this return boolean or better check the 200 header response ?

[JaxRS Client] Add authentication

Implement/refactor the Client so it's able to make the request for secured hosts

private String username;
private String password;

// GETTERS and SETTERS

Execution test mode

Short description: Provide a single/multiple execution mode

Implement two classes :

  • SingleTest: The CRUD operation would be executed inside a unique method
  • IndependentTest: For each CRUD operation we must have a separate test

Use Annotation Processing

For each class with uses the custom annotation, it will generate automatically the java class .
The generated file will extend the abstract class which contains all the configuration in order to be deployed and executed by Arquillian

The annotated class should looks like

@IndependentTest(dto = UserDto.class, endpoint = UserEndpoint.class)
public class User {
    // Leave the class empty
}

Where UserDTO should be our entity to be tested

public class UserDto {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Where UserEndpoint is our interface with all the path. It should contain at least the basic CRUD operations which are: GET, PUT, POST and DELETE

@Path("/users")
public interface UserEndpoint {

    //  [...]
    
    @GET
    @Path("/")
    public List<UserDto> getAll();

   //  [...]
}

Endpoint's path is not fetched

The RestClient always appoints to the same direction, instead of appointing based on the endpoint which has to be tested.

Declare endpoint as a required field on EnpointTest's annotation and fetch the final endpoint throw Path's annotation

Clean generated class

  • Use simple name instead of canonical one for the DTO
  • Possibility to add empty line inside the methods
  • Add @Generated before the class

It should look like

package com.dorinbrage.github.jme.note.rest;

// Here the imports

@SuppressWarnings("unchecked")
@Generated(
      value = "com.dbrage.lib.easyrs.processor", 
      date = "2017-02-20T18:16:47+0100",
      comments = "version: 1.0.0.CR2, compiler: javac, environment: Java 1.8.0_65 (Oracle Corporation)"
)
public class NoteRestTestEndpoint extends Container<NoteDto, NoteRest> {

  // Here you can define your global variables

  @Before
  public void setUp() {
    // Here you can initialize your variables

  }

  @Test
  public void getAll() {
    List<NoteDto> entities = (ArrayList<NoteDto>) getData(GET_ALL);
    List<NoteDto> fetchedEntities = (ArrayList<NoteDto>) getClient().getAll();
    Assert.assertEquals(entities.size(), fetchedEntities.size());
  }

  @Test
  public void create() {
    NoteDto entity = (NoteDto) getData(PUT);
    Assert.assertNotNull(entity);

    NoteDto fetchedEntity = (NoteDto) getClient().put(entity);
    Assert.assertNotNull(fetchedEntity);
    Assert.assertEquals(entity, fetchedEntity);

  }

  @Test
  public void update() {
    NoteDto entity = (NoteDto) getData(POST);
    Assert.assertNotNull(entity);

    entity = (NoteDto) getClient().put(entity);
    Assert.assertNotNull(entity);

    NoteDto fetchedEntity = (NoteDto) getClient().post(entity.getGuid(), entity);
    Assert.assertNotNull(fetchedEntity);
    Assert.assertEquals(entity, fetchedEntity);
  }

  @Test
  public void delete() {
    NoteDto entity = (NoteDto) getData(DELETE);
    Assert.assertNotNull(entity);

    entity = (NoteDto) getClient().put(entity);
    Assert.assertNotNull(entity);

    NoteDto fetchedEntity = (NoteDto) getClient().delete(entity.getGuid());
    Assert.assertNull(fetchedEntity);
  }

}

Map JSON to Object

Short description: Implement function to map a json file to an object via reflection.
As a first approach it should map primitive and simple objects (String, Long, Double), then map Lists, ArrayList and similar.

Running mode

The generated class should be executed either with Arquillian or as a Singleton application

Cleanup the project

  • Unused imports

  • Rename packaged using the common sense and good practice

  • Check unused dependencies

Change groupId

Due it gonna be publish onto Sonatype's repository and the new requested groupId was approved therefore is will be changed to com.dorinbrage and the artifact will be easyrs

<groupId>com.dorinbrage</groupId>
<artifactId>easyrs</artifactId>

Provide GUI/ID field

When the class is generated, it might contain errors due the DTO to be tested might contain or not getGuid() or either getId(), so if it doesn't contain such field it will complain.

Improve the generated class

Due each method is an independent test each method we should persist first the data and afterwords proceed to execute the proper crud operation depending on the method which is being tested.

An example about how getAll() shoud look like

  @Test
  @InSequence(1)
  public void getAll() {
  	
  	UserDto persistDto = Data.importData();
  	persistDto = client.put(dto);
  	
  	Assert.assertNotNull(persistDto);
  	
  	List<UserDto> getPersistedEntities = client.get();
  	Assert.assertNotNull(getPersistedEntities);
  	
  	Assert.assertEquals(1, getPersistedEntities.size());
  	
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.