com.amazon.ask.dispatcher.request.handler.HandlerInput Java Examples

The following examples show how to use com.amazon.ask.dispatcher.request.handler.HandlerInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WhenSessionAttributeTest.java    From ask-sdk-frameworks-java with Apache License 2.0 6 votes vote down vote up
@Test
public void test_nested_value_doesnt_match() throws Exception {
    Predicate<HandlerInput> predicate = buildPredicate("inSessionNested");

    HandlerInput rootDoesntExist = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh");
    HandlerInput valueDoesntMatch = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh", Collections.singletonMap(
        "a", Collections.singletonMap("b", "INVALID")
    ));
    HandlerInput notNested = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh", Collections.singletonMap(
        "a", "B"
    ));
    HandlerInput keyDoesntExist = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh", Collections.singletonMap(
        "a", Collections.singletonMap("c", "value")
    ));

    assertFalse(predicate.test(rootDoesntExist));
    assertFalse(predicate.test(valueDoesntMatch));
    assertFalse(predicate.test(notNested));
    assertFalse(predicate.test(keyDoesntExist));
}
 
Example #2
Source File: AttributesManagerArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            AttributesManager.class,
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    HandlerInput handlerInput = HandlerInput
            .builder()
            .withRequestEnvelope(envelope)
            .build();
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, handlerInput);

    assertSame(handlerInput.getAttributesManager(), resolver.resolve(input).get());
}
 
Example #3
Source File: DialogStatePredicateTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotIntentRequest() {
    HandlerInput handlerInput = HandlerInput.builder()
        .withRequestEnvelope(RequestEnvelope.builder()
            .withRequest(LaunchRequest.builder()
                .build())
            .build())
        .build();

    assertFalse(inProgress.test(handlerInput));
    assertFalse(inProgressOrCompleted.test(handlerInput));
}
 
Example #4
Source File: ControllerRequestMapper.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<RequestHandlerChain> getRequestHandlerChain(HandlerInput input) {
    if (predicate.test(input)) {
        // Both the CONTROLLER & METHOD predicates must match, so we can slightly
        // optimize a scan by short-circuiting it if the controller's predicate
        // yields false.
        return requestMapper.getRequestHandlerChain(input);
    } else {
        return Optional.empty();
    }
}
 
Example #5
Source File: SlotValueArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    Method method = MappingsController.class.getMethod("handleSlotValue", new Class[]{String.class});
    MethodParameter methodParameter = new MethodParameter(
            method,
            0,
            String.class,
            method.getParameterAnnotations()[0]
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertEquals("hola", resolver.resolve(input).get());
}
 
Example #6
Source File: SlotValuesArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    Method method = MappingsController.class.getMethod("handleSlotValues", new Class[]{Map.class});
    MethodParameter methodParameter = new MethodParameter(
            method,
            0,
            Map.class,
            method.getParameterAnnotations()[0]
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertEquals(Collections.singletonMap("GREETING", "hola"), resolver.resolve(input).get());
}
 
Example #7
Source File: SlotValuesArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Object.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #8
Source File: IntentArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Intent.class,
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertSame(((IntentRequest) envelope.getRequest()).getIntent(), resolver.resolve(input).get());
}
 
Example #9
Source File: IntentArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Object.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #10
Source File: TriviaController.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@ExceptionHandler
public Optional<Response> handleException(HandlerInput handlerInput, Throwable throwable) {
    // TODO: Use a view?
    System.out.println("Exception: " + throwable.toString());
    System.out.println("Exception thrown while receiving: " + handlerInput.getRequestEnvelope().getRequest().getType());
    return handlerInput.getResponseBuilder()
        .withSpeech("Sorry. I have problems answering your request. Please try again")
        .withShouldEndSession(true)
        .build();
}
 
Example #11
Source File: WhenSessionAttributeTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test_value_not_in_session() throws NoSuchMethodException {
    Predicate<HandlerInput> predicate = buildPredicate("inSession");
    HandlerInput dispatcherInput = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh");

    assertFalse(predicate.test(dispatcherInput));
}
 
Example #12
Source File: WhenSessionAttributeTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test_value_not_in_session_match_undefined() throws NoSuchMethodException {
    Predicate<HandlerInput> predicate = buildPredicate("inSessionUndef");
    HandlerInput dispatcherInput = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh");

    assertTrue(predicate.test(dispatcherInput));
}
 
Example #13
Source File: WhenSessionAttributeTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test_nested_value_in_session() throws Exception {
    Predicate<HandlerInput> predicate = buildPredicate("inSessionNested");

    HandlerInput dispatcherInput = Utils.buildSimpleSimpleIntentRequest("whatever", "meh", "meh", Collections.singletonMap(
        "a", Collections.singletonMap("b", "A")
    ));

    assertTrue(predicate.test(dispatcherInput));
}
 
Example #14
Source File: RequestEnvelopeArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Object.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #15
Source File: SlotArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    Method method = MappingsController.class.getMethod("handleSlotArg", new Class[]{Slot.class});
    MethodParameter methodParameter = new MethodParameter(
            method,
            0,
            Slot.class,
            method.getParameterAnnotations()[0]
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());
    assertSame(((IntentRequest) envelope.getRequest()).getIntent().getSlots().get("GREETING"), resolver.resolve(input).get());
}
 
Example #16
Source File: SessionAttributesMapArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Object.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #17
Source File: SessionAttributesMapArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    Method method = MappingsController.class.getMethod("handleSession", Map.class);
    MethodParameter methodParameter = new MethodParameter(
            method,
            0,
            Map.class,
            method.getParameterAnnotations()[0]
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertSame(envelope.getSession().getAttributes(), resolver.resolve(input).get());
}
 
Example #18
Source File: UnhandledSkillExceptionHandler.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Response> handle(HandlerInput handlerInput, Throwable throwable) {
    return handlerInput.getResponseBuilder()
        .withSpeech("Sorry. I couldn't understand that. Please try again")
        .withReprompt("Please try again")
        .withShouldEndSession(false)
        .build();
}
 
Example #19
Source File: RequestArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            IntentRequest.class,
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertSame(envelope.getRequest(), resolver.resolve(input).get());
}
 
Example #20
Source File: LocaleArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
        this.getClass().getMethod("testSupportAndResolve"),
        0,
        Object.class, //<---- wrong class
        MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #21
Source File: CatchAllExceptionHandler.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Response> handle(HandlerInput handlerInput, Throwable throwable) {
    System.out.println("Exception: " + throwable.toString());
    System.out.println("Exception thrown while receiving: " + handlerInput.getRequestEnvelope().getRequest().getType());
    return handlerInput.getResponseBuilder()
        .withSpeech("Sorry. I have problems answering your request. Please try again")
        .withShouldEndSession(true)
        .build();
}
 
Example #22
Source File: AttributesManagerArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Object.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("intent");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #23
Source File: CatchAllExceptionHandler.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Response> handle(HandlerInput handlerInput, Throwable throwable) {
    System.out.println("Exception: " + throwable.toString());
    System.out.println("Exception thrown while receiving: " + handlerInput.getRequestEnvelope().getRequest().getType());
    return handlerInput.getResponseBuilder()
        .withSpeech("Sorry. I have problems answering your request. Please try again")
        .withShouldEndSession(true)
        .build();
}
 
Example #24
Source File: IntentModelArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Map.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("PetTypeIntent", "pet", "DRAGON");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    Optional<Object> result = resolver.resolve(input);
    assertFalse(result.isPresent());
}
 
Example #25
Source File: IntentModelArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            MappingsController.PetTypeIntent.class,
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("PetTypeIntent", "pet", "DRAGON");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    Object resolved = resolver.resolve(input).get();
    assertTrue(resolved instanceof MappingsController.PetTypeIntent);
}
 
Example #26
Source File: SlotModelArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesntSupport() throws NoSuchMethodException {
    MethodParameter methodParameter = new MethodParameter(
            this.getClass().getMethod("testSupportAndResolve"),
            0,
            Map.class, //<---- wrong class
            MethodParameter.EMPTY_ANNOTATIONS
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("PetTypeIntent2", "pet", "DRAGON");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertFalse(resolver.resolve(input).isPresent());
}
 
Example #27
Source File: SlotModelArgumentResolverTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportAndResolve() throws NoSuchMethodException {
    Method method = MappingsController.class.getMethod("handleModelSlot", MappingsController.PetType.class);
    MethodParameter methodParameter = new MethodParameter(
            method,
            0,
            MappingsController.PetType.class,
            method.getParameterAnnotations()[0]
    );

    RequestEnvelope envelope = Utils.buildSimpleEnvelope("PetTypeIntentTwo", "pet", "DRAGON");
    ArgumentResolverContext input = new ArgumentResolverContext(mockSkillContext, methodParameter, HandlerInput.builder().withRequestEnvelope(envelope).build());

    assertEquals(MappingsController.PetType.DRAGON, resolver.resolve(input).get());
}
 
Example #28
Source File: WhenSessionAttributeTest.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
private Predicate<HandlerInput> buildPredicate(String methodName) throws NoSuchMethodException {
    return underTest
        .apply(
            ControllerMethodContext.builder()
                .withSkillContext(mockSkillContext)
                .withController(this)
                .withMethod(getClass().getMethod(methodName))
                .build(),
            getClass().getMethod(methodName).getAnnotation(WhenSessionAttribute.class));
}
 
Example #29
Source File: TestAlexApplication.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@IntentHandler("HelloWorldIntent")
public Optional<Response> greet(HandlerInput input) {
    String speechText = "Hello World";
    return input.getResponseBuilder()
            .withSpeech(speechText)
            .withSimpleCard("HelloWorld", speechText)
            .build();
}
 
Example #30
Source File: ControllerRequestMapper.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
/**
 * Generic procedure for discovering request/exception handlers and request/response interceptors
 * from a controller's methods.
 *
 * Applies method discovery logic consistently for all mapping types:
 * <ul>
 *     <li>resolve a {@link Predicate} of {@link HandlerInput} from the method</li>
 *     <li>delegate responsibility to the underlying handler, but guard it with the predicate</li>
 *     <li>look for the {@link Priority} annotation on each method</li>
 *     <li>order each handler in the controller by its priority</li>
 *     <li>methods not annotated with {@link Priority} are considered to have priority 0</li>
 * </ul>
 *
 * @param controller scanned for methods with mappers
 * @param resolvers set of resolvers
 * @param guardBuilder supplies a {@link Guard.Builder} for this mapping type
 * @param <T> type of handler/interceptor/etc. being discovered, e.g. {@link RequestInterceptor}
 * @param <G> type of guard, e.g. {@link RequestInterceptorGuard}
 * @param <B> type of guard builder, e.g. {@link RequestInterceptorGuard#builder()}
 * @return stream of constructed delegates, ordered by priority
 */
protected <T, G extends Guard<T>, B extends Guard.Builder<B, T, G>> Stream<G> find(
    Object controller,
    Set<? extends Resolver<ControllerMethodContext, T>> resolvers,
    Supplier<B> guardBuilder) {

    return Arrays.stream(controller.getClass().getMethods())
        .map(method -> ControllerMethodContext.builder()
            .withSkillContext(skillContext)
            .withController(controller)
            .withMethod(method)
            .build())
        .flatMap(context -> {
            Predicate<HandlerInput> predicate = findPredicates(context).orElse(TRUE);

            return resolvers.stream()
                .flatMap(resolver -> resolver.resolve(context).map(Stream::of).orElse(Stream.empty()))
                .map(delegate -> guardBuilder.get()
                    .withDelegate(delegate)
                    .withPredicate(predicate)
                    .withPriority(Optional.ofNullable(context.getMethod().getAnnotation(Priority.class))
                        .map(Priority::value)
                        .orElse(0))) // default to the '0' bucket for methods not annotated with Priority
                .map(Guard.Builder::<G>build);
        })
        // sort in descending order, so "higher priority" is more intuitive
        .sorted((a, b) -> -1 * Integer.compare(a.getPriority(), b.getPriority()));
}