2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.asdc.client;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import java.io.IOException;
27 import java.net.URISyntaxException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.security.MessageDigest;
31 import java.security.NoSuchAlgorithmException;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.apache.commons.codec.binary.Base64;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.mockito.Mockito;
42 import org.onap.sdc.api.notification.IArtifactInfo;
43 import org.onap.sdc.api.notification.INotificationData;
44 import org.onap.sdc.api.notification.IResourceInstance;
45 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
46 import org.onap.sdc.api.results.IDistributionClientResult;
47 import org.onap.sdc.utils.DistributionActionResultEnum;
48 import org.openecomp.mso.asdc.client.ASDCConfiguration;
49 import org.openecomp.mso.asdc.client.ASDCGlobalController;
50 import org.openecomp.mso.asdc.client.exceptions.ASDCControllerException;
51 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
52 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
53 import org.openecomp.mso.properties.MsoJavaProperties;
54 import org.openecomp.mso.properties.MsoPropertiesException;
55 import org.openecomp.mso.properties.MsoPropertiesFactory;
60 * THis class tests the ASDC Controller by using the ASDC Mock CLient
64 public class ASDCGlobalControllerTest {
66 private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
68 private static String heatExample;
69 private static String heatExampleMD5HashBase64;
71 private static INotificationData iNotif;
73 private static IDistributionClientDownloadResult downloadResult;
74 private static IDistributionClientDownloadResult downloadCorruptedResult;
76 private static IDistributionClientResult successfulClientInitResult;
77 private static IDistributionClientResult unsuccessfulClientInitResult;
79 private static IArtifactInfo artifactInfo1;
81 private static IResourceInstance resource1;
83 public static final String ASDC_PROP = MsoJavaProperties.class.getClassLoader().getResource("mso.json").toString().substring(5);
84 public static final String ASDC_PROP2 = MsoJavaProperties.class.getClassLoader().getResource("mso2.json").toString().substring(5);
85 public static final String ASDC_PROP3 = MsoJavaProperties.class.getClassLoader().getResource("mso3.json").toString().substring(5);
86 public static final String ASDC_PROP_BAD = MsoJavaProperties.class.getClassLoader().getResource("mso-bad.json").toString().substring(5);
87 public static final String ASDC_PROP_WITH_NULL = MsoJavaProperties.class.getClassLoader().getResource("mso-with-NULL.json").toString().substring(5);
88 public static final String ASDC_PROP_WITH_DOUBLE = MsoJavaProperties.class.getClassLoader().getResource("mso-two-configs.json").toString().substring(5);
89 public static final String ASDC_PROP_WITH_DOUBLE2 = MsoJavaProperties.class.getClassLoader().getResource("mso-two-configs2.json").toString().substring(5);
92 public static final void prepareMockNotification() throws MsoPropertiesException, IOException, URISyntaxException, NoSuchAlgorithmException, ArtifactInstallerException {
94 heatExample = new String(Files.readAllBytes(Paths.get(ASDCGlobalControllerTest.class.getClassLoader().getResource("resource-examples/autoscaling.yaml").toURI())));
95 MessageDigest md = MessageDigest.getInstance("MD5");
96 byte[] md5Hash = md.digest(heatExample.getBytes());
97 heatExampleMD5HashBase64 = Base64.encodeBase64String(md5Hash);
99 iNotif= Mockito.mock(INotificationData.class);
101 // Create fake ArtifactInfo
102 artifactInfo1 = Mockito.mock(IArtifactInfo.class);
103 Mockito.when(artifactInfo1.getArtifactChecksum()).thenReturn(ASDCGlobalControllerTest.heatExampleMD5HashBase64);
105 Mockito.when(artifactInfo1.getArtifactName()).thenReturn("artifact1");
106 Mockito.when(artifactInfo1.getArtifactType()).thenReturn(ASDCConfiguration.HEAT);
107 Mockito.when(artifactInfo1.getArtifactURL()).thenReturn("https://localhost:8080/v1/catalog/services/srv1/2.0/resources/aaa/1.0/artifacts/aaa.yml");
108 Mockito.when(artifactInfo1.getArtifactUUID()).thenReturn("UUID1");
109 Mockito.when(artifactInfo1.getArtifactDescription()).thenReturn("testos artifact1");
111 // Now provision the NotificationData mock
112 List<IArtifactInfo> listArtifact = new ArrayList<>();
113 listArtifact.add(artifactInfo1);
115 // Create fake resource Instance
116 resource1 = Mockito.mock (IResourceInstance.class);
117 Mockito.when (resource1.getResourceType ()).thenReturn ("VF");
118 Mockito.when (resource1.getResourceName ()).thenReturn ("resourceName");
119 Mockito.when (resource1.getArtifacts ()).thenReturn (listArtifact);
121 List<IResourceInstance> resources = new ArrayList<> ();
122 resources.add (resource1);
124 Mockito.when(iNotif.getResources()).thenReturn(resources);
125 Mockito.when(iNotif.getDistributionID()).thenReturn("distributionID1");
126 Mockito.when(iNotif.getServiceName()).thenReturn("serviceName1");
127 Mockito.when(iNotif.getServiceUUID()).thenReturn("serviceNameUUID1");
128 Mockito.when(iNotif.getServiceVersion()).thenReturn("1.0");
130 downloadResult = Mockito.mock(IDistributionClientDownloadResult.class);
131 Mockito.when(downloadResult.getArtifactPayload()).thenReturn(heatExample.getBytes());
132 Mockito.when(downloadResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);
133 Mockito.when(downloadResult.getDistributionMessageResult()).thenReturn("Success");
135 downloadCorruptedResult = Mockito.mock(IDistributionClientDownloadResult.class);
136 Mockito.when(downloadCorruptedResult.getArtifactPayload()).thenReturn((heatExample+"badone").getBytes());
137 Mockito.when(downloadCorruptedResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);
138 Mockito.when(downloadCorruptedResult.getDistributionMessageResult()).thenReturn("Success");
141 // Mock now the ASDC distribution client behavior
142 successfulClientInitResult = Mockito.mock(IDistributionClientResult.class);
143 Mockito.when(successfulClientInitResult.getDistributionActionResult ()).thenReturn(DistributionActionResultEnum.SUCCESS);
145 unsuccessfulClientInitResult = Mockito.mock(IDistributionClientResult.class);
146 Mockito.when(unsuccessfulClientInitResult.getDistributionActionResult ()).thenReturn(DistributionActionResultEnum.GENERAL_ERROR);
151 public final void initBeforeEachTest() throws MsoPropertiesException {
153 msoPropertiesFactory.removeAllMsoProperties();
154 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
158 public static final void kill () throws MsoPropertiesException {
160 msoPropertiesFactory.removeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC);
164 public final void testUpdateControllersConfigIfNeeded() throws ASDCControllerException, ASDCParametersException, IOException, MsoPropertiesException {
166 ASDCGlobalController asdcGlobalController = new ASDCGlobalController();
167 assertTrue(asdcGlobalController.getControllers().size()==0);
170 assertTrue(asdcGlobalController.updateControllersConfigIfNeeded());
171 assertTrue(asdcGlobalController.getControllers().size()==1);
172 assertTrue(asdcGlobalController.getControllers().get("asdc-controller1") != null);
175 msoPropertiesFactory.removeAllMsoProperties();
176 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_WITH_DOUBLE);
177 assertTrue(asdcGlobalController.updateControllersConfigIfNeeded());
178 assertTrue(asdcGlobalController.getControllers().size()==2);
179 assertTrue(asdcGlobalController.getControllers().get("asdc-controller1") != null);
180 assertTrue(asdcGlobalController.getControllers().get("asdc-controller2") != null);
181 // Check that update does nothing
182 assertFalse(asdcGlobalController.updateControllersConfigIfNeeded());
183 assertTrue(asdcGlobalController.getControllers().size()==2);
185 // Change the second one name
186 msoPropertiesFactory.removeAllMsoProperties();
187 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_WITH_DOUBLE2);
188 assertTrue(asdcGlobalController.updateControllersConfigIfNeeded());
189 assertTrue(asdcGlobalController.getControllers().size()==2);
190 assertTrue(asdcGlobalController.getControllers().get("asdc-controller1") != null);
191 assertTrue(asdcGlobalController.getControllers().get("asdc-controller2B") != null);
197 public final void testCloseASDC() {
199 ASDCGlobalController asdcGlobalController = new ASDCGlobalController();
200 assertTrue(asdcGlobalController.getControllers().size()==0);
203 assertTrue(asdcGlobalController.updateControllersConfigIfNeeded());
204 assertTrue(asdcGlobalController.getControllers().size()==1);
205 assertTrue(asdcGlobalController.getControllers().get("asdc-controller1") != null);
207 asdcGlobalController.closeASDC();
208 assertTrue(asdcGlobalController.getControllers().size()==0);