Ejemplo n.º 1
0
  @Test
  public void testAppendContextElement_Error() throws Exception {
    ArgumentCaptor<UpdateContext> updateContextArg = ArgumentCaptor.forClass(UpdateContext.class);

    List attributes = Collections.singletonList(new ContextAttribute("test", "string", "OK"));

    UpdateContextResponse response = new UpdateContextResponse();
    response.setErrorCode(new StatusCode(CodeEnum.CODE_400));
    when(ngsiController.updateContext(any())).thenReturn(response);

    AppendContextElement appendContextElement = new AppendContextElement();
    appendContextElement.setAttributeList(attributes);

    mockMvc
        .perform(
            post("/v1/contextEntities/12345678")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(json(mapper, appendContextElement)))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.errorCode.code").value("400"))
        .andExpect(jsonPath("$.id").doesNotExist())
        .andExpect(jsonPath("$.type").doesNotExist())
        .andExpect(jsonPath("$.isPattern").doesNotExist())
        .andExpect(jsonPath("$.contextResponses").doesNotExist());

    verify(ngsiController).updateContext(any());
  }
Ejemplo n.º 2
0
  @Test
  public void testAppendContextElement_OK() throws Exception {
    ArgumentCaptor<UpdateContext> updateContextArg = ArgumentCaptor.forClass(UpdateContext.class);

    List attributes = Collections.singletonList(new ContextAttribute("test", "string", "OK"));

    ContextElement contextElement = new ContextElement();
    contextElement.setEntityId(new EntityId("12345678", "", false));
    contextElement.setContextAttributeList(attributes);
    ContextElementResponse contextElementResponse =
        new ContextElementResponse(contextElement, new StatusCode(CodeEnum.CODE_200));
    UpdateContextResponse response = new UpdateContextResponse();
    response.setContextElementResponses(Collections.singletonList(contextElementResponse));
    when(ngsiController.updateContext(any())).thenReturn(response);

    AppendContextElement appendContextElement = new AppendContextElement();
    appendContextElement.setAttributeList(attributes);

    mockMvc
        .perform(
            post("/v1/contextEntities/12345678")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(json(mapper, appendContextElement)))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.errorCode").doesNotExist())
        .andExpect(jsonPath("$.id").value("12345678"))
        .andExpect(jsonPath("$.type").value(""))
        .andExpect(jsonPath("$.isPattern").value("false"))
        .andExpect(jsonPath("$.contextResponses[0].attributes[0].name").value("test"))
        .andExpect(jsonPath("$.contextResponses[0].attributes[0].type").value("string"))
        .andExpect(jsonPath("$.contextResponses[0].attributes[0].value").value("OK"))
        .andExpect(jsonPath("$.contextResponses[0].statusCode.code").value("200"));

    verify(ngsiController).updateContext(updateContextArg.capture());

    UpdateContext updateContextRequest = updateContextArg.getValue();
    assertNotNull(updateContextRequest);
    assertEquals(UpdateAction.APPEND, updateContextRequest.getUpdateAction());
    assertEquals(
        "12345678", updateContextRequest.getContextElements().get(0).getEntityId().getId());
    assertEquals("", updateContextRequest.getContextElements().get(0).getEntityId().getType());
    assertEquals(
        false, updateContextRequest.getContextElements().get(0).getEntityId().getIsPattern());
  }