com.orange.ngsi.client.QueryContextRequestTest.java Source code

Java tutorial

Introduction

Here is the source code for com.orange.ngsi.client.QueryContextRequestTest.java

Source

/*
 * Copyright (C) 2015 Orange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.orange.ngsi.client;

import com.orange.ngsi.TestConfiguration;
import com.orange.ngsi.model.CodeEnum;
import com.orange.ngsi.model.QueryContextResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;

import javax.inject.Inject;
import java.util.function.Consumer;

import static com.orange.ngsi.Util.*;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.reset;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;

/**
 * Test for the NGSI QueryContext request
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class)
public class QueryContextRequestTest {

    private static String baseUrl = "http://localhost:8080";

    private MockRestServiceServer mockServer;

    @Autowired
    private MappingJackson2HttpMessageConverter jsonConverter;

    @Autowired
    private MappingJackson2XmlHttpMessageConverter xmlConverter;

    @Autowired
    NgsiClient ngsiClient;

    @Inject
    private AsyncRestTemplate asyncRestTemplate;

    private Consumer<QueryContextResponse> onSuccess = Mockito.mock(Consumer.class);

    private Consumer<Throwable> onFailure = Mockito.mock(Consumer.class);

    @Before
    public void tearUp() {
        this.mockServer = MockRestServiceServer.createServer(asyncRestTemplate);
    }

    @After
    public void tearDown() {
        reset(onSuccess);
        reset(onFailure);
    }

    @Test(expected = HttpServerErrorException.class)
    public void queryContextRequestWith500() throws Exception {

        mockServer.expect(requestTo(baseUrl + "/ngsi10/queryContext")).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));

        ngsiClient.queryContext(baseUrl, null, createQueryContextTemperature()).get();
    }

    @Test(expected = HttpClientErrorException.class)
    public void queryContextRequestWith404() throws Exception {

        mockServer.expect(requestTo(baseUrl + "/ngsi10/queryContext")).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.NOT_FOUND));

        ngsiClient.queryContext(baseUrl, null, createQueryContextTemperature()).get();
    }

    @Test
    public void queryContextRequestOK() throws Exception {

        ngsiClient.protocolRegistry.unregisterHost(baseUrl);

        String responseBody = json(jsonConverter, createQueryContextResponseTemperature());

        this.mockServer.expect(requestTo(baseUrl + "/ngsi10/queryContext")).andExpect(method(HttpMethod.POST))
                .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE))
                .andExpect(header("Accept", MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.entities[*]", hasSize(1)))
                .andExpect(jsonPath("$.entities[0].id").value("S*"))
                .andExpect(jsonPath("$.entities[0].type").value("TempSensor"))
                .andExpect(jsonPath("$.entities[0].isPattern").value("true"))
                .andExpect(jsonPath("$.attributes[*]", hasSize(1)))
                .andExpect(jsonPath("$.attributes[0]").value("temp"))
                .andExpect(jsonPath("*.restriction").doesNotExist())
                .andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

        QueryContextResponse response = ngsiClient.queryContext(baseUrl, null, createQueryContextTemperature())
                .get();
        this.mockServer.verify();

        Assert.assertEquals(1, response.getContextElementResponses().size());
        Assert.assertEquals(CodeEnum.CODE_200.getLabel(),
                response.getContextElementResponses().get(0).getStatusCode().getCode());
        Assert.assertEquals("S1",
                response.getContextElementResponses().get(0).getContextElement().getEntityId().getId());
        Assert.assertEquals("TempSensor",
                response.getContextElementResponses().get(0).getContextElement().getEntityId().getType());
        Assert.assertEquals(false,
                response.getContextElementResponses().get(0).getContextElement().getEntityId().getIsPattern());
        Assert.assertEquals(1,
                response.getContextElementResponses().get(0).getContextElement().getContextAttributeList().size());
        Assert.assertEquals("temp", response.getContextElementResponses().get(0).getContextElement()
                .getContextAttributeList().get(0).getName());
        Assert.assertEquals("float", response.getContextElementResponses().get(0).getContextElement()
                .getContextAttributeList().get(0).getType());
        Assert.assertEquals("15.5", response.getContextElementResponses().get(0).getContextElement()
                .getContextAttributeList().get(0).getValue());
    }

    @Test
    public void queryContextRequestOK_XML() throws Exception {

        ngsiClient.protocolRegistry.registerHost(baseUrl);

        String responseBody = xml(xmlConverter, createQueryContextResponseTemperature());

        this.mockServer.expect(requestTo(baseUrl + "/ngsi10/queryContext")).andExpect(method(HttpMethod.POST))
                .andExpect(header("Content-Type", MediaType.APPLICATION_XML_VALUE))
                .andExpect(header("Accept", MediaType.APPLICATION_XML_VALUE))
                .andExpect(xpath("queryContextRequest/entityIdList/entityId[*]").nodeCount(1))
                .andExpect(xpath("queryContextRequest/entityIdList/entityId/id").string("S*"))
                .andExpect(xpath("queryContextRequest/entityIdList/entityId/@type").string("TempSensor"))
                .andExpect(xpath("queryContextRequest/entityIdList/entityId/@isPattern").string("true"))
                .andExpect(xpath("queryContextRequest/attributeList/*").nodeCount(1))
                .andExpect(xpath("queryContextRequest/attributeList/attribute").string("temp"))
                .andExpect(xpath("queryContextRequest/restriction").doesNotExist())
                .andRespond(withSuccess(responseBody, MediaType.APPLICATION_XML));

        QueryContextResponse response = ngsiClient.queryContext(baseUrl, null, createQueryContextTemperature())
                .get();
        this.mockServer.verify();

        Assert.assertEquals(1, response.getContextElementResponses().size());
        Assert.assertEquals(CodeEnum.CODE_200.getLabel(),
                response.getContextElementResponses().get(0).getStatusCode().getCode());
        Assert.assertEquals("S1",
                response.getContextElementResponses().get(0).getContextElement().getEntityId().getId());
        Assert.assertEquals("TempSensor",
                response.getContextElementResponses().get(0).getContextElement().getEntityId().getType());
        Assert.assertEquals(false,
                response.getContextElementResponses().get(0).getContextElement().getEntityId().getIsPattern());
        Assert.assertEquals(1,
                response.getContextElementResponses().get(0).getContextElement().getContextAttributeList().size());
        Assert.assertEquals("temp", response.getContextElementResponses().get(0).getContextElement()
                .getContextAttributeList().get(0).getName());
        Assert.assertEquals("float", response.getContextElementResponses().get(0).getContextElement()
                .getContextAttributeList().get(0).getType());
        Assert.assertEquals("15.5", response.getContextElementResponses().get(0).getContextElement()
                .getContextAttributeList().get(0).getValue());
    }

}