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