23c06a6817bf0da3fa7e4f92f6c142def0f066d2
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaiclient.client.aai;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
26 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
27 import static org.hamcrest.CoreMatchers.equalTo;
28 import static org.junit.Assert.assertThat;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.spy;
31 import java.util.List;
32 import java.util.Optional;
33 import javax.ws.rs.core.GenericType;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.Status;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.junit.runner.RunWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Spy;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
45 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
46 import org.onap.aaiclient.client.aai.entities.uri.ServiceInstanceUri;
47 import org.onap.aaiclient.client.defaultproperties.DefaultAAIPropertiesImpl;
48 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
49 import com.github.tomakehurst.wiremock.junit.WireMockRule;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class AAIResourcesClientWithServiceInstanceUriTest {
53
54     @Rule
55     public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
56
57     @Rule
58     public ExpectedException thrown = ExpectedException.none();
59
60     @Spy
61     public AAIClient client;
62
63     @InjectMocks
64     public AAIResourcesClient aaiClient = new AAIResourcesClient();
65
66     private ServiceInstanceUri uri;
67
68     @Before
69     public void setUp() {
70         doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
71         wireMockRule.stubFor(get(urlMatching("/aai/v[0-9]+/nodes.*")).willReturn(
72                 aResponse().withStatus(404).withHeader("Content-Type", "application/json").withHeader("Mock", "true")));
73
74         uri = spy((ServiceInstanceUri) AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment("id")));
75         doReturn(aaiClient).when(uri).getResourcesClient();
76     }
77
78     @Test
79     public void getWithClass() {
80         AAIResourcesClient client = aaiClient;
81         Optional<String> result = client.get(String.class, uri);
82
83         assertThat(result.isPresent(), equalTo(false));
84     }
85
86     @Test
87     public void getFullResponse() {
88         AAIResourcesClient client = aaiClient;
89         Response result = client.getFullResponse(uri);
90         assertThat(result.getStatus(), equalTo(Status.NOT_FOUND.getStatusCode()));
91     }
92
93     @Test
94     public void getWithGenericType() {
95         AAIResourcesClient client = aaiClient;
96         Optional<List<String>> result = client.get(new GenericType<List<String>>() {}, uri);
97         assertThat(result.isPresent(), equalTo(false));
98     }
99
100     @Test
101     public void getAAIWrapper() {
102         AAIResourcesClient client = aaiClient;
103         AAIResultWrapper result = client.get(uri);
104         assertThat(result.isEmpty(), equalTo(true));
105     }
106
107     @Test
108     public void getWithException() {
109         AAIResourcesClient client = aaiClient;
110         this.thrown.expect(IllegalArgumentException.class);
111         AAIResultWrapper result = client.get(uri, IllegalArgumentException.class);
112     }
113
114     @Test
115     public void existsTest() {
116         AAIResourcesClient client = aaiClient;
117         doReturn(uri).when(uri).clone();
118         boolean result = client.exists(uri);
119         assertThat(result, equalTo(false));
120     }
121
122 }