Skip to content

Commit afd97ac

Browse files
committed
Updated readme & added one test case
1 parent 8a9504e commit afd97ac

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

README.md

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
[![Build Status](https://travis-ci.org/code-brewery/untappd.svg?branch=master)](https://travis-ci.org/code-brewery/java-ormen)
22
[![codecov.io](http://codecov.io/github/code-brewery/java-ormen/coverage.svg?branch=master)](http://codecov.io/github/code-brewery/java-ormen?branch=master)
33
# java-ormen
4-
Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database.
4+
Java-ormen is an Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database.
55

6-
The Async java-ormen library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses, without the developer needing to think about the http requests.
6+
Thos async library purpose is to allow Java applications to easily execute HTTP requests over an REST API, and create an abstraction on top of REST, so that only the CRUD operations are exposed.
77

8-
The library creates an abstraction layer on top of REST. The user of this library will in fact know very little about what protocol is used.
9-
10-
The library has been designed using the Future API. If you you don't need a ascynchronous library you can tell the library to execute query's synchronous.
8+
The library has been designed using the Java CompletableFuture. If you you don't need a ascynchronous library you can tell the library to execute query's synchronous.
119

1210
The library uses the Jackson ObjectMapper, so its possible to use jackson annotations to describe how objects shall be marshalled and unmarshalled.
1311

14-
The library strives for creating immutable objects.
12+
The library strives for creating immutable objects. This means that you can have control over your objects.
1513

1614
## Usage
1715

1816
Lets say that we have a REST API that exposes CRUD Operations for fetching Dog´s. Well dogs can bark, so we will use dogs in this example.
19-
20-
As an developer you will probably create some POJOS for a java representation of the Dogs. Then you will probably write some kind of handler that takes these objects and sends them
17+
18+
As an developer you will probably create some POJOś as an a java representation of the Dogs. Then you will probably write some kind of handler that takes these POJO instance´s and sends them
2119

2220
back and forth the REST API. Well, you will not need to do that any longer.
2321

2422

2523
One just simply do the following:
2624

27-
Start of by creating a normal POJO, but we will make sure it extends AbstractRestModel. This will give the POJO super powers ( almost ). Well it will give the pojo possibility interact with an rest api.
25+
Start of by creating a normal POJO, but we will make sure it extends RestModel. This will give the POJO super powers ( almost ). Well it will give the pojo possibility interact with an rest api.
2826

29-
The AbstractRest model gives you 4 public methods for interacting with the rest api, one for each CRUD operation. The good part here is that these methods takes an ActionCompletedinterface as parameter, so you can work with async request. More about that later. here is the code.
27+
The Rest model gives you 4 public methods for interacting with the rest api, one for each CRUD operation. The good part here is that these methods returns a Completable future.
3028

29+
Look at the comments, it will give you some hints about what information you need to provide for successfully send requests over rest.
3130

3231
```java
3332

34-
public class DogModel extends RESTModel {
33+
public class MockRestModel extends RESTModel {
3534

36-
public final String name;
37-
38-
public DogModel() {
35+
/**
36+
* Default constructor, this is needed by the jackson json mapper. so always include it.
37+
**/
38+
public MockRestModel() {
3939
this.name = "";
4040
}
41-
public DogModel(String name) {
41+
42+
public MockRestModel(String name) {
4243
this.name = name;
4344
}
44-
45+
/**
46+
*
47+
* The return value of this method will indicate the resource url. The resource url points to the collection of resources that the pojo targets
48+
**/
4549
@Override
4650
String resourceUrl() {
4751
return "dogs";
4852
}
49-
53+
/**
54+
* This method returns the identifying data for this resource. for example this might be this.id or this.name or something else.
55+
*
56+
**/
5057
@Override
5158
String identifierValue() {
5259

@@ -56,50 +63,36 @@ public class DogModel extends RESTModel {
5663

5764

5865
}
59-
6066
```
6167

62-
63-
The Dog model has a method called ```resourceUrl```. This url will point on to the url location where we can work on dogs. The Dog model has a method called ```identifierValue```. It will be appended on the url on RUD operations. The good part here is that we will not need to write any marshall or unmarshalling logic. This is handled the RestModel class. But if you get into truble you can use jackson json annotations on your fields.
68+
The good part with this model is that we will not need to write any marshall or unmarshalling logic. This is handled by the RestModel class. But if you get into trouble you can use jackson json annotations on your fields. For example there might be times when you want to exclude some data from de json marshalling, or perhaps rename.
6469

6570

6671
So now we have created a model. Its now time to use it to fetch data. Simply write the following in your code. Remember, these operations are async so we will add a sleep in the end ( only for educational purpose ).
6772

6873

6974
```java
70-
public class App
71-
{
72-
public static void main( String[] args ) throws ExecutionException, InterruptedException, IOException {
73-
// create a new client
74-
7575

76-
System.out.println("fetching the dog");
76+
77+
// 1. create a dog
7778
DogModel pluto = new DogModel();
78-
pluto.fetch(new ActionCompletedInterface() {
79-
public void onDone(RESTModel model) {
80-
81-
DogModel fetchedModel = (DogModel)model;
82-
83-
System.out.println("it went good " + fetchedModel.bark());
84-
85-
}
86-
87-
public void onError(Throwable e) {
88-
89-
System.out.println("it went bad" + e.getMessage());
90-
}
91-
});
92-
79+
80+
// 2. start to fetch the dog, take care of the completable future.
81+
CompletableFuture<RESTModel> futurePluto = pluto.fetch();
82+
83+
84+
future.thenApply(result -> isDone(result)); // sync callback
9385

9486
Thread.sleep(4000);
9587
System.out.println("dying");
9688

97-
}
98-
}
89+
90+
9991
```
10092

101-
In the code above, the dog model will call the rest api when the fetch method is invoked. When the server responds the onDone or onError will be fired.
93+
In the code above, the dog model will call the rest api when the fetch method is invoked. A CompletableFuture is returned, and we can work with that future as an promise.
10294

10395

10496

10597
# LICENSE
98+
The MIT License (MIT)

src/test/java/org/codebrewery/TestAbstractRestModel.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void testThatFetchReturnsRightUrl() {
138138

139139

140140
@Test
141-
public void testThatPutReturnsRightUrl() throws IOException {
141+
public void testThatPostReturnsRightUrl() throws IOException {
142142
CompletableFuture<RESTModel> future = mockModel.create();
143143
Request request = mockModel.getBoundRequestBuilder().build();
144144

@@ -147,16 +147,30 @@ public void testThatPutReturnsRightUrl() throws IOException {
147147
}
148148

149149
@Test
150-
public void testThatPostReturnsRightUrl() {
150+
public void testThatDeleteReturnsRightUrl() {
151151
CompletableFuture<RESTModel> future = mockModel.destroy();
152152
Request request = mockModel.getBoundRequestBuilder().build();
153153

154154
assertEquals("http://localhost:8081/api/dogs/identifier",request.getUrl());
155155

156156
}
157157

158+
@Test
159+
public <U> void testThatUpdateReturnsRightUrl() throws IOException {
160+
CompletableFuture<RESTModel> future = mockModel.update();
161+
Request request = mockModel.getBoundRequestBuilder().build();
162+
future.thenApply(result -> isDone(result)); // sync callback
163+
164+
assertEquals("http://localhost:8081/api/dogs/identifier", request.getUrl());
158165

166+
}
167+
168+
private Void isDone(RESTModel result) {
169+
MockRestModel model = (MockRestModel)result;
170+
System.out.println(model.name);
159171

172+
return null;
173+
}
160174

161175

162176
}

0 commit comments

Comments
 (0)