2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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=========================================================
20 package org.onap.policy.pap.xacml.rest.components;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.Matchers.any;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mockito;
34 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
35 import org.onap.policy.rest.adapter.PolicyRestAdapter;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
40 import java.util.Collections;
42 @RunWith(PowerMockRunner.class)
43 public class OptimizationConfigPolicyTest {
45 public ExpectedException thrown = ExpectedException.none();
48 public void testConstructor1() {
49 thrown.expect(NullPointerException.class);
50 OptimizationConfigPolicy policy = new OptimizationConfigPolicy();
51 policy.getCorrectPolicyDataObject();
52 fail("Expected an exception");
56 public void testConstructor2() {
57 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
58 OptimizationConfigPolicy policy = new OptimizationConfigPolicy(policyAdapter);
59 assertNull(policy.getCorrectPolicyDataObject());
62 @PrepareForTest({OptimizationConfigPolicy.class})
64 public void testPrepareToSave() throws Exception {
65 // Need to mock internal dictionary retrieval
66 CommonClassDaoImpl impl = Mockito.mock(CommonClassDaoImpl.class);
67 PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(impl);
68 when(impl.getDataById(any(), anyString(), anyString())).thenReturn(null);
70 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
71 OptimizationConfigPolicy policy = new OptimizationConfigPolicy(policyAdapter);
72 policyAdapter.setHighestVersion(1);
73 policyAdapter.setPolicyType("Config");
74 policyAdapter.setNewFileName("foo.xml");
75 policyAdapter.setJsonBody("{ \"version\": \"1.0\"}");
76 policyAdapter.setServiceType("foo");
77 policy.prepareToSave();
78 assertEquals(true, policy.isPreparedToSave());
81 @PrepareForTest({CreateNewOptimizationModel.class})
83 public void testCreateModel() throws Exception {
84 // Mock file retrieval
85 File testFile = new File("testFile");
86 File[] testList = new File[1];
87 testList[0] = testFile;
88 File impl = Mockito.mock(File.class);
89 PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(impl);
90 when(impl.listFiles()).thenReturn(testList);
91 when(impl.isFile()).thenReturn(true);
93 // Mock internal dictionary retrieval
94 CommonClassDaoImpl daoImpl = Mockito.mock(CommonClassDaoImpl.class);
95 PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(daoImpl);
96 when(daoImpl.getDataById(any(), anyString(), anyString())).thenReturn(Collections.emptyList());
98 // Test create methods
99 String testFileName = "testFile.zip";
100 String testVal = "testVal";
101 CreateNewOptimizationModel model = new CreateNewOptimizationModel(testFileName, testVal, testVal, testVal, testVal);
102 model.addValuesToNewModel();
103 model.saveImportService();