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