aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java
blob: c1d6ab78a8f2ad1e071756400a436ebf0ffd812c (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
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2018 Nokia 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.vid.asdc.rest;

import io.joshworks.restclient.http.HttpResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.asdc.beans.Service;
import org.onap.vid.client.SyncRestClient;

import java.io.InputStream;
import java.nio.file.Path;
import java.util.UUID;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.matches;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class SdcRestClientTest {

    private static final String SAMPLE_SERVICE_NAME = "sampleService";
    private static final String SAMPLE_BASE_URL = "baseUrl";
    private static final String SAMPLE_AUTH = "sampleAuth";
    private static final String METADATA_URL_REGEX = ".*sdc/v1/catalog/services/%s/metadata";
    private static final String MODEL_URL_REGEX = ".*sdc/v1/catalog/services/%s/toscaModel";


    @Mock
    private SyncRestClient mockedSyncRestClient;

    @Mock
    private HttpResponse<Object> httpResponse;

    @Mock
    private HttpResponse<InputStream> httpStreamResponse;

    @Mock
    private InputStream inputStream;

    private UUID randomId;

    private Service sampleService;

    private SdcRestClient restClient;


    @Before
    public void setUp() {
        randomId = UUID.randomUUID();
        sampleService = createTestService();
        restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
    }


    @Test
    public void shouldReturnServiceForGivenUUID() throws AsdcCatalogException {
        String url = String.format(METADATA_URL_REGEX, randomId);
        when(mockedSyncRestClient.get(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), any())).thenReturn(httpResponse);
        when(httpResponse.getBody()).thenReturn(sampleService);

        Service service = restClient.getService(randomId);


        assertThat(service, is(sampleService));
    }

    @Test(expected = AsdcCatalogException.class)
    public void shouldRaiseAsdcExceptionWhenClientFails() throws AsdcCatalogException {
        String url = String.format(METADATA_URL_REGEX, randomId);
        when(mockedSyncRestClient.get(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), any())).thenThrow(new RuntimeException());

        restClient.getService(randomId);
    }


    @Test
    public void shouldProperlyDownloadAndCopyToscaArtifact() throws AsdcCatalogException {
        String url = String.format(MODEL_URL_REGEX, randomId);
        when(mockedSyncRestClient.getStream(matches(url), any(), any())).thenReturn(httpStreamResponse);
        when(httpStreamResponse.getBody()).thenReturn(inputStream);


        Path serviceToscaModel = restClient.getServiceToscaModel(randomId);


        assertThat(serviceToscaModel, notNullValue());
        assertThat(serviceToscaModel.toFile().isFile(), is(true));

        serviceToscaModel.toFile().deleteOnExit();
    }

    @Test(expected = AsdcCatalogException.class)
    public void shouldRaiseAsdcExceptionWhenDownloadFails() throws AsdcCatalogException {
        String url = String.format(MODEL_URL_REGEX, randomId);
        when(mockedSyncRestClient.getStream(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class))).thenThrow(new RuntimeException());


        restClient.getServiceToscaModel(randomId);
    }


    private Service createTestService() {
        Service service = new Service();
        service.setInvariantUUID(randomId.toString());
        service.setUuid(randomId.toString());
        service.setName(SAMPLE_SERVICE_NAME);
        return service;
    }

}