# Building a mock API server for the ScholarX API

**URL:** <https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186>\
**Category:** ScholarX\
**Created:** [October 16, 2020, 3:06pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186 "2020-10-16T15:06:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaye](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/jaye/32/810_2.png) [@jaye](https://sef.discourse.group/u/jaye)\
**Post date:** [October 16, 2020, 3:06pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/1 "2020-10-16T15:06:08Z")

</div>

Hello there,

Since the API gateway task takes a few more days to get delivered and we are running out of time for the initial phase, building a mock server is essential to continue the frontend development. The requirement is to build a mock API server that is smart enough to simulate the real API.

I thought of initializing this task, I’ll update my work with this thread.

---

<div class="post-metadata">

**Author:** ![jaye](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/jaye/32/810_2.png) [@jaye](https://sef.discourse.group/u/jaye)\
**Post date:** [October 16, 2020, 5:34pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/2 "2020-10-16T17:34:58Z")

</div>

Hi all,

I went through major mock server applications including [Postman](https://www.postman.com/) and [Insomnia](https://insomnia.rest/). The problem is, those platforms have their own limitations. ex: a limited number of requests.

In my humble opinion, one of the ideal solutions would be invoking the API service directly for the developments. (Thanks @Gravewalker for the silly idea! 🤗 😁) There are some unimplemented APIs, shall we implement only the controllers for those APIs until they get completed?

However, I used Postman to test the available APIs. While testing them I created a collection too… Click this handy button to add it to your postman,  
[![Run in Postman](https://global.discourse-cdn.com/free1/uploads/sef/original/1X/62001d431221743f8671b6517c58dda2fdd82996.svg)](https://app.getpostman.com/run-collection/65abcbf35132f80a5218)  
Thanks, @piumal1999 and @YohanAvishke you have done a great job with the backend!

I’ve found the following bugs,

**POST admin/programs**

- The state should not send as a parameter - because it’s always should be in the `CREATED` state
- The request body isn’t validated. ex: throws 500 when a not null field is not available in the body
- If the landing page URL or image URL is not in the body, it adds a null value for them. It should return a bad request.

**PUT admin/programs/{id}/state**

- Throws 400.

```auto
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: 
Cannot deserialize instance of `org.sefglobal.scholarx.util.ProgramState` out of START_OBJECT token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot deserialize instance of `org.sefglobal.scholarx.util.ProgramState` out of START_OBJECT token

```

But I’m not sure if I sent the correct payload. This is what I sent: `{"state": "CREATED"}`

**DELETE /admin/programs/{id}/state**

- Works fine, but send an empty payload as the response. It should have a body.

**Other:**

- No endpoint to edit a program

(I didn’t check other admin APIs because apply as a mentee and mentor APIS aren’t available)

---

<div class="post-metadata">

**Author:** ![piumal1999](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/piumal1999/32/274_2.png) [@piumal1999](https://sef.discourse.group/u/piumal1999)\
**Post date:** [October 17, 2020, 10:36am UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/3 "2020-10-17T10:36:30Z")

</div>

> [@jaye](#):
>
> But I’m not sure if I sent the correct payload. This is what I sent: `{"state": "CREATED"}`

Only the state is needed to be sent. Like this:  
`"CREATED"`

> [@jaye](#):
>
> **DELETE /admin/programs/{id}/state**
> 
> - Works fine, but send an empty payload as the response. It should have a body

Like a boolean response? (Return true if it is deleted properly) or a String saying the object is deleted

> [@jaye](#):
>
> **POST admin/programs**
> 
> - The state should not send as a parameter - because it’s always should be in the `CREATED` state
> - The request body isn’t validated. ex: throws 500 when a not null field is not available in the body
> - If the landing page URL or image URL is not in the body, it adds a null value for them. It should return a bad request

I’ll fix this.

---

<div class="post-metadata">

**Author:** ![jaye](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/jaye/32/810_2.png) [@jaye](https://sef.discourse.group/u/jaye)\
**Post date:** [October 17, 2020, 10:42am UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/4 "2020-10-17T10:42:41Z")

</div>

> [@piumal1999](#):
>
> Only the state is needed to be sent. Like this:  
> `"CREATED"`

Better if we could convert it to a JSON object

> [@piumal1999](#):
>
> Like a boolean response? (Return true if it is deleted properly) or a String saying the object is deleted

Send a JSON payload

---

<div class="post-metadata">

**Author:** ![piumal1999](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/piumal1999/32/274_2.png) [@piumal1999](https://sef.discourse.group/u/piumal1999)\
**Post date:** [October 18, 2020, 5:20pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/5 "2020-10-18T17:20:40Z")

</div>

> [@jaye](#):
>
> **PUT admin/programs/{id}/state**
> 
> - Throws 400.
> 
> ```auto
> Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: 
> Cannot deserialize instance of `org.sefglobal.scholarx.util.ProgramState` out of START_OBJECT token;
> nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
> Cannot deserialize instance of `org.sefglobal.scholarx.util.ProgramState` out of START_OBJECT token
> 
> ```
> 
> But I’m not sure if I sent the correct payload. This is what I sent: `{"state": "CREATED"}`

In the current version, the ‘update state endpoint’ only switches the state to the next stage. So a payload will not be sent and only the id will be sent(as a path parameter).

---

<div class="post-metadata">

**Author:** ![jaye](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/jaye/32/810_2.png) [@jaye](https://sef.discourse.group/u/jaye)\
**Post date:** [October 18, 2020, 6:29pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/6 "2020-10-18T18:29:31Z")

</div>

That’s great! 😇

---

<div class="post-metadata">

**Author:** ![piumal1999](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/piumal1999/32/274_2.png) [@piumal1999](https://sef.discourse.group/u/piumal1999)\
**Post date:** [November 14, 2020, 7:27pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/7 "2020-11-14T19:27:53Z")

</div>

> [@jaye](#):
>
> **PUT admin/programs/{id}/state**
> 
> - Throws 400.
> 
> ```auto
> Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: 
> Cannot deserialize instance of `org.sefglobal.scholarx.util.ProgramState` out of START_OBJECT token;
> nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
> Cannot deserialize instance of `org.sefglobal.scholarx.util.ProgramState` out of START_OBJECT token
> 
> ```
> 
> But I’m not sure if I sent the correct payload. This is what I sent: `{"state": "CREATED"}`

The same problem is in the Approve/reject mentor endpoint

In the “approve/reject mentor endpoint” we have, the RequestBody is only an Enrollment state(not a JSON payload). But, it have to be a json payload. The solution i found to fix this is creating a custom Dto class for the payload.  
Is there a better solution for this? @jaye @YohanAvishke

---

<div class="post-metadata">

**Author:** ![YohanAvishke](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/yohanavishke/32/106_2.png) [@YohanAvishke](https://sef.discourse.group/u/YohanAvishke)\
**Post date:** [November 14, 2020, 7:36pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/8 "2020-11-14T19:36:41Z")

</div>

We can solve this easily by adding `@JsonValue` annotation to `getState()` method in `Mentor` class. There are other possible ways as well please refer to below link and choose the most suitable option

> **[How To Serialize and Deserialize Enums with Jackson | Baeldung](https://www.baeldung.com/jackson-serialize-enums)**
>
> How to serialize and deserialize an Enum as a JSON Object using Jackson 2.

---

<div class="post-metadata">

**Author:** ![piumal1999](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/piumal1999/32/274_2.png) [@piumal1999](https://sef.discourse.group/u/piumal1999)\
**Post date:** [November 18, 2020, 8:10pm UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/9 "2020-11-18T20:10:13Z")

</div>

I tried doing this, but after using `@JsonValue` annotation with the `getState` method in `enrolledUser`, some tests are not working (update mentor data, apply as a mentor) However it changes nothing.

What if we do something like this for now:  
We get a `mentor` object as the request payload. And we can get the state from it. For that, i think this kind of json payload will be enough:

```auto
    {
        "state" : "APPROVED"
    }

```

---

<div class="post-metadata">

**Author:** ![jaye](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/jaye/32/810_2.png) [@jaye](https://sef.discourse.group/u/jaye)\
**Post date:** [November 19, 2020, 6:57am UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/10 "2020-11-19T06:57:00Z")

</div>

> [@piumal1999](#):
>
> We get a `mentor` object as the request payload. And we can get the state from it. For that, i think this kind of json payload will be enough:

This seems like a hack right?  
How about using like this `@RequestBody Map<String, String> payload` ?

or `@RequestBody Map<String, Object> payload`

---

<div class="post-metadata">

**Author:** ![piumal1999](https://yyz2.discourse-cdn.com/free1/user_avatar/sef.discourse.group/piumal1999/32/274_2.png) [@piumal1999](https://sef.discourse.group/u/piumal1999)\
**Post date:** [November 19, 2020, 7:44am UTC](https://sef.discourse.group/t/building-a-mock-api-server-for-the-scholarx-api/186/11 "2020-11-19T07:44:48Z")

</div>

> [@jaye](#):
>
> How about using like this `@RequestBody Map<String, String> payload` ?
> 
> or `@RequestBody Map<String, Object> payload`

This one is fine. I think we can use this for that boolean payload also
