コード例 #1
0
  @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());
  }
コード例 #2
0
  private QueryContextResponse createRoomQueryResponse(EntityId queryEntityId) {
    QueryContextResponse queryContextResponse = new QueryContextResponse();

    List<ContextElementResponse> contextElementResponseList = new ArrayList<>();
    if ((queryEntityId.getIsPattern()) && (queryEntityId.getId().equals("Room*"))) {
      for (int varTemp : tempTab) {
        for (int varRoom = 1; varRoom < 5; varRoom++) {
          for (int varFloor = 1; varFloor < 4; varFloor++) {
            ContextElement contextElement = new ContextElement();
            String name = "Room" + varFloor + varRoom;
            EntityId entityId = new EntityId(name, "Room", false);
            contextElement.setEntityId(entityId);
            int value = ((varTemp + (2 * varFloor) + varRoom));
            ContextAttribute tempContextAttribute =
                new ContextAttribute("temperature", "double", String.valueOf(value));
            ContextAttribute floorContextAttribute =
                new ContextAttribute("floor", "string", "Floor" + varFloor);
            List<ContextAttribute> contextAttributeList = new ArrayList<>();
            contextAttributeList.add(tempContextAttribute);
            contextAttributeList.add(floorContextAttribute);
            contextElement.setContextAttributeList(contextAttributeList);
            ContextElementResponse contextElementResponse = new ContextElementResponse();
            contextElementResponse.setContextElement(contextElement);
            contextElementResponse.setStatusCode(new StatusCode(CodeEnum.CODE_200));
            contextElementResponseList.add(contextElementResponse);
          }
        }
      }
    } else {
      String id = queryEntityId.getId();
      int varFloor = Integer.parseInt(String.valueOf(id.charAt(4)));
      int varRoom = Integer.parseInt(String.valueOf(id.charAt(5)));
      Random rand = new Random();
      int i = rand.nextInt(tempTab.length);
      int varTemp = tempTab[i];
      ContextElement contextElement = new ContextElement();

      contextElement.setEntityId(queryEntityId);
      int value = ((varTemp + (2 * varFloor) + varRoom));
      ContextAttribute tempContextAttribute =
          new ContextAttribute("temperature", "double", String.valueOf(value));
      ContextAttribute floorContextAttribute =
          new ContextAttribute("floor", "string", "Floor" + varFloor);
      List<ContextAttribute> contextAttributeList = new ArrayList<>();
      contextAttributeList.add(tempContextAttribute);
      contextAttributeList.add(floorContextAttribute);
      contextElement.setContextAttributeList(contextAttributeList);
      ContextElementResponse contextElementResponse = new ContextElementResponse();
      contextElementResponse.setContextElement(contextElement);
      contextElementResponse.setStatusCode(new StatusCode(CodeEnum.CODE_200));
      contextElementResponseList.add(contextElementResponse);
    }
    queryContextResponse.setContextElementResponses(contextElementResponseList);
    return queryContextResponse;
  }
コード例 #3
0
  @Test
  public void testGetContextEntityTypes_Error() throws Exception {
    QueryContextResponse response = new QueryContextResponse();
    response.setErrorCode(new StatusCode(CodeEnum.CODE_500));
    when(ngsiController.queryContext(any())).thenReturn(response);

    mockMvc
        .perform(get("/v1/contextEntityTypes/TempSensor").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andDo(mvcResult -> System.out.println(mvcResult.getResponse().getContentAsString()))
        .andExpect(jsonPath("$.errorCode.code").value("500"))
        .andExpect(jsonPath("$.contextResponses").doesNotExist());

    verify(ngsiController).queryContext(any());
  }
コード例 #4
0
  @Override
  public QueryContextResponse queryContext(final QueryContext query) {
    logger.info("=> queryContext: {}", query.toString());

    // check if the incoming query is on Room
    EntityId queryEntityId = query.getEntityIdList().get(0);
    if (queryEntityId.getId().contains("Room")) {
      return createRoomQueryResponse(queryEntityId);
    } else if (queryEntityId.getId().contains("Shutter")) {
      return createShutterQueryResponse(queryEntityId);
    } else {
      QueryContextResponse queryResponse = new QueryContextResponse();
      queryResponse.setErrorCode(new StatusCode(CodeEnum.CODE_404, queryEntityId.getId()));
      return queryResponse;
    }
  }
コード例 #5
0
  @Test
  public void testGetContextElement_Error() throws Exception {
    ArgumentCaptor<UpdateContext> updateContextArg = ArgumentCaptor.forClass(UpdateContext.class);

    QueryContextResponse response = new QueryContextResponse();
    response.setErrorCode(new StatusCode(CodeEnum.CODE_400));
    when(ngsiController.queryContext(any())).thenReturn(response);

    mockMvc
        .perform(get("/v1/contextEntities/12345678").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.statusCode.code").value("400"))
        .andExpect(jsonPath("$.contextElement").doesNotExist());

    verify(ngsiController).queryContext(any());
  }
コード例 #6
0
  @Test
  public void testGetContextEntityTypesAttribute_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", "TempSensor", 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/contextEntityTypes/TempSensor/attributes/temp")
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andDo(mvcResult -> System.out.println(mvcResult.getResponse().getContentAsString()))
        .andExpect(jsonPath("$.errorCode").doesNotExist())
        .andExpect(jsonPath("$.contextResponses").isArray())
        .andExpect(jsonPath("$.contextResponses[0].statusCode.code").value("200"))
        .andExpect(jsonPath("$.contextResponses[0].contextElement.id").value("12345678"))
        .andExpect(jsonPath("$.contextResponses[0].contextElement.type").value("TempSensor"))
        .andExpect(jsonPath("$.contextResponses[0].contextElement.isPattern").value("false"))
        .andExpect(
            jsonPath("$.contextResponses[0].contextElement.attributes[0].name").value("test"))
        .andExpect(
            jsonPath("$.contextResponses[0].contextElement.attributes[0].type").value("string"))
        .andExpect(
            jsonPath("$.contextResponses[0].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());
    assertNotNull(queryContext.getAttributeList());
    assertEquals(".*", queryContext.getEntityIdList().get(0).getId());
    assertEquals("TempSensor", queryContext.getEntityIdList().get(0).getType());
    assertEquals(true, queryContext.getEntityIdList().get(0).getIsPattern());
    assertEquals("temp", queryContext.getAttributeList().get(0));
  }
コード例 #7
0
  private QueryContextResponse createShutterQueryResponse(EntityId queryEntityId) {
    QueryContextResponse queryContextResponse = new QueryContextResponse();

    List<ContextElementResponse> contextElementResponseList = new ArrayList<>();
    Random rand = new Random();
    if ((queryEntityId.getIsPattern()) && (queryEntityId.getId().equals("Shutter*"))) {
      for (int varRoom = 1; varRoom < 5; varRoom++) {
        for (int varFloor = 1; varFloor < 4; varFloor++) {
          ContextElement contextElement = new ContextElement();
          String name = "Shutter" + varFloor + varRoom;
          EntityId entityId = new EntityId(name, "Room", false);
          contextElement.setEntityId(entityId);
          int i = rand.nextInt(statusTab.length);
          String varStatus = statusTab[i];
          ContextAttribute statusContextAttribute =
              new ContextAttribute("status", "string", varStatus);
          List<ContextAttribute> contextAttributeList = new ArrayList<>();
          contextAttributeList.add(statusContextAttribute);
          contextElement.setContextAttributeList(contextAttributeList);
          ContextElementResponse contextElementResponse = new ContextElementResponse();
          contextElementResponse.setContextElement(contextElement);
          contextElementResponse.setStatusCode(new StatusCode(CodeEnum.CODE_200));
          contextElementResponseList.add(contextElementResponse);
        }
      }

    } else {
      int i = rand.nextInt(statusTab.length);
      String varStatus = statusTab[i];
      ContextElement contextElement = new ContextElement();
      contextElement.setEntityId(queryEntityId);
      ContextAttribute statusContextAttribute = new ContextAttribute("status", "string", varStatus);
      List<ContextAttribute> contextAttributeList = new ArrayList<>();
      contextAttributeList.add(statusContextAttribute);
      contextElement.setContextAttributeList(contextAttributeList);
      ContextElementResponse contextElementResponse = new ContextElementResponse();
      contextElementResponse.setContextElement(contextElement);
      contextElementResponse.setStatusCode(new StatusCode(CodeEnum.CODE_200));
      contextElementResponseList.add(contextElementResponse);
    }
    queryContextResponse.setContextElementResponses(contextElementResponseList);
    return queryContextResponse;
  }
コード例 #8
0
  @Test
  public void testGetContextAttributeValue_NotImplemented() 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/attributes/test/DEADBEEF")
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.code").value("500"));

    verify(ngsiController, never()).queryContext(queryContextArg.capture());
  }