Skip to content

Commit

Permalink
#29 #52 sending mockito tests and modified method version
Browse files Browse the repository at this point in the history
  • Loading branch information
Plugsocket committed Dec 26, 2018
1 parent 1c4c10a commit 2fb31b6
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 88 deletions.
37 changes: 1 addition & 36 deletions models/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"slots": [
{
"name": "Number",
"type": "NUMBER_OF_PEOPLE"
"type": "AMAZON.NUMBER"
}
],
"samples": [
Expand Down Expand Up @@ -238,41 +238,6 @@
}
]
},
{
"name": "NUMBER_OF_PEOPLE",
"values": [
{
"name": {
"value": "eine"
}
},
{
"name": {
"value": "zwei"
}
},
{
"name": {
"value": "drei"
}
},
{
"name": {
"value": "vier"
}
},
{
"name": {
"value": "fuenf"
}
},
{
"name": {
"value": "sechs"
}
}
]
},
{
"name": "FavoritRecipes",
"values": [
Expand Down
80 changes: 28 additions & 52 deletions src/main/java/verkocht/handlers/SetNumberOfPeopleIntentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static com.amazon.ask.request.Predicates.intentName;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -43,59 +42,36 @@ public boolean canHandle(HandlerInput input) {
@Override
public Optional<Response> handle(HandlerInput input) {
String speechText = "";
Request request = input.getRequestEnvelope().getRequest();
IntentRequest intentRequest = (IntentRequest) request;
Intent intent = intentRequest.getIntent();
Map<String, Slot> slots = intent.getSlots();

Slot chosenNumberSlot = slots.get(NUMBER_SLOT);

String chosenNumber = chosenNumberSlot.getValue();
input.getAttributesManager().setSessionAttributes(Collections.singletonMap(NUMBER_KEY, chosenNumber));

String actualNumber = (String) input.getAttributesManager().getSessionAttributes().get(NUMBER_KEY);

Recipe recipe = Recipe.getSavedRecipe();
if (actualNumber == null || actualNumber.isEmpty()) {
speechText = PhrasesForAlexa.PEOPLE_UNKNOWN;

if (recipe == null) {
speechText = PhrasesForAlexa.MODIFY_UNIT_SELECT_RECIPE_FIRST;
} else {
switch (actualNumber.toString()) {
case "eine":
case "mich":
recipe.setNumberOfPeople(1);
speechText = PhrasesForAlexa.PEOPLE_ONE;
break;
case "2":
case "zwei":
recipe.setNumberOfPeople(2);
speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 2);
break;
case "3":
case "drei":
recipe.setNumberOfPeople(3);
speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 3);
break;
case "4":
case "vier":
recipe.setNumberOfPeople(4);
speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 4);
break;
case "5":
case "fünf":
case "fuenf":
recipe.setNumberOfPeople(5);
speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 5);
break;
case "6":
case "sechs":
recipe.setNumberOfPeople(6);
speechText = String.format(PhrasesForAlexa.PEOPLE_SET, 6);
break;
default:
speechText = PhrasesForAlexa.PEOPLE_NUMBER_UNCLEAR;
break;
}
}
try {
Request request = input.getRequestEnvelope().getRequest();
IntentRequest intentRequest = (IntentRequest) request;
Intent intent = intentRequest.getIntent();
Map<String, Slot> slots = intent.getSlots();

Slot chosenNumberSlot = slots.get(NUMBER_SLOT);
String chosenNumber = chosenNumberSlot.getValue();
int getNumberOfPeople = Integer.parseInt(chosenNumber);

if (getNumberOfPeople <= 0 || getNumberOfPeople > 12) {
speechText = PhrasesForAlexa.PEOPLE_NUMBER_UNCLEAR;
} else {
if (getNumberOfPeople == 1) {
recipe.setNumberOfPeople(1);
speechText = PhrasesForAlexa.PEOPLE_ONE;
} else {
recipe.setNumberOfPeople(getNumberOfPeople);
speechText = String.format(PhrasesForAlexa.PEOPLE_SET, getNumberOfPeople);
}
}
} catch (Exception e) {
speechText = PhrasesForAlexa.PEOPLE_UNKNOWN;
}
}

return input.getResponseBuilder()
.withSpeech(speechText)
Expand Down
186 changes: 186 additions & 0 deletions src/test/java/handlers/SetNumberOfPeopleIntentHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package handlers;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.model.Intent;
import com.amazon.ask.model.IntentRequest;
import com.amazon.ask.model.RequestEnvelope;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.Slot;
import com.amazon.ask.response.ResponseBuilder;

import verkocht.handlers.SetNumberOfPeopleIntentHandler;
import verkocht.model.Category;
import verkocht.model.PhrasesForAlexa;
import verkocht.model.Recipe;

public class SetNumberOfPeopleIntentHandlerTest {
private SetNumberOfPeopleIntentHandler handler;
private Recipe testRecipe = new Recipe("test", Category.MEAT, 2, 20);

@Before
public void setup() {
handler = new SetNumberOfPeopleIntentHandler();
}

@Test
public void testCanHandle() {
final HandlerInput inputMock = Mockito.mock(HandlerInput.class);
when(inputMock.matches(any())).thenReturn(true);
assertTrue(handler.canHandle(inputMock));
}

@Test
public void testHandleWithoutSavedRecipe() {
Recipe.saveRecipe(null);
final HandlerInput inputMock = Mockito.mock(HandlerInput.class);
when(inputMock.getResponseBuilder()).thenReturn(new ResponseBuilder());

final Optional<Response> returnResponse = handler.handle(inputMock);

assertTrue(returnResponse.isPresent());
final Response response = returnResponse.get();

assertFalse(response.getShouldEndSession());
assertNotNull(response.getOutputSpeech());
assertTrue(response.getOutputSpeech().toString()
.contains(PhrasesForAlexa.MODIFY_UNIT_SELECT_RECIPE_FIRST));
}

@Test
public void testHandleModificationNotPassed() {
Recipe.saveRecipe(testRecipe);
HandlerInput inputMock = Mockito.mock(HandlerInput.class);
when(inputMock.getResponseBuilder()).thenReturn(new ResponseBuilder());

final Optional<Response> returnResponse = handler.handle(inputMock);

assertTrue(returnResponse.isPresent());
final Response response = returnResponse.get();

assertFalse(response.getShouldEndSession());
assertNotNull(response.getOutputSpeech());
assertTrue(response.getOutputSpeech().toString()
.contains(PhrasesForAlexa.PEOPLE_UNKNOWN));
}

@Test
public void testHandleModificationPassed() {
Recipe.saveRecipe(testRecipe);

final List<Slot> slotValues = new ArrayList<>();
Slot numberOfPeople = Slot.builder().withName("Number")
.withValue("3")
.build();
slotValues.add(numberOfPeople);
Intent.Builder intentBuilder = Intent.builder();
slotValues.forEach((slotItem) ->
intentBuilder.putSlotsItem(slotItem.getName(), slotItem)
);

final RequestEnvelope requestEnvelopeMock = RequestEnvelope.builder()
.withRequest(IntentRequest.builder()
.withIntent(intentBuilder.build())
.build())
.build();

HandlerInput input = Mockito.mock(HandlerInput.class);
when(input.getResponseBuilder()).thenReturn(new ResponseBuilder());
when(input.getRequestEnvelope()).thenReturn(requestEnvelopeMock);

final Optional<Response> returnResponse = handler.handle(input);

assertTrue(returnResponse.isPresent());
final Response response = returnResponse.get();

assertFalse(response.getShouldEndSession());
assertNotNull(response.getOutputSpeech());
assertTrue(response.getOutputSpeech().toString()
.contains(String.format(PhrasesForAlexa.PEOPLE_SET,"3")));
}

@Test
public void testHandleModificationOutOfParameter() {
Recipe.saveRecipe(testRecipe);

final List<Slot> slotValues = new ArrayList<>();
Slot numberOfPeople = Slot.builder().withName("Number")
.withValue("13")
.build();
slotValues.add(numberOfPeople);
Intent.Builder intentBuilder = Intent.builder();
slotValues.forEach((slotItem) ->
intentBuilder.putSlotsItem(slotItem.getName(), slotItem)
);

final RequestEnvelope requestEnvelopeMock = RequestEnvelope.builder()
.withRequest(IntentRequest.builder()
.withIntent(intentBuilder.build())
.build())
.build();

HandlerInput input = Mockito.mock(HandlerInput.class);
when(input.getResponseBuilder()).thenReturn(new ResponseBuilder());
when(input.getRequestEnvelope()).thenReturn(requestEnvelopeMock);

final Optional<Response> returnResponse = handler.handle(input);

assertTrue(returnResponse.isPresent());
final Response response = returnResponse.get();

assertFalse(response.getShouldEndSession());
assertNotNull(response.getOutputSpeech());
assertTrue(response.getOutputSpeech().toString()
.contains(PhrasesForAlexa.PEOPLE_NUMBER_UNCLEAR));
}

@Test
public void testHandleModificationParameterOne() {
Recipe.saveRecipe(testRecipe);

final List<Slot> slotValues = new ArrayList<>();
Slot numberOfPeople = Slot.builder().withName("Number")
.withValue("1")
.build();
slotValues.add(numberOfPeople);
Intent.Builder intentBuilder = Intent.builder();
slotValues.forEach((slotItem) ->
intentBuilder.putSlotsItem(slotItem.getName(), slotItem)
);

final RequestEnvelope requestEnvelopeMock = RequestEnvelope.builder()
.withRequest(IntentRequest.builder()
.withIntent(intentBuilder.build())
.build())
.build();

HandlerInput input = Mockito.mock(HandlerInput.class);
when(input.getResponseBuilder()).thenReturn(new ResponseBuilder());
when(input.getRequestEnvelope()).thenReturn(requestEnvelopeMock);

final Optional<Response> returnResponse = handler.handle(input);

assertTrue(returnResponse.isPresent());
final Response response = returnResponse.get();

assertFalse(response.getShouldEndSession());
assertNotNull(response.getOutputSpeech());
assertTrue(response.getOutputSpeech().toString()
.contains(PhrasesForAlexa.PEOPLE_ONE));
}


}

0 comments on commit 2fb31b6

Please sign in to comment.