5493d6778e7331d3bf4aa592088c837261221e72
[so.git] / common / src / test / java / org / onap / so / client / aai / AAIResourcesClientWithServiceInstanceUriTest.java
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.so.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
32 import java.util.List;
33 import java.util.Optional;
34
35 import javax.ws.rs.core.GenericType;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
38
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.junit.runner.RunWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Spy;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.so.client.aai.entities.AAIResultWrapper;
48 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
49 import org.onap.so.client.aai.entities.uri.ServiceInstanceUri;
50 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
51
52 import com.github.tomakehurst.wiremock.junit.WireMockRule;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class AAIResourcesClientWithServiceInstanceUriTest {
56
57         @Rule
58         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
59         
60         @Rule
61         public ExpectedException thrown = ExpectedException.none();
62         
63         @Spy
64         public AAIClient client;
65         
66         @InjectMocks
67         public AAIResourcesClient aaiClient = new AAIResourcesClient();
68         
69         private ServiceInstanceUri uri;
70         @Before
71         public void setUp() {
72                 
73                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
74                 wireMockRule.stubFor(get(urlMatching("/aai/v[0-9]+/nodes.*")) 
75                                 .willReturn(aResponse() 
76                                         .withStatus(404) 
77                                         .withHeader("Content-Type", "application/json")
78                                         .withHeader("Mock", "true")));
79                 
80                 uri = spy((ServiceInstanceUri)AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "id"));
81                 doReturn(aaiClient).when(uri).getResourcesClient();
82         }
83         
84         @Test
85         public void getWithClass() {
86                 AAIResourcesClient client = aaiClient;
87                 Optional<String> result = client.get(String.class, uri);
88                 
89                 assertThat(result.isPresent(), equalTo(false));
90         }
91         
92         @Test
93         public void getFullResponse() {
94                 AAIResourcesClient client = aaiClient;
95                 Response result = client.getFullResponse(uri);
96                 assertThat(result.getStatus(), equalTo(Status.NOT_FOUND.getStatusCode()));
97         }
98         
99         @Test
100         public void getWithGenericType() {
101                 AAIResourcesClient client = aaiClient;
102                 Optional<List<String>> result = client.get(new GenericType<List<String>>() {}, uri);
103                 assertThat(result.isPresent(), equalTo(false));
104         }
105         
106         @Test
107         public void getAAIWrapper() {
108                 AAIResourcesClient client = aaiClient;
109                 AAIResultWrapper result = client.get(uri);
110                 assertThat(result.isEmpty(), equalTo(true));
111         }
112         
113         @Test
114         public void getWithException() {
115                 AAIResourcesClient client = aaiClient;
116                 this.thrown.expect(IllegalArgumentException.class);
117                 AAIResultWrapper result = client.get(uri, IllegalArgumentException.class);
118         }
119         
120         @Test
121         public void existsTest() {
122                 AAIResourcesClient client = aaiClient;
123                 doReturn(uri).when(uri).clone();
124                 boolean result = client.exists(uri);
125                 assertThat(result, equalTo(false));
126         }
127
128 }