@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());
  }
  @Test
  public void missingParameterErrorUpdate() throws Exception {

    UpdateContext updateContext = Util.createUpdateContextTempSensor(0);
    updateContext.setUpdateAction(null);

    mockMvc
        .perform(
            post("/ni/updateContext")
                .content(Util.json(jsonConverter, updateContext))
                .contentType(MediaType.APPLICATION_JSON)
                .header("Host", "localhost")
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(
            MockMvcResultMatchers.jsonPath("$.errorCode.code").value(CodeEnum.CODE_471.getLabel()))
        .andExpect(
            MockMvcResultMatchers.jsonPath("$.errorCode.reasonPhrase")
                .value(CodeEnum.CODE_471.getShortPhrase()))
        .andExpect(
            MockMvcResultMatchers.jsonPath("$.errorCode.details")
                .value("The parameter updateAction of type string is missing in the request"));
  }
  @Override
  public UpdateContextResponse updateContext(final UpdateContext update) {

    // send response with status 200 = OK
    UpdateContextResponse updateContextResponse = new UpdateContextResponse();
    List<ContextElementResponse> contextElementResponseList = new ArrayList<>();
    StatusCode statusCode = new StatusCode(CodeEnum.CODE_200);
    for (ContextElement contextElement : update.getContextElements()) {
      logger.info("=> updateContext: {}", contextElement.toString());
      contextElementResponseList.add(new ContextElementResponse(contextElement, statusCode));
    }
    updateContextResponse.setContextElementResponses(contextElementResponseList);
    return updateContextResponse;
  }
  @Test
  public void testDeleteContextAttributeValue_OK() throws Exception {
    ArgumentCaptor<UpdateContext> updateContextArg = ArgumentCaptor.forClass(UpdateContext.class);

    ContextAttribute contextAttribute = new ContextAttribute("test", "string", "OK");
    contextAttribute.setMetadata(
        Collections.singletonList(new ContextMetadata("ID", "string", "DEADBEEF")));
    List attributes = Collections.singletonList(contextAttribute);

    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);

    mockMvc
        .perform(
            delete("/v1/contextEntities/12345678/attributes/test/DEADBEEF")
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.code").value("200"));

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

    UpdateContext updateContext = updateContextArg.getValue();
    assertNotNull(updateContext);
    assertNotNull(updateContext.getContextElements());
    assertEquals(1, updateContext.getContextElements().size());
    assertEquals(UpdateAction.DELETE, updateContext.getUpdateAction());
    assertEquals("12345678", updateContext.getContextElements().get(0).getEntityId().getId());
    assertEquals("", updateContext.getContextElements().get(0).getEntityId().getType());
    assertEquals(false, updateContext.getContextElements().get(0).getEntityId().getIsPattern());
    assertNotNull(updateContext.getContextElements().get(0).getContextAttributeList());
    assertEquals(1, updateContext.getContextElements().get(0).getContextAttributeList().size());
    assertEquals(
        "test",
        updateContext.getContextElements().get(0).getContextAttributeList().get(0).getName());
    assertEquals(
        "", updateContext.getContextElements().get(0).getContextAttributeList().get(0).getType());
    assertEquals(
        "", updateContext.getContextElements().get(0).getContextAttributeList().get(0).getValue());
    assertNotNull(
        updateContext.getContextElements().get(0).getContextAttributeList().get(0).getMetadata());
    assertEquals(
        1,
        updateContext
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getMetadata()
            .size());
    assertEquals(
        "ID",
        updateContext
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getMetadata()
            .get(0)
            .getName());
    assertEquals(
        "string",
        updateContext
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getMetadata()
            .get(0)
            .getType());
    assertEquals(
        "DEADBEEF",
        updateContext
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getMetadata()
            .get(0)
            .getValue());
  }
  @Test
  public void testUpdateContextAttribute_OK() throws Exception {
    ArgumentCaptor<UpdateContext> updateContextArg = ArgumentCaptor.forClass(UpdateContext.class);

    ContextAttribute attribute = new ContextAttribute("temp", "float", "15.5");

    ContextElement contextElement = new ContextElement();
    contextElement.setEntityId(new EntityId("12345678", "", false));
    contextElement.setContextAttributeList(Collections.singletonList(attribute));
    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);

    UpdateContextAttribute updateContextAttribute = new UpdateContextAttribute();
    updateContextAttribute.setAttribute(attribute);

    mockMvc
        .perform(
            put("/v1/contextEntities/12345678/attributes/temp")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(json(mapper, updateContextAttribute)))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.code").value(CodeEnum.CODE_200.getLabel()));

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

    UpdateContext updateContextRequest = updateContextArg.getValue();
    assertNotNull(updateContextRequest);
    assertEquals(UpdateAction.UPDATE, updateContextRequest.getUpdateAction());
    assertNotNull(updateContextRequest.getContextElements());
    assertEquals(1, updateContextRequest.getContextElements().size());
    assertNotNull(updateContextRequest.getContextElements().get(0).getEntityId());
    assertEquals(
        "12345678", updateContextRequest.getContextElements().get(0).getEntityId().getId());
    assertEquals("", updateContextRequest.getContextElements().get(0).getEntityId().getType());
    assertEquals(
        false, updateContextRequest.getContextElements().get(0).getEntityId().getIsPattern());
    assertNotNull(updateContextRequest.getContextElements().get(0).getContextAttributeList());
    assertEquals(
        1, updateContextRequest.getContextElements().get(0).getContextAttributeList().size());
    assertEquals(
        "temp",
        updateContextRequest
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getName());
    assertEquals(
        "float",
        updateContextRequest
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getType());
    assertEquals(
        "15.5",
        updateContextRequest
            .getContextElements()
            .get(0)
            .getContextAttributeList()
            .get(0)
            .getValue());
  }