Add unit tests to increase the sonar coverage
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / service / ModelLoaderServiceTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *     http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * ============LICENSE_END=========================================================\r
21  *\r
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
23  */\r
24 package org.onap.aai.modelloader.service;\r
25 \r
26 import org.junit.Assert;\r
27 import org.junit.Before;\r
28 import org.junit.Test;\r
29 import org.mockito.Mockito;\r
30 \r
31 import javax.servlet.http.HttpServletRequest;\r
32 import javax.ws.rs.core.Response;\r
33 import java.io.File;\r
34 import java.io.IOException;\r
35 import java.lang.reflect.Field;\r
36 import java.lang.reflect.Modifier;\r
37 \r
38 public class ModelLoaderServiceTest {\r
39 \r
40     ModelLoaderService service;\r
41     @Before\r
42     public void init() throws IOException, NoSuchFieldException, IllegalAccessException {\r
43         System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
44         setFinalStatic(System.getProperty("AJSC_HOME")+"/src/test/resources/model-loader.properties");\r
45         service = new ModelLoaderService();\r
46     }\r
47 \r
48     @Test\r
49     public void testLoadModel(){\r
50         Response response = service.loadModel("model-1");\r
51         Assert.assertNotNull(response);\r
52     }\r
53 \r
54     @Test\r
55     public void testSaveModel(){\r
56         Response response = service.saveModel("model-1", "name-1");\r
57         Assert.assertNotNull(response);\r
58     }\r
59 \r
60     @Test\r
61     public void testIngestModel() throws IOException {\r
62         HttpServletRequest req = Mockito.mock(HttpServletRequest.class);\r
63         Response response = service.ingestModel("model-id-1", req, "payload");\r
64         Assert.assertNotNull(response);\r
65     }\r
66 \r
67     static void setFinalStatic(String fieldValue) throws NoSuchFieldException, SecurityException,\r
68             IllegalArgumentException, IllegalAccessException {\r
69         Field configField = ModelLoaderService.class.getDeclaredField("CONFIG_FILE");\r
70         configField.setAccessible(true);\r
71 \r
72         Field modifiersField = Field.class.getDeclaredField( "modifiers" );\r
73         modifiersField.setAccessible( true );\r
74         modifiersField.setInt( configField, configField.getModifiers() & ~Modifier.FINAL );\r
75 \r
76         configField.set(null, fieldValue);\r
77 \r
78         Field authField = ModelLoaderService.class.getDeclaredField("CONFIG_AUTH_LOCATION");\r
79         authField.setAccessible(true);\r
80 \r
81         Field modifiersField1 = Field.class.getDeclaredField( "modifiers" );\r
82         modifiersField1.setAccessible( true );\r
83         modifiersField1.setInt( authField, authField.getModifiers() & ~Modifier.FINAL );\r
84 \r
85         authField.set(null, System.getProperty("AJSC_HOME"));\r
86     }\r
87 }\r