Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Handler Test

Yuichi Morimichi edited this page Sep 14, 2018 · 1 revision

Requirements

Handler Test

Test Spring Boot to write a Controller test. clova-extension-test helps with building the CEK Request.

Request Body Template

Prepare the following JSON file under the classpath.

{
  "version": "1.0",
  "session": {
    "sessionId": "2bcb5fac-3a51-4ce8-812d-3674ef7a701a",
    "user": {
      "userId": "V6Htd1UjREmIFY5tv6IfCQ",
      "accessToken": "ddbd424b-6bb0-4baf-b9f5-ce2c303133fa"
    },
    "new": true,
    "sessionAttributes": {}
  },
  "context": {
    "System": {
      "application": {
        "applicationId": ""
      },
      "user": {
        "userId": "V6Htd1UjREmIFY5tv6IfCQ",
        "accessToken": "ddbd424b-6bb0-4baf-b9f5-ce2c303133fa"
      },
      "device": {
        "deviceId": "05d87b6b-c814-40c8-843e-0d74ff7a8ec8",
        "display": {
          "size": "l100",
          "orientation": "landscape",
          "dpi": 96,
          "contentLayer": {
            "width": 640,
            "height": 360
          }
        }
      }
    }
  },
  "request": {
    "type": "${requestType}",
    "intent": {
      "name": "${intent}",
      "slots": {}
    },
    "event": {
      "namespace": "${event.namespace}",
      "name": "${event.name}",
      "payload": null
    }
  }
}

Any placeholders can be added as ${...}.

Building Request Body

Construct JSON of Request Body. Specify the path of the template file and build it.

String body = CEKRequestGenerator.requestBodyBuilder("data/request.json", this.configuration)
                                 .intent("UpdateItemOrder")
                                 .slot("itemName", itemName)
                                 .sessionAttribute("param", param)
                                 .build();

To use the customized ObjectMapper in the application, generate com.jayway.jsonpath.Configuration as follows and pass it to CEKRequestGenerator.requestBodyBuilder(String, Configuration).

@Bean
com.jayway.jsonpath.Configuration jsonPathConfig(ObjectProvider<ObjectMapper> objectMapperProvider) {
    ObjectMapper objectMapper = objectMapperProvider.getIfAvailable(
            () -> Jackson2ObjectMapperBuilder.json().build());
    return com.jayway.jsonpath.Configuration.builder()
                                            .mappingProvider(new JacksonMappingProvider(objectMapper))
                                            .jsonProvider(new JacksonJsonProvider(objectMapper))
                                            .build();
}

Others are the same as when testing Spring MVC's Controller. Refer to xxxxHandlerTest.java in com.linecorp.clova.extension.boot.handler package for details.

Clone this wiki locally