fa11462371067bc120a3388cf86d41117b4984f1
[appc.git] / appc-dg / appc-dg-shared / appc-dg-mdsal-store / appc-dg-mdsal-bundle / src / test / java / org / onap / appc / mdsal / impl / MDSALStoreImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
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  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.mdsal.impl;
23
24 import java.io.BufferedInputStream;
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.MalformedURLException;
29 import java.nio.charset.Charset;
30 import java.util.Date;
31 import org.apache.http.HttpEntity;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.StatusLine;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.onap.appc.exceptions.APPCException;
39 import org.onap.appc.mdsal.MDSALStore;
40 import org.onap.appc.mdsal.exception.MDSALStoreException;
41 import org.onap.appc.mdsal.objects.BundleInfo;
42 import org.onap.appc.rest.client.RestClientInvoker;
43 import org.osgi.framework.Bundle;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.FrameworkUtil;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49 import org.powermock.reflect.Whitebox;
50 import com.att.eelf.configuration.EELFLogger;
51 import org.junit.Assert;
52 import org.junit.Before;
53 import org.junit.Rule;
54 import java.net.URL;
55
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest(FrameworkUtil.class)
58 public class MDSALStoreImplTest {
59
60     private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
61     private final Bundle bundleService=Mockito.mock(Bundle.class);
62     private MDSALStoreImpl mdsalStore;
63
64     @Rule
65     public ExpectedException expectedEx = ExpectedException.none();
66
67     @Before
68     public void setup() {
69         mdsalStore = (MDSALStoreImpl) MDSALStoreFactory.createMDSALStore();
70         PowerMockito.mockStatic(FrameworkUtil.class);
71         PowerMockito.when(FrameworkUtil.getBundle(MDSALStoreImpl.class)).thenReturn(bundleService);
72         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
73         PowerMockito.when(bundleContext.getBundle("TEST_MODULE_NAME")).thenReturn(bundleService);
74     }
75
76     @Test
77     public void testMDSALStoreImpl() {
78         Assert.assertTrue(mdsalStore.isModulePresent("TEST_MODULE_NAME", new Date()));
79     }
80
81     @Test
82     public void testStoreYangModule() throws MDSALStoreException {
83         expectedEx.expect(MDSALStoreException.class);
84         expectedEx.expectMessage("Error storing yang module:");
85         mdsalStore.storeYangModule("", new BundleInfo());
86     }
87
88     @Test
89     public void testStoreYangModuleOnLeader() throws MDSALStoreException, APPCException, IllegalStateException, IOException {
90         RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
91         Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
92         HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
93         Mockito.doReturn(mockResponse).when(mockInvoker).doGet(Constants.GET_SHARD_LIST_PATH);
94         String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}";
95         InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
96         HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
97         Mockito.doReturn(is).when(mockEntity).getContent();
98         Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
99         StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
100         Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
101         Mockito.doReturn(200).when(mockStatusLine).getStatusCode();
102         HttpResponse mockLeaderResponse = Mockito.mock(HttpResponse.class);
103         Mockito.doReturn(mockLeaderResponse).when(mockInvoker).doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, "NodeName-shard-default-config"));
104         String httpLeaderString = "{\"value\":{\"Leader\":\"NodeName-shard-default-config\"}}";
105         InputStream isLeader = new ByteArrayInputStream(httpLeaderString.getBytes(Charset.defaultCharset()));
106         HttpEntity mockLeaderEntity = Mockito.mock(HttpEntity.class);
107         Mockito.doReturn(isLeader).when(mockLeaderEntity).getContent();
108         Mockito.doReturn(mockLeaderEntity).when(mockLeaderResponse).getEntity();
109         StatusLine mockLeaderStatusLine = Mockito.mock(StatusLine.class);
110         Mockito.doReturn(mockLeaderStatusLine).when(mockLeaderResponse).getStatusLine();
111         Mockito.doReturn(200).when(mockLeaderStatusLine).getStatusCode();
112         EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
113         Whitebox.setInternalState(mdsalStore, "logger", mockLogger);
114         mdsalStore.storeYangModuleOnLeader("", "");
115         Mockito.verify(mockLogger).debug("Current node is a leader.");
116     }
117     
118     @Test
119     public void testStoreYangModuleOnLeaderNotLeader() throws MDSALStoreException, APPCException, IllegalStateException, IOException {
120         RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
121         Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
122         HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
123         Mockito.doReturn(mockResponse).when(mockInvoker).doGet(Constants.GET_SHARD_LIST_PATH);
124         String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}";
125         InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
126         HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
127         Mockito.doReturn(is).when(mockEntity).getContent();
128         Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
129         StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
130         Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
131         Mockito.doReturn(200).when(mockStatusLine).getStatusCode();
132         HttpResponse mockLeaderResponse = Mockito.mock(HttpResponse.class);
133         Mockito.doReturn(mockLeaderResponse).when(mockInvoker).doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, "NodeName-shard-default-config"));
134         String httpLeaderString = "{\"value\":{\"Leader\":\"OtherShardName\",\"PeerAddresses\":\"OtherShardName@adf:a\"}}";
135         InputStream isLeader = new ByteArrayInputStream(httpLeaderString.getBytes(Charset.defaultCharset()));
136         HttpEntity mockLeaderEntity = Mockito.mock(HttpEntity.class);
137         Mockito.doReturn(isLeader).when(mockLeaderEntity).getContent();
138         Mockito.doReturn(mockLeaderEntity).when(mockLeaderResponse).getEntity();
139         StatusLine mockLeaderStatusLine = Mockito.mock(StatusLine.class);
140         Mockito.doReturn(mockLeaderStatusLine).when(mockLeaderResponse).getStatusLine();
141         Mockito.doReturn(200).when(mockLeaderStatusLine).getStatusCode();
142         MDSALStoreImpl mdsalStoreSpy = Mockito.spy((MDSALStoreImpl) MDSALStoreFactory.createMDSALStore());
143         RestClientInvoker mockRemoteInvoker = Mockito.mock(RestClientInvoker.class);
144         Mockito.doReturn(mockRemoteInvoker).when(mdsalStoreSpy).getRestClientInvoker(Mockito.any(URL.class));
145         StatusLine mockRemoteStatusLine = Mockito.mock(StatusLine.class);
146         Mockito.doReturn(200).when(mockRemoteStatusLine).getStatusCode();
147         HttpResponse mockLeaderRemoteResponse = Mockito.mock(HttpResponse.class);
148         Mockito.doReturn(mockLeaderResponse).when(mockRemoteInvoker).doPost(Mockito.anyString(), Mockito.anyString());
149         Mockito.doReturn(mockRemoteStatusLine).when(mockLeaderRemoteResponse).getStatusLine();
150         EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
151         Whitebox.setInternalState(mdsalStoreSpy, "logger", mockLogger);
152         mdsalStoreSpy.storeYangModuleOnLeader("", "");
153         Mockito.verify(mockLogger).debug("Yang module successfully loaded on leader. Response code: 200");
154     }
155
156     @Test
157     public void testStoreJson() throws MDSALStoreException, APPCException, IllegalStateException, IOException {
158         RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
159         Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
160         HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
161         Mockito.doReturn(mockResponse).when(mockInvoker).doPut(Mockito.anyString(), Mockito.anyString());
162         String httpString = "{\"value\":{\"MemberName\":\"NodeName\"}}";
163         InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
164         HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
165         Mockito.doReturn(is).when(mockEntity).getContent();
166         Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
167         StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
168         Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
169         Mockito.doReturn(200).when(mockStatusLine).getStatusCode();
170         EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
171         Whitebox.setInternalState(mdsalStore, "logger", mockLogger);
172         mdsalStore.storeJson("", "", "");
173         Mockito.verify(mockLogger).debug("Configuration JSON stored to MD-SAL store successfully. Response code: 200");
174     }
175
176     @Test
177     public void testStoreJsonRestconfResponse() throws MDSALStoreException, APPCException, IllegalStateException, IOException {
178         RestClientInvoker mockInvoker = Mockito.mock(RestClientInvoker.class);
179         Whitebox.setInternalState(mdsalStore, "client", mockInvoker);
180         HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
181         Mockito.doReturn(mockResponse).when(mockInvoker).doPut(Mockito.anyString(), Mockito.anyString());
182         String httpString = "{\"errors\":{\"error\":{\"error-message\":{\"error-message\":\"ERROR_MESSAGE\"}}}}";
183         InputStream is = new ByteArrayInputStream(httpString.getBytes(Charset.defaultCharset()));
184         HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
185         Mockito.doReturn(is).when(mockEntity).getContent();
186         Mockito.doReturn(mockEntity).when(mockResponse).getEntity();
187         StatusLine mockStatusLine = Mockito.mock(StatusLine.class);
188         Mockito.doReturn(mockStatusLine).when(mockResponse).getStatusLine();
189         Mockito.doReturn(199).when(mockStatusLine).getStatusCode();
190         expectedEx.expect(MDSALStoreException.class);
191         expectedEx.expectMessage("Failed to load config JSON to MD SAL store. Error Message:");
192         mdsalStore.storeJson("", "", "");
193     }
194 }