Merge "Fixed the Policy GUI Editor tab right click issue."
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / MicroServicePolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.anyString;
29 import static org.mockito.Mockito.when;
30
31 import java.io.File;
32 import java.util.Collections;
33
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
40 import org.onap.policy.rest.adapter.PolicyRestAdapter;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 @RunWith(PowerMockRunner.class)
46 public class MicroServicePolicyTest {
47     @Rule
48     public ExpectedException thrown = ExpectedException.none();
49
50     @Test
51     public void testConstructor1() {
52         thrown.expect(NullPointerException.class);
53         MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy();
54         policy.getCorrectPolicyDataObject();
55         fail("Expected an exception");
56     }
57
58     @Test
59     public void testConstructor2() {
60         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
61         MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(policyAdapter);
62         assertNull(policy.getCorrectPolicyDataObject());
63     }
64
65     @PrepareForTest({MicroServiceConfigPolicy.class})
66     @Test
67     public void testPrepareToSave() throws Exception {
68         // Need to mock internal dictionary retrieval
69         CommonClassDaoImpl impl = Mockito.mock(CommonClassDaoImpl.class);
70         PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(impl);
71         when(impl.getDataById(any(), anyString(), anyString())).thenReturn(null);
72
73         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
74         MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(policyAdapter);
75         policyAdapter.setHighestVersion(1);
76         policyAdapter.setPolicyType("Config");
77         policyAdapter.setNewFileName("foo.xml");
78         policyAdapter.setJsonBody("{ \"version\": \"1.0\"}");
79         policyAdapter.setServiceType("foo");
80         policy.prepareToSave();
81         assertEquals(policy.isPreparedToSave(), true);
82     }
83
84     @Test
85     public void testCreateConstructor1() {
86         CreateNewMicroServiceModel model = new CreateNewMicroServiceModel(null, null, null, null);
87         assertNotNull(model);
88     }
89
90     @PrepareForTest({CreateNewMicroServiceModel.class})
91     @Test
92     public void testCreateModel() throws Exception {
93         // Mock file retrieval
94         File testFile = new File("testFile");
95         File[] testList = new File[1];
96         testList[0] = testFile;
97         File impl = Mockito.mock(File.class);
98         PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(impl);
99         when(impl.listFiles()).thenReturn(testList);
100         when(impl.isFile()).thenReturn(true);
101
102         // Mock internal dictionary retrieval
103         CommonClassDaoImpl daoImpl = Mockito.mock(CommonClassDaoImpl.class);
104         PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(daoImpl);
105         when(daoImpl.getDataById(any(), anyString(), anyString())).thenReturn(Collections.emptyList());
106
107         // Test create methods
108         String testFileName = "testFile.zip";
109         String testVal = "testVal";
110         CreateNewMicroServiceModel model =
111                 new CreateNewMicroServiceModel(testFileName, testVal, testVal, testVal, testVal);
112         model.addValuesToNewModel(".xmi");
113         model.saveImportService();
114     }
115 }