Merge "Upgrade dmaap client with security fix"
[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.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;
38
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest(MicroServiceConfigPolicy.class)
41 public class MicroServicePolicyTest {
42         @Rule
43     public ExpectedException thrown = ExpectedException.none();
44
45         @Test
46         public void testConstructor1() {
47                 thrown.expect(NullPointerException.class);
48                 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy();
49                 policy.getCorrectPolicyDataObject();
50                 fail("Expected an exception");
51         }
52         
53         @Test
54         public void testConstructor2() {
55                 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
56                 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(policyAdapter);
57                 assertNull(policy.getCorrectPolicyDataObject());
58         }
59         
60         @Test
61         public void testPrepareToSave() throws Exception {
62                 // Need to mock internal dictionary retrieval
63                 CommonClassDaoImpl impl = Mockito.mock(CommonClassDaoImpl.class);
64                 PowerMockito.whenNew(CommonClassDaoImpl.class).withNoArguments().thenReturn(impl);
65                 when(impl.getDataById(any(), anyString(), anyString())).thenReturn(null);
66                 
67                 PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
68                 MicroServiceConfigPolicy policy = new MicroServiceConfigPolicy(policyAdapter);
69                 policyAdapter.setHighestVersion(1);
70                 policyAdapter.setPolicyType("Config");
71                 policyAdapter.setNewFileName("foo.xml");
72                 policyAdapter.setJsonBody("{ \"version\": \"1.0\"}");
73                 policyAdapter.setServiceType("foo");
74                 policy.prepareToSave();
75                 assertEquals(policy.isPreparedToSave(), true);
76         }
77 }