2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Ericsson
6 * ================================================================================
7 * Modifications Copyright (C) 2019 AT&T Intellectual Property
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.mdsal.impl;
26 import java.io.BufferedInputStream;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.MalformedURLException;
31 import java.nio.charset.Charset;
32 import java.util.Date;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.StatusLine;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.mdsal.MDSALStore;
42 import org.onap.appc.mdsal.exception.MDSALStoreException;
43 import org.onap.appc.mdsal.objects.BundleInfo;
44 import org.onap.appc.rest.client.RestClientInvoker;
45 import org.osgi.framework.Bundle;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.FrameworkUtil;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import org.powermock.reflect.Whitebox;
52 import com.att.eelf.configuration.EELFLogger;
53 import org.junit.Assert;
54 import org.junit.Before;
55 import org.junit.Rule;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest(FrameworkUtil.class)
60 public class MDSALStoreImplTest {
62 private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
63 private final Bundle bundleService = Mockito.mock(Bundle.class);
64 private MDSALStoreImpl mdsalStore;
67 public ExpectedException expectedEx = ExpectedException.none();
71 mdsalStore = (MDSALStoreImpl) MDSALStoreFactory.createMDSALStore();
72 PowerMockito.mockStatic(FrameworkUtil.class);
73 PowerMockito.when(FrameworkUtil.getBundle(MDSALStoreImpl.class)).thenReturn(bundleService);
74 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
75 PowerMockito.when(bundleContext.getBundle("TEST_MODULE_NAME")).thenReturn(bundleService);
79 public void testMDSALStoreImpl() {
80 Assert.assertTrue(mdsalStore.isModulePresent("TEST_MODULE_NAME", new Date()));
84 public void testStoreYangModule() throws MDSALStoreException {
85 expectedEx.expect(MDSALStoreException.class);
86 expectedEx.expectMessage("Error storing yang module:");
87 mdsalStore.storeYangModule("", new BundleInfo());
91 public void testStoreYangModuleOnLeader()
92 throws MDSALStoreException, APPCException, IllegalStateException, IOException {
93 RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
94 Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
95 HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
96 Mockito.doReturn(mockResponse).when(mockInvoker).doGet(Constants.GET_SHARD_LIST_PATH);
97 String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}";
98 InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
99 HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
100 Mockito.doReturn(is).when(mockEntity).getContent();
101 Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
102 StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
103 Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
104 Mockito.doReturn(200).when(mockStatusLine).getStatusCode();
105 HttpResponse mockLeaderResponse = Mockito.mock(HttpResponse.class);
106 Mockito.doReturn(mockLeaderResponse).when(mockInvoker)
107 .doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, "NodeName-shard-default-config"));
108 String httpLeaderString = "{\"value\":{\"Leader\":\"NodeName-shard-default-config\"}}";
109 InputStream isLeader = new ByteArrayInputStream(httpLeaderString.getBytes(Charset.defaultCharset()));
110 HttpEntity mockLeaderEntity = Mockito.mock(HttpEntity.class);
111 Mockito.doReturn(isLeader).when(mockLeaderEntity).getContent();
112 Mockito.doReturn(mockLeaderEntity).when(mockLeaderResponse).getEntity();
113 StatusLine mockLeaderStatusLine = Mockito.mock(StatusLine.class);
114 Mockito.doReturn(mockLeaderStatusLine).when(mockLeaderResponse).getStatusLine();
115 Mockito.doReturn(200).when(mockLeaderStatusLine).getStatusCode();
116 EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
117 Whitebox.setInternalState(mdsalStore, "logger", mockLogger);
118 mdsalStore.storeYangModuleOnLeader("", "");
119 Mockito.verify(mockLogger).debug("Current node is a leader.");
123 public void testStoreYangModuleOnLeaderNotLeader()
124 throws MDSALStoreException, APPCException, IllegalStateException, IOException {
125 RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
126 Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
127 HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
128 Mockito.doReturn(mockResponse).when(mockInvoker).doGet(Constants.GET_SHARD_LIST_PATH);
129 String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}";
130 InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
131 HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
132 Mockito.doReturn(is).when(mockEntity).getContent();
133 Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
134 StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
135 Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
136 Mockito.doReturn(200).when(mockStatusLine).getStatusCode();
137 HttpResponse mockLeaderResponse = Mockito.mock(HttpResponse.class);
138 Mockito.doReturn(mockLeaderResponse).when(mockInvoker)
139 .doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, "NodeName-shard-default-config"));
140 String httpLeaderString =
141 "{\"value\":{\"Leader\":\"OtherShardName\",\"PeerAddresses\":\"OtherShardName@adf:a\"}}";
142 InputStream isLeader = new ByteArrayInputStream(httpLeaderString.getBytes(Charset.defaultCharset()));
143 HttpEntity mockLeaderEntity = Mockito.mock(HttpEntity.class);
144 Mockito.doReturn(isLeader).when(mockLeaderEntity).getContent();
145 Mockito.doReturn(mockLeaderEntity).when(mockLeaderResponse).getEntity();
146 StatusLine mockLeaderStatusLine = Mockito.mock(StatusLine.class);
147 Mockito.doReturn(mockLeaderStatusLine).when(mockLeaderResponse).getStatusLine();
148 Mockito.doReturn(200).when(mockLeaderStatusLine).getStatusCode();
149 MDSALStoreImpl mdsalStoreSpy = Mockito.spy((MDSALStoreImpl) MDSALStoreFactory.createMDSALStore());
150 RestClientInvoker mockRemoteInvoker = Mockito.mock(RestClientInvoker.class);
151 Mockito.doReturn(mockRemoteInvoker).when(mdsalStoreSpy).getRestClientInvoker(Mockito.any(URL.class));
152 StatusLine mockRemoteStatusLine = Mockito.mock(StatusLine.class);
153 Mockito.doReturn(200).when(mockRemoteStatusLine).getStatusCode();
154 HttpResponse mockLeaderRemoteResponse = Mockito.mock(HttpResponse.class);
155 Mockito.doReturn(mockLeaderResponse).when(mockRemoteInvoker).doPost(Mockito.anyString(), Mockito.anyString());
156 Mockito.doReturn(mockRemoteStatusLine).when(mockLeaderRemoteResponse).getStatusLine();
157 EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
158 Whitebox.setInternalState(mdsalStoreSpy, "logger", mockLogger);
159 mdsalStoreSpy.storeYangModuleOnLeader("", "");
160 Mockito.verify(mockLogger).debug("Yang module successfully loaded on leader. Response code: 200");
164 public void testStoreJson() throws MDSALStoreException, APPCException, IllegalStateException, IOException {
165 RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
166 Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
167 HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
168 Mockito.doReturn(mockResponse).when(mockInvoker).doPut(Mockito.anyString(), Mockito.anyString());
169 String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}";
170 InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
171 HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
172 Mockito.doReturn(is).when(mockEntity).getContent();
173 Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
174 StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
175 Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
176 Mockito.doReturn(200).when(mockStatusLine).getStatusCode();
177 EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
178 Whitebox.setInternalState(mdsalStore, "logger", mockLogger);
179 mdsalStore.storeJson("", "", "");
180 Mockito.verify(mockLogger).debug("Configuration JSON stored to MD-SAL store successfully. Response code: 200");
184 public void testStoreJsonRestconfResponse()
185 throws MDSALStoreException, APPCException, IllegalStateException, IOException {
186 RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
187 Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
188 HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
189 Mockito.doReturn(mockResponse).when(mockInvoker).doPut(Mockito.anyString(), Mockito.anyString());
190 String httpString = "{\"errors\":{\"error\":{\"error-message\":{\"error-message\":\"ERROR_MESSAGE\"}}}}";
191 InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
192 HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
193 Mockito.doReturn(is).when(mockEntity).getContent();
194 Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
195 StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
196 Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
197 Mockito.doReturn(199).when(mockStatusLine).getStatusCode();
198 expectedEx.expect(MDSALStoreException.class);
199 expectedEx.expectMessage("Failed to load config JSON to MD SAL store. Error Message:");
200 mdsalStore.storeJson("", "", "");