alexa / alexa-skills-kit-sdk-for-java

The Alexa Skills Kit SDK for Java helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
http://developer.amazon.com/ask
Apache License 2.0
817 stars 747 forks source link

fix: adding Null Handler Check and NPE unit test; rename variables #322

Closed tkm22 closed 2 years ago

tkm22 commented 2 years ago
  1. Fix issue causing intent without request handler to throw a NullPointerException (NPE), was not fixed in a historical PR: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/312. isPresent is not the same as check is the Optional object itself is null. isPresent checks if the value of the Optional is null.
  2. add unit test and rename variable.

Description

  1. Fix Null Pointer Exception issue. For details, see this PR: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/320
  2. Add corresponding unit test for this change, which was missing and hence didn't catch the issue.
  3. Rename variable name for better readability.

Motivation and Context

Avoid throwing an NPE in case of missing RequestHandler, instead handle the scenario more gracefully by providing an empty or null response.

Testing

Pass all existing and new unit tests.

From the new unit test no_handler_response_returns_envelope_with_empty_response: Before the null check change:

java.lang.NullPointerException
    at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:149)
    at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:114)
    at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:107)
    at com.amazon.ask.SkillTest.no_handler_response_returns_envelope_with_empty_response(SkillTest.java:92)...

After the null check change,can see the mock responseEnvelope contains

version = "1.0"
sessionAttributes = {HashMap@2337}  size = 0
userAgent = "ask-java/2.43.2 Java/1.8.0_322"
response = null

, which is consistent with the original PR Response JSON format.

Original PR and its testing process, see https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/320

Types of changes

Checklist

License