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 MicroServicePolicyTest {
45 public ExpectedException thrown = ExpectedException.none();
48 public void testConstructor1() {
49 thrown.expect(NullPointerException.class);
50 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy();
51 policy.getCorrectPolicyDataObject();
52 fail("Expected an exception");
56 public void testConstructor2() {
57 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
58 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(policyAdapter);
59 assertNull(policy.getCorrectPolicyDataObject());
62 @PrepareForTest({MicroServiceConfigPolicy.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 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(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(policy.isPreparedToSave(), true);
82 public void testCreateConstructor1() {
83 CreateNewMicroServiceModel model = new CreateNewMicroServiceModel(null, null, null, null);
87 @PrepareForTest({CreateNewMicroServiceModel.class})
89 public void testCreateModel() throws Exception {
90 // Mock file retrieval
91 File testFile = new File("testFile");
92 File[] testList = new File[1];
93 testList[0] = testFile;
94 File impl = Mockito.mock(File.class);
95 PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(impl);
96 when(impl.listFiles()).thenReturn(testList);
97 when(impl.isFile()).thenReturn(true);
99 // Mock internal dictionary retrieval
100 CommonClassDaoImpl daoImpl = Mockito.mock(CommonClassDaoImpl.class);
101 PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(daoImpl);
102 when(daoImpl.getDataById(any(), anyString(), anyString())).thenReturn(Collections.emptyList());
104 // Test create methods
105 String testFileName = "testFile.zip";
106 String testVal = "testVal";
107 CreateNewMicroServiceModel model = new CreateNewMicroServiceModel(testFileName, testVal, testVal, testVal, testVal);
108 model.addValuesToNewModel(".xmi");
109 model.saveImportService();