Initial OpenECOMP A&AI Model Loader commit
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / service / ModelLoaderServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * MODEL LOADER SERVICE
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.modelloader.service;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Matchers;
31 import org.openecomp.modelloader.config.ModelLoaderConfig;
32 import org.openecomp.modelloader.notification.EventCallback;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37 import org.openecomp.sdc.api.IDistributionClient;
38 import org.openecomp.sdc.api.results.IDistributionClientResult;
39 import org.openecomp.sdc.impl.DistributionClientFactory;
40 import org.openecomp.sdc.utils.DistributionActionResultEnum;
41
42 @PrepareForTest({ DistributionClientFactory.class })
43 @RunWith(PowerMockRunner.class)
44 public class ModelLoaderServiceTest {
45
46   /*
47    * //TODO this should be re-added once we come up with a strategy to fail
48    * gracefully
49    * 
50    * @Test public void testNonExistentConfiguration(){
51    * ModelLoaderService.CONFIG_LOCATION = "FAKELOCATION";
52    * 
53    * try{ new ModelLoaderService().start(); }catch(RuntimeException e){
54    * assertTrue("Got unexpected message from error log",
55    * e.getMessage().contains("Failed to load configuration")); return; }
56    * 
57    * fail("Expecting runtime exception"); }
58    */
59
60   @Test
61   public void testConfigureStartDistributionClient() {
62     PowerMockito.mockStatic(DistributionClientFactory.class);
63
64     IDistributionClient mockClient = mock(IDistributionClient.class);
65     ModelLoaderConfig mockConfig = mock(ModelLoaderConfig.class);
66
67     when(DistributionClientFactory.createDistributionClient()).thenReturn(mockClient);
68
69     IDistributionClientResult result = mock(IDistributionClientResult.class);
70
71     when(result.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);
72     when(mockClient.init(Matchers.<ModelLoaderConfig> any(), Matchers.<EventCallback> any()))
73         .thenReturn(result);
74     when(mockClient.start()).thenReturn(result);
75
76     new ModelLoaderService().init();
77
78     // Validate that the client was initialized and started
79     verify(mockClient, times(1)).init(Matchers.<ModelLoaderConfig> any(),
80         Matchers.<EventCallback> any());
81     verify(mockClient, times(1)).start();
82   }
83
84   @Test
85   public void testInitializeButNotStarted() {
86     PowerMockito.mockStatic(DistributionClientFactory.class);
87
88     IDistributionClient mockClient = mock(IDistributionClient.class);
89     ModelLoaderConfig mockConfig = mock(ModelLoaderConfig.class);
90
91     DistributionActionResultEnum failureReason = DistributionActionResultEnum.ASDC_CONNECTION_FAILED;
92
93     when(DistributionClientFactory.createDistributionClient()).thenReturn(mockClient);
94
95     IDistributionClientResult initResult = mock(IDistributionClientResult.class);
96     when(initResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);
97     IDistributionClientResult startResult = mock(IDistributionClientResult.class);
98     when(startResult.getDistributionActionResult()).thenReturn(failureReason);
99
100     when(mockClient.init(Matchers.<ModelLoaderConfig> any(), Matchers.<EventCallback> any()))
101         .thenReturn(initResult);
102     when(mockClient.start()).thenReturn(startResult);
103
104     // TODO this should be re-added once we come up with a strategy to fail
105     // gracefully
106     /*
107      * try{ new ModelLoaderService().init(mockConfig); }catch(RuntimeException
108      * e){ assertTrue(e.getMessage().contains(failureReason.toString()));
109      * return; }
110      * 
111      * fail("Expecting runtime exception with failure: " +
112      * failureReason.toString());
113      */
114   }
115 }