เริ่มต้นติดตั้ง Citrus Simulator

Panuwat Srisawat
Sep 5, 2018 · 2 min read

Post นี้จะมาแนะนำการเริ่มต้นติดตั้ง Citrus Simulator เพื่อใช้ในการทำ Mock Server ในการทำ Integration Testing

ความต้องการ

ความต้องการเบื้องต้นคือ ต้องการทดสอบระบบแบบ End-to-End ตั้งแต่ต้นทางไปปลายทาง ซึ่งในระบบนั้นมีการเรียกใช้งาน 3rd party Service ข้างนอกหลายตัว แต่ครั้งนี้เราไม่ต้องการจะไปเรียก Service เหล่านั้นจริงๆ เราจึงมองหา Tool ที่จะมาช่วย Mock Service ข้างนอกเหล่านี้ และ Tool นั้นต้องสามารถรันอยู่ใน Environment ของเราได้ ไม่ต้องเรียกออก Internet

เรามาเจอ Tool ตัวหนึ่งอยู่ภายใต้โปรเจค Citrus Framework (Citrus เป็น Tool ที่ใช้ทำ Integration Test) ชื่อว่า “Citrus Simulator” ซึ่งเอาไว้ทำเป็น Mock Server

นั้นจึงตอบโจทย์ความต้องการของเรา

ลงมือ

Citrus Simulator ตัวนี้ใช้งานร่วมกับ Spring Boot เพื่อความง่าย จึงไปสร้างโปรเจคโดยใช ้Spring Initializr เลือก Dependencies มี “Web” อย่างเดียว แล้วเลือกเป็นโปรเจคแบบ Maven และ Spring Boot เวอร์ชั่น 1.5.10.RELEASE

พอดาวน์โหลดโปรเจคมาแล้วก็เริ่มลงมือทำได้เลย เริ่มต้น เราต้องไปแก้ไข pom ที่ได้มาเพื่อใส่ Dependencies ของ Citrus Simulator และ Simulator UI ตาม code ด้านล้างนี้ ส่วนสาเหตุที่ใช้ Spring Boot เวอร์ชั่น 1.5.10.RELEASE เนื่องจากพอเป็น 2.x.x แล้วเจอปัญหา Response ไม่คืน Message ตามที่ตั้งไว้

Citrus Simulator Dependencies

เมื่อแก้ pom เสร็จแล้วก็เปิด command line รัน
mvn clean install spring-boot:run
แล้วเรียก browser ไปที่ http://localhost:8080 แล้วเราก็จะเจอหน้าจอด้านล้างนี้

simulator-ui

ถ้าขึ้นมาตามนี้แสดงว่า Simulator เรารันขึ้นได้แล้ว สิ่งที่ต้องทำต่อไป คือ การเพิ่ม Scenario (Scenario คือ Endpoint ที่เราอยากให้มีในตัว Mock Server)

วันนี้เราจะเพิ่ม Scenario เข้าไป 2 ตัวเป็นตัวอย่าง มีดังนี้

  1. Default Scenario — GET /services/rest/
  2. Create New Order — POST/services/rest/order/

Default Scenario

Default Scenario จะเป็น Endpoint ที่จะถูกเรียกทุกครั้งเมื่อ Request ที่เข้ามาไม่ตรงกับ Endpoint ใดๆ ก็จะเข้ามาที่ Default Scenario ตัวนี้

เริ่มจาก สร้าง Class ขึ้นมาโดยให้
1. Extends AbstractSimulatorScenario
2. Annotation “Scenario”
3. Override method run

จากตัวอย่าง Code ด้านล่างนี้ จะรับ Request จาก Get Method และจะคืน Status code 200 และ Message “This is a default response!” กลับไป เมื่อใช้ Rest Client เรียกดู จะได้ Status code 200 และ Message กลับไป

คำสั่ง Requestcurl -X GET http://localhost:8080/services/rest

Create New Order Scenario

ต่อไปเราจะสร้างอีก Scenario เพื่อเป็น Endpoint จากตัวอย่างนี้ สร้าง Service ที่รับ POST เข้ามา ตั้งชื่อให้ว่า “CreateNewOrder” ดู Code ได้ตามด้านล่าง และเมื่อเราลองยิง Request เข้าไปแล้ว เราก็ต้องได้ Status code 200 และ Message “New Order has been created…” กลับมา

คำสั่ง Request

curl -X POST \
http://localhost:8080/services/rest/order \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: 4492e821-f2ab-73cc-b65d-a61f11a2e9eb' \
-d '{
"orderId": "new order"
}'

Citrus Simulator UI

แสดง message ของ Request/Response

ทุกๆ Request/Response ที่ยิงเข้ามาจะถูก Log เก็บไว้ และดูได้ผ่านจาก UI ในตอนต้นที่เราได้เปิดไปแล้ว localhost:8080 ลองเข้าไปดูจะเห็นกราฟบอก Status Request/Response เช่น Total, Passed, Failed ไปเท่าไหร่…

ลองกดเข้าไปที่ Tab Message จะแสดงหน้าบอกรายละเอียดหน้าตาของ Message หรือ Payload ด้วย

เพียงเท่านี้เราก็ได้ Mock Server แบบง่ายๆ มาใช้งานแล้ว คล้ายๆ เขียน Spring boot ขึ้นมาเอง แล้วก็มี UI ให้ดู Log ด้วยก็ถือว่าสะดวกดี

Links

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade