gt:         QueryContextResponse response = new QueryContextResponse();
context: /*
   * Copyright (C) 2016 Orange
   *
   * This software is distributed under the terms and conditions of the 'GNU GENERAL PUBLIC LICENSE
   * Version 2' license which can be found in the file 'LICENSE.txt' in this package distribution or
   * at 'http://www.gnu.org/licenses/gpl-2.0-standalone.html'.
   */
  
  package com.orange.cepheus.broker.controller;
  
  import com.orange.cepheus.broker.Application;
  import com.orange.ngsi.model.*;
  import org.junit.After;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.ArgumentCaptor;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.SpringApplicationConfiguration;
  import org.springframework.http.MediaType;
  import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.web.context.WebApplicationContext;
  
  import javax.annotation.PostConstruct;
  import java.util.Collections;
  import java.util.List;
  
  import static com.orange.cepheus.broker.Util.createSubscribeContextTemperature;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  import static com.orange.cepheus.broker.Util.*;
  import static org.mockito.Mockito.*;
  import static org.junit.Assert.*;
  
  /**
   * Tests for the NgsiRestController
   */
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  @WebAppConfiguration
  public class NgsiRestControllerTest {
  
      private MockMvc mockMvc;
  
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      @Autowired
      private MappingJackson2HttpMessageConverter mapper;
  
      @Mock
      private NgsiController ngsiController;
  
      @Autowired
      @InjectMocks
      private NgsiRestController ngsiRestController;
  
      @PostConstruct
      public void tearUp() {
          MockitoAnnotations.initMocks(this);
          mockMvc = webAppContextSetup(webApplicationContext).build();
      }
  
      @After
      public void teadDown() {
          reset(ngsiController);
      }
  
      @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 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());
      }
  
      @Test
      public void testUpdateContextElement_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);
  
          UpdateContextElement updateContextElement = new UpdateContextElement();
          updateContextElement.setContextAttributes(attributes);
  
          mockMvc.perform(put("/v1/contextEntities/12345678").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
                  .content(json(mapper, updateContextElement))).andExpect(status().isOk()).andExpect(jsonPath("$.errorCode").doesNotExist())
                  .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.UPDATE, 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 testUpdateContextElement_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);
  
          UpdateContextElement updateContextElement = new UpdateContextElement();
          updateContextElement.setContextAttributes(attributes);
  
          mockMvc.perform(put("/v1/contextEntities/12345678").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
                  .content(json(mapper, updateContextElement))).andExpect(status().isOk())
                  .andExpect(jsonPath("$.errorCode.code").value("400")).andExpect(jsonPath("$.contextResponses").doesNotExist());
  
          verify(ngsiController).updateContext(any());
      }
  
      @Test
      public void testGetContextElement_OK() throws Exception {
          ArgumentCaptor<QueryContext> queryContextArg = ArgumentCaptor.forClass(QueryContext.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));
          QueryContextResponse response = new QueryContextResponse();
          response.setContextElementResponses(Collections.singletonList(contextElementResponse));
          when(ngsiController.queryContext(any())).thenReturn(response);
  
          mockMvc.perform(get("/v1/contextEntities/12345678").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
                  .andExpect(jsonPath("$.statusCode.code").value("200"))
                  .andExpect(jsonPath("$.contextElement.attributes[0].name").value("test"))
                  .andExpect(jsonPath("$.contextElement.attributes[0].type").value("string"))
                  .andExpect(jsonPath("$.contextElement.attributes[0].value").value("OK"));
  
          verify(ngsiController).queryContext(queryContextArg.capture());
  
          QueryContext queryContext = queryContextArg.getValue();
          assertNotNull(queryContext);
          assertNotNull(queryContext.getEntityIdList());
          assertEquals(1, queryContext.getEntityIdList().size());
          assertNull(queryContext.getAttributeList());
          assertEquals("12345678", queryContext.getEntityIdList().get(0).getId());
          assertEquals("", queryContext.getEntityIdList().get(0).getType());
          assertEquals(false, queryContext.getEntityIdList().get(0).getIsPattern());
      }
  
      @Test
      public void testGetContextElement_Error() throws Exception {
          ArgumentCaptor<UpdateContext> updateContextArg = ArgumentCaptor.forClass(UpdateContext.class);
  
  