/* Test the contract for multipart POST commands.
   * A POST command should should receive an InteractionContext that has the new resource set; enabling the
   * command to process the resource contained in the current part of the multipart request
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testMultipartPostCommandReceivesResource() throws InteractionException {
    ResourceState initialState = new ResourceState("entity", "state", mockActions(), "/test");
    initialState.addTransition(
        new Transition.Builder().method("POST").target(initialState).build());
    // create a mock command to test the context is initialised correctly
    InteractionCommand mockCommand = mock(InteractionCommand.class);
    when(mockCommand.execute(any(InteractionContext.class))).thenReturn(Result.SUCCESS);
    // RIM with command controller that issues commands that always return SUCCESS
    HTTPHypermediaRIM rim =
        new HTTPHypermediaRIM(
            mockCommandController(mockCommand),
            new ResourceStateMachine(initialState),
            createMockMetadata());

    UriInfo uriInfo = mock(UriInfo.class);
    when(uriInfo.getPathParameters(anyBoolean())).thenReturn(mock(MultivaluedMap.class));
    when(uriInfo.getQueryParameters(anyBoolean())).thenReturn(mock(MultivaluedMap.class));

    InMultiPart inMP = mock(InMultiPart.class);
    when(inMP.hasNext()).thenReturn(true, false);
    when(inMP.next()).thenReturn(mock(InPart.class));

    rim.post(mock(HttpHeaders.class), uriInfo, inMP);
    verify(mockCommand)
        .execute((InteractionContext) argThat(new CommandReceivesResourceArgumentMatcher()));
  }
 private NewCommandController mockCommandController() {
   NewCommandController cc = mock(NewCommandController.class);
   try {
     InteractionCommand mockCommand = mock(InteractionCommand.class);
     when(mockCommand.execute(any(InteractionContext.class))).thenReturn(Result.FAILURE);
     when(cc.fetchCommand("DO")).thenReturn(mockCommand);
     InteractionCommand mockCommand1 = mock(InteractionCommand.class);
     when(mockCommand1.execute(any(InteractionContext.class))).thenReturn(Result.FAILURE);
     when(cc.fetchCommand("GET")).thenReturn(mockCommand1);
   } catch (InteractionException ie) {
     Assert.fail(ie.getMessage());
   }
   return cc;
 }
  /* We decode the query parameters to workaround an issue in Wink */
  @SuppressWarnings({"unchecked"})
  @Test
  public void testDecodeQueryParameters() throws InteractionException {
    ResourceState initialState = new ResourceState("entity", "state", mockActions(), "/test");
    // this test simply mocks a command to test the context query parameters is initialised properly
    InteractionCommand mockCommand = mock(InteractionCommand.class);
    when(mockCommand.execute(any(InteractionContext.class))).thenReturn(Result.FAILURE);
    // RIM with command controller that issues commands that always return SUCCESS
    HTTPHypermediaRIM rim =
        new HTTPHypermediaRIM(
            mockCommandController(mockCommand),
            new ResourceStateMachine(initialState),
            createMockMetadata());

    UriInfo uriInfo = mock(UriInfo.class);
    when(uriInfo.getPathParameters(true)).thenReturn(mock(MultivaluedMap.class));
    MultivaluedMap<String, String> queryMap = new MultivaluedMapImpl<String, String>();
    queryMap.add("$filter", "this+that");
    when(uriInfo.getQueryParameters(anyBoolean())).thenReturn(queryMap);

    rim.get(mock(HttpHeaders.class), "id", uriInfo);
    verify(mockCommand)
        .execute((InteractionContext) argThat(new InteractionContextArgumentMatcher()));
  }