2 * ============LICENSE_START=======================================================
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
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.modelloader.service;
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;
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;
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;
42 @PrepareForTest({ DistributionClientFactory.class })
43 @RunWith(PowerMockRunner.class)
44 public class ModelLoaderServiceTest {
47 * //TODO this should be re-added once we come up with a strategy to fail
50 * @Test public void testNonExistentConfiguration(){
51 * ModelLoaderService.CONFIG_LOCATION = "FAKELOCATION";
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; }
57 * fail("Expecting runtime exception"); }
61 public void testConfigureStartDistributionClient() {
62 PowerMockito.mockStatic(DistributionClientFactory.class);
64 IDistributionClient mockClient = mock(IDistributionClient.class);
65 ModelLoaderConfig mockConfig = mock(ModelLoaderConfig.class);
67 when(DistributionClientFactory.createDistributionClient()).thenReturn(mockClient);
69 IDistributionClientResult result = mock(IDistributionClientResult.class);
71 when(result.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS);
72 when(mockClient.init(Matchers.<ModelLoaderConfig> any(), Matchers.<EventCallback> any()))
74 when(mockClient.start()).thenReturn(result);
76 new ModelLoaderService().init();
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();
85 public void testInitializeButNotStarted() {
86 PowerMockito.mockStatic(DistributionClientFactory.class);
88 IDistributionClient mockClient = mock(IDistributionClient.class);
89 ModelLoaderConfig mockConfig = mock(ModelLoaderConfig.class);
91 DistributionActionResultEnum failureReason = DistributionActionResultEnum.ASDC_CONNECTION_FAILED;
93 when(DistributionClientFactory.createDistributionClient()).thenReturn(mockClient);
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);
100 when(mockClient.init(Matchers.<ModelLoaderConfig> any(), Matchers.<EventCallback> any()))
101 .thenReturn(initResult);
102 when(mockClient.start()).thenReturn(startResult);
104 // TODO this should be re-added once we come up with a strategy to fail
107 * try{ new ModelLoaderService().init(mockConfig); }catch(RuntimeException
108 * e){ assertTrue(e.getMessage().contains(failureReason.toString()));
111 * fail("Expecting runtime exception with failure: " +
112 * failureReason.toString());