diff options
9 files changed, 325 insertions, 31 deletions
diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml index 179094ba9..2c34c5eef 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/pom.xml @@ -188,6 +188,12 @@ <version>${apache.httpcomponents.version}</version> </dependency> <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>${apache.httpcomponents.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.onap.ccsdk.sli.core</groupId> <artifactId>sli-common</artifactId> <scope>compile</scope> diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java index 908c42cb1..ba88be5a2 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/Constants.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,47 +34,47 @@ public class Constants { /** * Manifest attribute for OSGI Bundle Name */ - public static final String MANIFEST_ATTR_BUNDLE_NAME= "Bundle-Name"; + public static final String MANIFEST_ATTR_BUNDLE_NAME = "Bundle-Name"; /** * Manifest attribute for OSGI Bundle Symbolic Name */ - public static final String MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME= "Bundle-SymbolicName"; + public static final String MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME = "Bundle-SymbolicName"; /** * Manifest attribute for OSGI Bundle Description */ - public static final String MANIFEST_ATTR_BUNDLE_DESCRIPTION= "Bundle-Description"; + public static final String MANIFEST_ATTR_BUNDLE_DESCRIPTION = "Bundle-Description"; /** * Manifest attribute for OSGI Bundle Manifest version */ - public static final String MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION= "Bundle-ManifestVersion"; + public static final String MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION = "Bundle-ManifestVersion"; /** * Manifest attribute for OSGI Bundle Version */ - public static final String MANIFEST_ATTR_BUNDLE_VERSION= "Bundle-Version"; + public static final String MANIFEST_ATTR_BUNDLE_VERSION = "Bundle-Version"; /** * Manifest attribute for OSGI Bundle Blueprint */ - public static final String MANIFEST_ATTR_BUNDLE_BLUEPRINT= "Bundle-Blueprint"; + public static final String MANIFEST_ATTR_BUNDLE_BLUEPRINT = "Bundle-Blueprint"; /** * Manifest value for Mainfest Version */ - public static final String MANIFEST_VALUE_VERSION= "1.0"; + public static final String MANIFEST_VALUE_VERSION = "1.0"; /** * Manifest value for OSGI Bundle Vesion */ - public static final String MANIFEST_VALUE_BUNDLE_MAN_VERSION= "2"; + public static final String MANIFEST_VALUE_BUNDLE_MAN_VERSION = "2"; /** * Manifest value for OSGI Bundle Blueprint location */ - public static final String MANIFEST_VALUE_BUNDLE_BLUEPRINT= "OSGI-INF/blueprint/blueprint.xml"; + public static final String MANIFEST_VALUE_BUNDLE_BLUEPRINT = "OSGI-INF/blueprint/blueprint.xml"; /** * Base URL for config actions exposed by RESTCONF API @@ -110,12 +112,12 @@ public class Constants { /** * HTTP Header attribute for Content type - JSON */ - public static final String OPERATION_APPLICATION_JSON= " application/json"; + public static final String OPERATION_APPLICATION_JSON = " application/json"; /** * HTTP protocol used for config operations */ - public static final String OPERATION_HTTPS= "https"; + public static final String OPERATION_HTTPS = "https"; /** * Constant for backslash to be used while formatting URL diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java index 2ef1da36e..8f7bfabac 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/impl/MDSALStoreImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,7 +85,7 @@ public class MDSALStoreImpl implements MDSALStore { } if (configUrl != null) { try { - client = new RestClientInvoker(new URL(configUrl)); + client = getRestClientInvoker(new URL(configUrl)); client.setAuthentication(user, password); } catch (MalformedURLException e) { logger.error("Error initializing RestConf client: " + e.getMessage(), e); @@ -187,7 +189,7 @@ public class MDSALStoreImpl implements MDSALStore { URL configUrl = new URL(properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT)); String user = properties.getProperty(Constants.CONFIG_USER_PROPERTY); String password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY); - RestClientInvoker remoteClient = new RestClientInvoker(new URL(configUrl.getProtocol(), leader, configUrl.getPort(), "")); + RestClientInvoker remoteClient = getRestClientInvoker(new URL(configUrl.getProtocol(), leader, configUrl.getPort(), "")); remoteClient.setAuthentication(user, password); remoteClientMap.put(leader, remoteClient); return remoteClient; @@ -350,4 +352,8 @@ public class MDSALStoreImpl implements MDSALStore { throw new MDSALStoreException(e); } } + + protected RestClientInvoker getRestClientInvoker(URL configUrl) throws MalformedURLException { + return new RestClientInvoker(configUrl); + } } diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java index 2f11b887b..13999299b 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/objects/BundleInfo.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,7 +38,7 @@ package org.onap.appc.mdsal.objects; * Creates an object of BundleInfo with version initialized to 1 */ public BundleInfo(){ - version =1; + version = 1; } public String getName() { diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java index 5ebe58b88..e565f2052 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/operation/ConfigOperationRequestFormatter.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +41,7 @@ public class ConfigOperationRequestFormatter { */ public String buildPath(String module, String containerName , String... subModules ) { - StringBuilder path = new StringBuilder( Constants.CONFIG_PATH + Constants.URL_BACKSLASH + module + ":"+containerName + Constants.URL_BACKSLASH); + StringBuilder path = new StringBuilder( Constants.CONFIG_PATH + Constants.URL_BACKSLASH + module + ":" + containerName + Constants.URL_BACKSLASH); if(subModules.length >0){ for(String subModule : subModules){ path.append(subModule); diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java index 18ff52242..545bd32a9 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/main/java/org/onap/appc/mdsal/provider/MdsalStoreProvider.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,9 +25,15 @@ package org.onap.appc.mdsal.provider; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.common.util.concurrent.Futures; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.onap.appc.Constants; +import org.onap.appc.mdsal.MDSALStore; +import org.onap.appc.mdsal.impl.MDSALStoreFactory; +import org.onap.appc.mdsal.objects.BundleInfo; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; import org.opendaylight.controller.sal.binding.api.NotificationProviderService; @@ -38,16 +46,9 @@ import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.StatusBuilder; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.onap.appc.Constants; -import org.onap.appc.mdsal.MDSALStore; -import org.onap.appc.mdsal.impl.MDSALStoreFactory; -import org.onap.appc.mdsal.objects.BundleInfo; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.util.concurrent.Futures; public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{ @@ -90,7 +91,8 @@ public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{ @Override public Future<RpcResult<StoreYangOutput>> storeYang(StoreYangInput input) { - Status status =null;String message=null; + Status status = null; + String message = null; try{ BundleInfo bundleInfo = new BundleInfo(); bundleInfo.setName(input.getModuleName()); diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java index 9469df60f..2229eff9d 100644 --- a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/MDSALStoreTest.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +26,8 @@ package org.onap.appc.mdsal; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.http.HttpResponse; import org.junit.Before; import org.junit.Ignore; -import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.appc.mdsal.exception.MDSALStoreException; diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/impl/MDSALStoreImplTest.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/impl/MDSALStoreImplTest.java new file mode 100644 index 000000000..fa1146237 --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/impl/MDSALStoreImplTest.java @@ -0,0 +1,194 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2019 Ericsson + * ============================================================================= + * 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.appc.mdsal.impl; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.nio.charset.Charset; +import java.util.Date; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.mdsal.MDSALStore; +import org.onap.appc.mdsal.exception.MDSALStoreException; +import org.onap.appc.mdsal.objects.BundleInfo; +import org.onap.appc.rest.client.RestClientInvoker; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; +import com.att.eelf.configuration.EELFLogger; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import java.net.URL; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(FrameworkUtil.class) +public class MDSALStoreImplTest { + + private final BundleContext bundleContext= Mockito.mock(BundleContext.class); + private final Bundle bundleService=Mockito.mock(Bundle.class); + private MDSALStoreImpl mdsalStore; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Before + public void setup() { + mdsalStore = (MDSALStoreImpl) MDSALStoreFactory.createMDSALStore(); + PowerMockito.mockStatic(FrameworkUtil.class); + PowerMockito.when(FrameworkUtil.getBundle(MDSALStoreImpl.class)).thenReturn(bundleService); + PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext); + PowerMockito.when(bundleContext.getBundle("TEST_MODULE_NAME")).thenReturn(bundleService); + } + + @Test + public void testMDSALStoreImpl() { + Assert.assertTrue(mdsalStore.isModulePresent("TEST_MODULE_NAME", new Date())); + } + + @Test + public void testStoreYangModule() throws MDSALStoreException { + expectedEx.expect(MDSALStoreException.class); + expectedEx.expectMessage("Error storing yang module:"); + mdsalStore.storeYangModule("", new BundleInfo()); + } + + @Test + public void testStoreYangModuleOnLeader() throws MDSALStoreException, APPCException, IllegalStateException, IOException { + RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class); + Whitebox.setInternalState(mdsalStore, "client", mockInvoker); + HttpResponse mockResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockResponse).when(mockInvoker).doGet(Constants.GET_SHARD_LIST_PATH); + String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}"; + InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset())); + HttpEntity mockEntity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(is).when(mockEntity).getContent(); + Mockito.doReturn(mockEntity).when(mockResponse).getEntity(); + StatusLine mockStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine(); + Mockito.doReturn(200).when(mockStatusLine).getStatusCode(); + HttpResponse mockLeaderResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockLeaderResponse).when(mockInvoker).doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, "NodeName-shard-default-config")); + String httpLeaderString = "{\"value\":{\"Leader\":\"NodeName-shard-default-config\"}}"; + InputStream isLeader = new ByteArrayInputStream(httpLeaderString.getBytes(Charset.defaultCharset())); + HttpEntity mockLeaderEntity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(isLeader).when(mockLeaderEntity).getContent(); + Mockito.doReturn(mockLeaderEntity).when(mockLeaderResponse).getEntity(); + StatusLine mockLeaderStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(mockLeaderStatusLine).when(mockLeaderResponse).getStatusLine(); + Mockito.doReturn(200).when(mockLeaderStatusLine).getStatusCode(); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Whitebox.setInternalState(mdsalStore, "logger", mockLogger); + mdsalStore.storeYangModuleOnLeader("", ""); + Mockito.verify(mockLogger).debug("Current node is a leader."); + } + + @Test + public void testStoreYangModuleOnLeaderNotLeader() throws MDSALStoreException, APPCException, IllegalStateException, IOException { + RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class); + Whitebox.setInternalState(mdsalStore, "client", mockInvoker); + HttpResponse mockResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockResponse).when(mockInvoker).doGet(Constants.GET_SHARD_LIST_PATH); + String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}"; + InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset())); + HttpEntity mockEntity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(is).when(mockEntity).getContent(); + Mockito.doReturn(mockEntity).when(mockResponse).getEntity(); + StatusLine mockStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine(); + Mockito.doReturn(200).when(mockStatusLine).getStatusCode(); + HttpResponse mockLeaderResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockLeaderResponse).when(mockInvoker).doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, "NodeName-shard-default-config")); + String httpLeaderString = "{\"value\":{\"Leader\":\"OtherShardName\",\"PeerAddresses\":\"OtherShardName@adf:a\"}}"; + InputStream isLeader = new ByteArrayInputStream(httpLeaderString.getBytes(Charset.defaultCharset())); + HttpEntity mockLeaderEntity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(isLeader).when(mockLeaderEntity).getContent(); + Mockito.doReturn(mockLeaderEntity).when(mockLeaderResponse).getEntity(); + StatusLine mockLeaderStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(mockLeaderStatusLine).when(mockLeaderResponse).getStatusLine(); + Mockito.doReturn(200).when(mockLeaderStatusLine).getStatusCode(); + MDSALStoreImpl mdsalStoreSpy = Mockito.spy((MDSALStoreImpl) MDSALStoreFactory.createMDSALStore()); + RestClientInvoker mockRemoteInvoker = Mockito.mock(RestClientInvoker.class); + Mockito.doReturn(mockRemoteInvoker).when(mdsalStoreSpy).getRestClientInvoker(Mockito.any(URL.class)); + StatusLine mockRemoteStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(200).when(mockRemoteStatusLine).getStatusCode(); + HttpResponse mockLeaderRemoteResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockLeaderResponse).when(mockRemoteInvoker).doPost(Mockito.anyString(), Mockito.anyString()); + Mockito.doReturn(mockRemoteStatusLine).when(mockLeaderRemoteResponse).getStatusLine(); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Whitebox.setInternalState(mdsalStoreSpy, "logger", mockLogger); + mdsalStoreSpy.storeYangModuleOnLeader("", ""); + Mockito.verify(mockLogger).debug("Yang module successfully loaded on leader. Response code: 200"); + } + + @Test + public void testStoreJson() throws MDSALStoreException, APPCException, IllegalStateException, IOException { + RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class); + Whitebox.setInternalState(mdsalStore, "client", mockInvoker); + HttpResponse mockResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockResponse).when(mockInvoker).doPut(Mockito.anyString(), Mockito.anyString()); + String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}"; + InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset())); + HttpEntity mockEntity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(is).when(mockEntity).getContent(); + Mockito.doReturn(mockEntity).when(mockResponse).getEntity(); + StatusLine mockStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine(); + Mockito.doReturn(200).when(mockStatusLine).getStatusCode(); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Whitebox.setInternalState(mdsalStore, "logger", mockLogger); + mdsalStore.storeJson("", "", ""); + Mockito.verify(mockLogger).debug("Configuration JSON stored to MD-SAL store successfully. Response code: 200"); + } + + @Test + public void testStoreJsonRestconfResponse() throws MDSALStoreException, APPCException, IllegalStateException, IOException { + RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class); + Whitebox.setInternalState(mdsalStore, "client", mockInvoker); + HttpResponse mockResponse = Mockito.mock(HttpResponse.class); + Mockito.doReturn(mockResponse).when(mockInvoker).doPut(Mockito.anyString(), Mockito.anyString()); + String httpString = "{\"errors\":{\"error\":{\"error-message\":{\"error-message\":\"ERROR_MESSAGE\"}}}}"; + InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset())); + HttpEntity mockEntity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(is).when(mockEntity).getContent(); + Mockito.doReturn(mockEntity).when(mockResponse).getEntity(); + StatusLine mockStatusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine(); + Mockito.doReturn(199).when(mockStatusLine).getStatusCode(); + expectedEx.expect(MDSALStoreException.class); + expectedEx.expectMessage("Failed to load config JSON to MD SAL store. Error Message:"); + mdsalStore.storeJson("", "", ""); + } +} diff --git a/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/provider/MdsalStoreProviderTest.java b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/provider/MdsalStoreProviderTest.java new file mode 100644 index 000000000..2f0e4936a --- /dev/null +++ b/appc-dg/appc-dg-shared/appc-dg-mdsal-store/appc-dg-mdsal-bundle/src/test/java/org/onap/appc/mdsal/provider/MdsalStoreProviderTest.java @@ -0,0 +1,80 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2019 Ericsson + * ============================================================================= + * 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.appc.mdsal.provider; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.mdsal.MDSALStore; +import org.onap.appc.mdsal.impl.MDSALStoreFactory; +import org.onap.appc.mdsal.impl.MDSALStoreImpl; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.sal.binding.api.NotificationProviderService; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.MdsalStoreService; +import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; +import com.att.eelf.configuration.EELFLogger; +import org.junit.Assert; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(MDSALStoreFactory.class) +public class MdsalStoreProviderTest { + + private DataBroker dataBroker = Mockito.mock(DataBroker.class); + private RpcProviderRegistry rpcRegistry = Mockito.mock(RpcProviderRegistry.class); + private NotificationProviderService notificationService = Mockito.mock(NotificationProviderService.class); + + @Test + public void testClose() throws Exception { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + MdsalStoreProvider mdsalStoreProvider = new MdsalStoreProvider(dataBroker, notificationService, rpcRegistry); + Whitebox.setInternalState(mdsalStoreProvider, "log", mockLogger); + mdsalStoreProvider.close(); + Mockito.verify(mockLogger).info("Closing provider for MdsalStoreProvider"); + Mockito.verify(mockLogger).info("Successfully closed provider for MdsalStoreProvider"); + } + + @Test + public void testStoreYang() throws Exception { + PowerMockito.mockStatic(MDSALStoreFactory.class); + MDSALStore mdsalStore = Mockito.mock(MDSALStore.class); + PowerMockito.when(MDSALStoreFactory.createMDSALStore()).thenReturn(mdsalStore); + MdsalStoreProvider mdsalStoreProvider = new MdsalStoreProvider(dataBroker, notificationService, rpcRegistry); + StoreYangInput mockInput = Mockito.mock(StoreYangInput.class); + Assert.assertNotNull(mdsalStoreProvider.storeYang(mockInput)); + } + + @Test + public void testStoreYangExceptionFlow() throws Exception { + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + MdsalStoreProvider mdsalStoreProvider = new MdsalStoreProvider(dataBroker, notificationService, rpcRegistry); + Whitebox.setInternalState(mdsalStoreProvider, "log", mockLogger); + StoreYangInput mockInput = Mockito.mock(StoreYangInput.class); + mdsalStoreProvider.storeYang(mockInput); + Mockito.verify(mockLogger).error(Mockito.anyString(), Mockito.any(NullPointerException.class)); + } +} |