シミュレーションファイルの例
Tosca Cloud は、サンプルワークスペースにサンプルシミュレーションファイルを提供します。このワークスペースを選択して、それらを探索してください。
このトピックでは、各例について説明します。
The hello_world_test.yml file is a simple simulation that sends a request and verifies the response.サービスとステップがどのように機能するかの基本を示します。
仕組み:
-
Send the request: The step with the Out direction specifies that the simulation sends a request to the /hello endpoint .
-
Verify the response: The verify rule step checks if the response includes the expected value: Hello world!.
schema: SimV1
name: default
includes:
- hello_world
services:
- steps:
- direction: Out
insert:
- uri: /hello
- verify:
- value: Hello world!
The hello_world.yml file is a basic simulation that listens for any incoming request and always responds with Hello world!.
仕組み:
-
Define the connection: The simulation opens a connection named hello on port 17071.ここで受信リクエストをリッスンします。
-
Trigger the service: The URI with the wildcard * means that the service handles any request path.
-
Send the response: The Out step specifies the simulation's response, which includes a message containing the text Hello world!.
schema: SimV1
name: default
connections:
- name: hello
port: 17071
services:
- steps:
- direction: In
trigger:
- uri: '*'
- direction: Out
message:
payload: Hello world!
The bank_account.yml is a simulation that models a basic bank account service with simple operations, such as creating an account and making deposits.
仕組み:
-
Create account: When a request is sent to /create, the service generates a random account ID {RND} and stores it as a buffer.It then responds with: “Thank you for opening an account!あなたのアカウントIDは {B[id] } です。」The value {B[id]} is the random number from the buffer that was stored before.
-
First deposit: A request to /deposit/{B[id]} triggers the simulation and stores the deposit amount as the initial balance.It responds with the message: “Thank you for your first deposit!現在の残高は{B[balance]}です。」The value {B[balance]} is the current balance from the buffer that was stored before.
-
Subsequent deposits: For each following deposit request, the service adds the deposit amount to the current balance using the math operation {MATH[{B[balance]} + {B[amount]}]}.It responds with the updated balance using the message: “Your current balance is {B[balance]}”.
-
Transaction limit: After three deposits, any additional /deposit/{B[id]} requests return the message: “Too many transactions for today.明日再度お試しください。」
schema: SimV1
name: bank account
connections:
- name: bank
port: 17073
services:
- name: bank service
steps:
- direction: In
trigger:
- uri: /create
buffer:
- name: id
value: '{RND}'
- direction: Out
insert:
- value: Thank you for opening an account! Your account ID is {B[id]}
- direction: In
trigger:
- uri: /deposit/{B[id]}
buffer:
- name: balance
- direction: Out
insert:
- value: Thank you for your first deposit! Your current balance is {B[balance]}
- direction: In
trigger:
- uri: /deposit/{B[id]}
buffer:
- name: amount
- name: balance
value: '{MATH[{B[balance]} + {B[amount]}]}'
- direction: Out
insert:
- value: Your current balance is {B[balance]}
- direction: In
trigger:
- uri: /deposit/{B[id]}
buffer:
- name: amount
- name: balance
value: '{MATH[{B[balance]} + {B[amount]}]}'
- direction: Out
insert:
- value: Your current balance is {B[balance]}
- direction: In
trigger:
- uri: /deposit/{B[id]}
- direction: Out
insert:
- value: To many transactions for today. Please try again tomorrow.
The calculator_test.yml file is a simulation for a calculator service.これには計算機の接続が含まれ、サービスが四則演算を正しく実行することを確認します。
仕組み:
-
Addition test: Sends a request with 7 + 77 and verifies that the response has the correct sum of 84.
-
Multiplication test: Sends a request with 7 × 7 and verifies that the response contains the correct product, 49.
-
Subtraction test: Sends a request with 7 – 7 and verifies that the response contains the correct difference, 0.
-
Division test: Sends a request with 7 ÷ 7 and verifies that the response contains the correct quotient, 1.
schema: SimV1
name: calculator contract test
includes:
- calculator
services:
- name: test addition
steps:
- direction: Out
to: calculator
message:
payload: '{ "augend": 7, "addend": 77 }'
insert:
- uri: /addition
- direction: In
verify:
- jsonPath: sum
value: 84
- name: test multiplication
steps:
- direction: Out
to: calculator
message:
payload: '{ "multiplier": 7, "multiplicand": 7 }'
insert:
- uri: /multiplication
- direction: In
verify:
- jsonPath: product
value: 49
- name: test subtraction
steps:
- direction: Out
to: calculator
message:
payload: '{ "minuend": 7, "subtrahend": 7 }'
insert:
- uri: /subtraction
- direction: In
verify:
- jsonPath: difference
value: 0
- name: test division
steps:
- direction: Out
to: calculator
message:
payload: '{ "dividend": 7, "divisor": 7 }'
insert:
- uri: /division
- direction: In
verify:
- jsonPath: quotient
value: 1
The calculator.yml file simulates a calculator service that performs the four basic arithmetic operations addition, subtraction, multiplication, and division.バッファ値と数式を使用します。
仕組み:
-
サービスをトリガーする:トリガーは、どのエンドポイント(URI)がサービスを起動するかを定義します。In this example, these are /addition, /subtraction, /multiplication and /division.
-
Extract values from the request: The incoming JSON request is expected to look like this: { "augend": 7, "addend": 77 }.The buffer uses the JsonPath expression to extract values from the request and store them as buffer variables.For example, the jsonPath augend means "find the property augend in the incoming JSON and store its value in a variable named augend".
-
応答ペイロードを準備する:ペイロードは応答の構造を定義します。For example, the payload value { "sum": 0 } sets up the response format with a placeholder field.
-
計算結果を挿入する:挿入ルールはプレースホルダーを実際の計算結果で置き換えます。For example, the value {math[{b[augend]} + {b[addend]}]} means, "replace the field sum in the response with the sum of the buffered values augend and addend".
schema: SimV1
name: calculator2
connections:
- name: calculator
port: 17072
services:
- name: addition
steps:
- direction: In
trigger:
- uri: /addition
buffer:
- jsonPath: augend
name: augend
- jsonPath: addend
name: addend
- direction: Out
message:
payload: '{ "sum": 0 }'
insert:
- jsonPath: sum
value: '{math[{b[augend]} + {b[addend]}]}'
- name: subtraction
steps:
- direction: In
trigger:
- uri: /subtraction
buffer:
- jsonPath: minuend
name: minuend
- jsonPath: subtrahend
name: subtrahend
- direction: Out
message:
payload: '{ "difference": 0 }'
insert:
- jsonPath: difference
value: '{math[{b[minuend]} - {b[subtrahend]}]}'
- name: multiplication
steps:
- direction: In
trigger:
- uri: /multiplication
buffer:
- jsonPath: multiplier
name: multiplier
- jsonPath: multiplicand
name: multiplicand
- direction: Out
message:
payload: '{ "product": 0 }'
insert:
- jsonPath: product
value: '{math[{b[multiplier]} * {b[multiplicand]}]}'
- name: division
steps:
- direction: In
trigger:
- uri: /division
buffer:
- jsonPath: dividend
name: dividend
- jsonPath: divisor
name: divisor
- direction: Out
message:
payload: '{ "quotient": 0 }'
insert:
- jsonPath: quotient
value: '{math[{b[dividend]} / {b[divisor]}]}'