aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcae/inventory/clients/DatabusControllerClientTest.java
blob: ae2f2ff96fedec9d3ddc37ddfe46ccaf5b7e1d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*-
 * ============LICENSE_START=======================================================
 * dcae-inventory
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcae.inventory.clients;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.configuration.ConfigurationException;
import io.dropwizard.configuration.YamlConfigurationFactory;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.validation.Validators;
import org.junit.Before;
import org.junit.Test;
import org.onap.dcae.inventory.InventoryConfiguration;
import org.onap.dcae.inventory.exceptions.DatabusControllerClientException;

import javax.validation.Validator;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

/**
 * Created by mhwang on 3/22/18.
 */
public class DatabusControllerClientTest {

    final private ObjectMapper objectMapper = Jackson.newObjectMapper();
    final private Validator validator = Validators.newValidator();
    final private YamlConfigurationFactory<InventoryConfiguration.DatabusControllerConnectionConfiguration> factory
            = new YamlConfigurationFactory<>(InventoryConfiguration.DatabusControllerConnectionConfiguration.class, validator, objectMapper, "dw");

    private InventoryConfiguration.DatabusControllerConnectionConfiguration configuration = null;

    @Before
    public void setupClass() throws IOException, ConfigurationException {
        final File yaml = new File(Thread.currentThread().getContextClassLoader().getResource("config-databus.yaml").getPath());
        this.configuration = factory.build(yaml);
    }

    @Test
    public void testConstructResourceURI() {
        DatabusControllerClient client = new DatabusControllerClient(null, this.configuration);
        URI uri = client.constructResourceURI("/some-path");
        assertEquals(uri.toString(), "https://databus-controller-hostname:8443/some-path");
    }

    private DatabusControllerClient setupForIsExists(Response mockResponse) {
        Client mockClient = mock(Client.class);
        WebTarget mockWebTarget = mock(WebTarget.class);
        Invocation.Builder mockBuilder = mock(Invocation.Builder.class);

        try {
            URI uri = new URI("https://databus-controller-hostname:8443/some-component-id");
            when(mockClient.target(uri)).thenReturn(mockWebTarget);
            //when(mockClient.target(new URI(any()))).thenReturn(mockWebTarget);
        } catch(URISyntaxException e) {
            fail("URI syntax error");
        }

        when(mockWebTarget.request(MediaType.APPLICATION_JSON_TYPE)).thenReturn(mockBuilder);
        when(mockBuilder.header(any(), any())).thenReturn(mockBuilder);

        when(mockBuilder.get()).thenReturn(mockResponse);
        return new DatabusControllerClient(mockClient, this.configuration);

    }

    @Test
    public void testIsExists() {
        Response mockResponse = mock(Response.class);
        when(mockResponse.getStatus()).thenReturn(200);
        InputStream stream = new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8));
        when(mockResponse.getEntity()).thenReturn(stream);

        DatabusControllerClient client = setupForIsExists(mockResponse);

        try {
            assertEquals(client.isExists("some-component-id"), false);
        } catch(Exception e) {
            fail("Unexpected exception");
        }
    }

    private void testIsExistsErrors(int statusError) {
        Response mockResponse = mock(Response.class);
        when(mockResponse.getStatus()).thenReturn(statusError);

        DatabusControllerClient client = setupForIsExists(mockResponse);

        try {
            client.isExists("some-component-id");
            fail("This was supposed to be a fail case. Exception not thrown.");
        } catch(DatabusControllerClientException e) {
            // Expected exception
        } catch(Exception e) {
            fail("Unexpected exception");
        }
    }

    @Test
    public void testIsExists401() {
        testIsExistsErrors(401);
    }

    @Test
    public void testIsExists403() {
        testIsExistsErrors(403);
    }

    @Test
    public void testIsExists500() {
        testIsExistsErrors(500);
    }

}