First part of Tests 89/77689/5
authorWojciech Sliwka <wojciech.sliwka@nokia.com>
Fri, 1 Feb 2019 06:23:42 +0000 (07:23 +0100)
committerWojciech Sliwka <wojciech.sliwka@nokia.com>
Thu, 28 Feb 2019 08:52:01 +0000 (09:52 +0100)
Issue-ID: VID-385

add first part of new tests for AAI, move some files to proper directory

Change-Id: I6600c48d26387faf9e6cd6dbe7107e10428f4b52
Signed-off-by: Wojciech Sliwka <wojciech.sliwka@nokia.com>
vid-app-common/src/main/java/org/onap/vid/aai/PombaClientImpl.java
vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java [moved from vid-app-common/src/test/java/org/onap/vid/services/AAITreeConverterTest.java with 95% similarity]
vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientServerTest.java
vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java
vid-app-common/src/test/java/org/onap/vid/aai/AaiResponseTranslatorTest.java [moved from vid-app-common/src/test/java/org/onap/vid/services/AaiResponseTranslatorTest.java with 98% similarity]
vid-app-common/src/test/java/org/onap/vid/aai/PombaClientImplTest.java
vid-app-common/src/test/java/org/onap/vid/aai/PombaRestInterfaceTest.java [new file with mode: 0644]
vid-app-common/src/test/java/org/onap/vid/aai/util/HttpsAuthClientTest.java
vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java

index e220401..1ee4da0 100644 (file)
@@ -3,13 +3,14 @@
  * VID
  * ================================================================================
  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nokia. 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.
@@ -3,13 +3,14 @@
  * VID
  * ================================================================================
  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nokia. 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.
  * ============LICENSE_END=========================================================
  */
 
-package org.opencomp.vid.services;
+package org.onap.vid.aai;
 
 import com.google.common.collect.ImmutableList;
 import org.mockito.InjectMocks;
 import org.mockito.MockitoAnnotations;
 import org.onap.vid.aai.util.AAITreeConverter;
 import org.onap.vid.model.Action;
-import org.onap.vid.model.aaiTree.*;
+import org.onap.vid.model.aaiTree.AAITreeNode;
+import org.onap.vid.model.aaiTree.Network;
+import org.onap.vid.model.aaiTree.ServiceInstance;
+import org.onap.vid.model.aaiTree.VfModule;
+import org.onap.vid.model.aaiTree.Vnf;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
@@ -33,7 +38,11 @@ import org.testng.annotations.Test;
 import java.util.List;
 
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.emptyOrNullString;
+import static org.hamcrest.Matchers.hasKey;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
 import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
 
 public class AAITreeConverterTest {
index fce9fa2..dd7b26c 100644 (file)
@@ -28,20 +28,20 @@ import org.assertj.core.api.Assertions;
 import org.glassfish.grizzly.http.util.HttpStatus;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.vid.aai.model.ResourceType;
 import org.onap.vid.client.SyncRestClient;
 import org.onap.vid.model.SubscriberList;
 import org.onap.vid.testUtils.StubServerUtil;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 
 import java.io.IOException;
 
-@RunWith(MockitoJUnitRunner.class)
+import static org.mockito.MockitoAnnotations.initMocks;
+
 public class AaiOverTLSClientServerTest {
 
     @Mock
@@ -100,6 +100,11 @@ public class AaiOverTLSClientServerTest {
         serverUtil.stopServer();
     }
 
+    @BeforeMethod
+    public void setUp(){
+        initMocks(this);
+    }
+
     @Test
     public void shouldSearchNodeTypeByName() throws IOException, ParseException {
         ObjectMapper objectMapper = getFasterXmlObjectMapper();
index 702a9e0..43fb5a3 100644 (file)
 package org.onap.vid.aai;
 
 import com.google.common.collect.ImmutableMap;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
 import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.junit.MockitoJUnitRunner;
+
 import org.onap.vid.aai.model.ResourceType;
 import org.onap.vid.client.SyncRestClient;
 import org.onap.vid.model.SubscriberList;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 
 import java.util.Collections;
 import java.util.Map;
 
 import static org.mockito.ArgumentMatchers.contains;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.MockitoAnnotations.initMocks;
 
-@RunWith(MockitoJUnitRunner.class)
 public class AaiOverTLSClientTest {
 
     private static final String SEARCH_NODES_QUERY_SEARCH_NODE_TYPE = "nodes/generic-vnfs?vnf-name=name";
@@ -50,8 +49,9 @@ public class AaiOverTLSClientTest {
     @Mock
     private AaiOverTLSPropertySupplier propertySupplier;
 
-    @Before
+    @BeforeMethod
     public void setUp() {
+        initMocks(this);
         aaiRestClient = new AaiOverTLSClient(syncRestClient,  propertySupplier);
     }
 
@@ -3,13 +3,14 @@
  * VID
  * ================================================================================
  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nokia. 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.
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.vid.services;
+package org.onap.vid.aai;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import org.onap.vid.aai.AaiResponseTranslator;
 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
index 483feec..222bf1e 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/PombaRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/PombaRestInterfaceTest.java
new file mode 100644 (file)
index 0000000..b409c1f
--- /dev/null
@@ -0,0 +1,127 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2019 Nokia
+ * ================================================================================
+ * 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.aai;
+
+
+import org.mockito.Mock;
+import org.onap.vid.aai.util.HttpClientMode;
+import org.onap.vid.aai.util.HttpsAuthClient;
+import org.onap.vid.aai.util.ServletRequestHelper;
+import org.onap.vid.aai.util.SystemPropertyHelper;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.Entity;
+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.IOException;
+import java.security.GeneralSecurityException;
+import java.util.UUID;
+import java.util.regex.Pattern;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.matches;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.FROM_APP_ID_HEADER;
+import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.TRANSACTION_ID_HEADER;
+import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
+
+public class PombaRestInterfaceTest {
+    private static final String UUID_REGEX = "[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}";
+    private static final String SAMPLE_APP_ID = "vid";
+    private static final String SAMPLE_URL = "sampleUrl";
+    private static final String SAMPLE_PAYLOAD = "samplePayload";
+    private static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX);
+
+    @Mock
+    private HttpsAuthClient authClient;
+
+    @Mock
+    private ServletRequestHelper requestHelper;
+
+    @Mock
+    private SystemPropertyHelper systemPropertyHelper;
+
+    @Mock
+    private Client client;
+
+    @Mock
+    private WebTarget webTarget;
+
+    @Mock
+    private Invocation.Builder builder;
+
+    @Mock
+    private Response response;
+
+    private PombaRestInterface pombaRestInterface;
+
+    @BeforeMethod
+    public void setUp() throws GeneralSecurityException, IOException {
+        initMocks(this);
+
+        when(requestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
+        when(authClient.getClient(HttpClientMode.WITH_KEYSTORE)).thenReturn(client);
+        setUpBuilder();
+        pombaRestInterface = new PombaRestInterface(authClient, requestHelper, systemPropertyHelper);
+    }
+
+
+    @Test
+    public void shouldProperlySendRequestWithAllRequiredHeaders() {
+        Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
+
+        assertThat(actualResponse).isEqualTo(response);
+
+        verify(builder).accept(MediaType.APPLICATION_JSON);
+        verify(builder).header(FROM_APP_ID_HEADER, SAMPLE_APP_ID);
+        verify(builder).header(matches(TRANSACTION_ID_HEADER), matches(UUID_PATTERN));
+        verify(builder).header(matches(REQUEST_ID_HEADER_KEY), matches(UUID_PATTERN));
+        verify(builder).post(any(Entity.class));
+    }
+
+    @Test
+    public void shouldReturnNullWhenExceptionWasRaised() {
+        doThrow(new RuntimeException()).when(client).target(anyString());
+
+        Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
+
+        assertThat(actualResponse).isNull();
+    }
+
+    private void setUpBuilder() {
+        when(client.target(anyString())).thenReturn(webTarget);
+        when(webTarget.request()).thenReturn(builder);
+        when(builder.accept(MediaType.APPLICATION_JSON)).thenReturn(builder);
+        when(builder.header(anyString(), any())).thenReturn(builder);
+        when(builder.post(any(Entity.class))).thenReturn(response);
+        when(response.getStatusInfo()).thenReturn(Response.Status.OK);
+    }
+}
\ No newline at end of file
index aff6c7f..3336a8a 100644 (file)
 
 package org.onap.vid.aai.util;
 
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+
 import org.onap.vid.aai.exceptions.HttpClientBuilderException;
 import org.onap.vid.exceptions.GenericUncheckedException;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 import org.togglz.core.manager.FeatureManager;
 
 import javax.net.ssl.SSLContext;
@@ -35,8 +35,8 @@ import java.util.Optional;
 
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.*;
+import static org.mockito.MockitoAnnotations.initMocks;
 
-@RunWith(MockitoJUnitRunner.class)
 public class HttpsAuthClientTest {
     @Mock
     private SystemPropertyHelper systemPropertyHelper;
@@ -51,14 +51,15 @@ public class HttpsAuthClientTest {
         return new HttpsAuthClient(CERT_FILE_PATH, systemPropertyHelper, sslContextProvider, mock(FeatureManager.class));
     }
 
-    @Before
+    @BeforeMethod
     public void setUp() throws Exception {
+        initMocks(this);
         when(systemPropertyHelper.getAAITruststoreFilename()).thenReturn(Optional.of("filename"));
         when(systemPropertyHelper.getDecryptedKeystorePassword()).thenReturn("password");
         when(systemPropertyHelper.getDecryptedTruststorePassword()).thenReturn("password");
     }
 
-    @Test(expected = HttpClientBuilderException.class)
+    @Test(expectedExceptions = {HttpClientBuilderException.class})
     public void testHttpClientBuilderExceptionOnGetClient() throws Exception {
         //when
         when(systemPropertyHelper.isClientCertEnabled()).thenReturn(true);
index 69e8ddc..3393aa7 100644 (file)
 
 package org.onap.vid.aai.util;
 
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.junit.MockitoJUnitRunner;
+
 import org.onap.vid.aai.ExceptionWithRequestInfo;
 import org.onap.vid.aai.exceptions.InvalidPropertyException;
 import org.onap.vid.exceptions.GenericUncheckedException;
 import org.onap.vid.utils.Unchecked;
 import org.springframework.http.HttpMethod;
 import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.client.Client;
@@ -51,8 +51,9 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
 
-@RunWith(MockitoJUnitRunner.class)
 public class SingleAAIRestInterfaceTest {
 
     private static final String PATH = "path";
@@ -78,8 +79,9 @@ public class SingleAAIRestInterfaceTest {
 
     private AAIRestInterface testSubject;
 
-    @Before
+    @BeforeMethod
     public void setUp() throws Exception {
+        initMocks(this);
         mockSystemProperties();
         testSubject = createTestSubject();
         when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
@@ -130,7 +132,7 @@ public class SingleAAIRestInterfaceTest {
         Assert.assertEquals(response, finalResponse);
     }
 
-    @Test(expected = ExceptionWithRequestInfo.class)
+    @Test(expectedExceptions = {ExceptionWithRequestInfo.class})
     public void testFailedRestJsonPut() {
         // given
         String methodName = "RestPut";