9ac50c4854c36ca659bcd81ffe909f7ecbd6a8f7
[so.git] / asdc-controller / src / test / java / org / openecomp / mso / asdc / client / tests / ASDCGlobalControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.asdc.client.tests;
22
23
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;
34
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;
41
42 import org.openecomp.sdc.api.notification.IArtifactInfo;
43 import org.openecomp.sdc.api.notification.INotificationData;
44 import org.openecomp.sdc.api.notification.IResourceInstance;
45 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
46 import org.openecomp.sdc.api.results.IDistributionClientResult;
47 import org.openecomp.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;
56
57
58
59 /**
60  * THis class tests the ASDC Controller by using the ASDC Mock CLient
61  *
62  *
63  */
64 public class ASDCGlobalControllerTest {
65
66         private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
67
68         private static String heatExample;
69         private static String heatExampleMD5HashBase64;
70
71         private static INotificationData iNotif;
72
73         private static IDistributionClientDownloadResult downloadResult;
74         private static IDistributionClientDownloadResult downloadCorruptedResult;
75
76         private static IDistributionClientResult successfulClientInitResult;
77         private static IDistributionClientResult unsuccessfulClientInitResult;
78
79         private static IArtifactInfo artifactInfo1;
80
81         private static IResourceInstance resource1;
82
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);
90
91         @BeforeClass
92         public static final void prepareMockNotification() throws MsoPropertiesException, IOException, URISyntaxException, NoSuchAlgorithmException, ArtifactInstallerException  {
93
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);
98
99                 iNotif= Mockito.mock(INotificationData.class);
100
101                 // Create fake ArtifactInfo
102                 artifactInfo1 = Mockito.mock(IArtifactInfo.class);
103                 Mockito.when(artifactInfo1.getArtifactChecksum()).thenReturn(ASDCGlobalControllerTest.heatExampleMD5HashBase64);
104
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");
110
111                 // Now provision the NotificationData mock
112                 List<IArtifactInfo> listArtifact = new ArrayList<IArtifactInfo>();
113                 listArtifact.add(artifactInfo1);
114
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);
120
121         List<IResourceInstance> resources = new ArrayList<> ();
122         resources.add (resource1);
123
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");
129
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");
134
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");
139
140
141                 // Mock now the ASDC distribution client behavior
142                 successfulClientInitResult = Mockito.mock(IDistributionClientResult.class);
143                 Mockito.when(successfulClientInitResult.getDistributionActionResult ()).thenReturn(DistributionActionResultEnum.SUCCESS);
144
145                 unsuccessfulClientInitResult = Mockito.mock(IDistributionClientResult.class);
146                 Mockito.when(unsuccessfulClientInitResult.getDistributionActionResult ()).thenReturn(DistributionActionResultEnum.GENERAL_ERROR);
147
148         }
149         
150         @Before
151         public final void initBeforeEachTest() throws MsoPropertiesException {
152                 // load the config
153                 msoPropertiesFactory.removeAllMsoProperties();
154                 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
155         }
156
157         @AfterClass
158         public static final void kill () throws MsoPropertiesException {
159
160                 msoPropertiesFactory.removeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC);
161         }
162
163         @Test
164         public final void testUpdateControllersConfigIfNeeded() throws ASDCControllerException, ASDCParametersException, IOException, MsoPropertiesException {
165
166                 ASDCGlobalController asdcGlobalController = new ASDCGlobalController();
167                 assertTrue(asdcGlobalController.getControllers().size()==0);
168                 
169                 // first init
170                 assertTrue(asdcGlobalController.updateControllersConfigIfNeeded());
171                 assertTrue(asdcGlobalController.getControllers().size()==1);
172                 assertTrue(asdcGlobalController.getControllers().get("asdc-controller1") != null);
173                 
174                 // Add a second one
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);
184                 
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);
192                 
193                 
194         }
195         
196         @Test
197         public final void testCloseASDC()  {
198
199                 ASDCGlobalController asdcGlobalController = new ASDCGlobalController();
200                 assertTrue(asdcGlobalController.getControllers().size()==0);
201                 
202                 // first init
203                 assertTrue(asdcGlobalController.updateControllersConfigIfNeeded());
204                 assertTrue(asdcGlobalController.getControllers().size()==1);
205                 assertTrue(asdcGlobalController.getControllers().get("asdc-controller1") != null);
206                 
207                 asdcGlobalController.closeASDC();
208                 assertTrue(asdcGlobalController.getControllers().size()==0);
209                 
210                 
211         }
212
213 }