JUnit additions for PAP-REST
[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 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 package org.onap.policy.pap.xacml.rest.components;
21
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;
39 import java.io.File;
40 import java.util.Collections;
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({MicroServiceConfigPolicy.class, CreateNewMicroServiceModel.class})
44 public class MicroServicePolicyTest {
45         @Rule
46     public ExpectedException thrown = ExpectedException.none();
47
48         @Test
49         public void testConstructor1() {
50                 thrown.expect(NullPointerException.class);
51                 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy();
52                 policy.getCorrectPolicyDataObject();
53                 fail("Expected an exception");
54         }
55         
56         @Test
57         public void testConstructor2() {
58                 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
59                 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(policyAdapter);
60                 assertNull(policy.getCorrectPolicyDataObject());
61         }
62         
63         @Test
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);
69                 
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);
79         }
80         
81         @Test
82         public void testCreateConstructor1() {
83                 CreateNewMicroServiceModel model = new CreateNewMicroServiceModel(null, null, null, null);
84                 assertNotNull(model);
85         }
86         
87         @Test
88         public void testCreateModel() throws Exception {
89                 // Mock file retrieval
90                 File testFile = new File("testFile");
91                 File[] testList = new File[1];
92                 testList[0] = testFile;
93                 File impl = Mockito.mock(File.class);
94                 PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(impl);
95                 when(impl.listFiles()).thenReturn(testList);
96                 when(impl.isFile()).thenReturn(true);
97
98                 // Mock internal dictionary retrieval
99                 CommonClassDaoImpl daoImpl = Mockito.mock(CommonClassDaoImpl.class);
100                 PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(daoImpl);
101                 when(daoImpl.getDataById(any(), anyString(), anyString())).thenReturn(Collections.emptyList());
102
103                 // Test create methods
104                 String testFileName = "testFile.zip";
105                 String testVal = "testVal";
106                 CreateNewMicroServiceModel model = new CreateNewMicroServiceModel(testFileName, testVal, testVal, testVal, testVal);
107                 model.addValuesToNewModel();
108                 model.saveImportService();
109         }
110 }