4e7e80ab3a3bfa635be7fe1237a800433baaa9b9
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / editattributes / EditAttributesTest.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.onap.aai.sparky.editattributes;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 import javax.servlet.ReadListener;
32 import javax.servlet.ServletInputStream;
33
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37
38
39 /**
40  * The Class EditAttributesTest.
41  */
42 public class EditAttributesTest {
43   String sampleJsonRequest =
44       "{ \"entity-uri\" : \"some/uri/value/here\", \"entity-type\" : \"complex\","
45       + " \"attributes\" : { \"prov-status\" : \"PREPROV\", \"inMaint\" : \"true\","
46       + " \"isClosedLoop\" : \"false\" }}";
47
48   /**
49    * Sets the up before class.
50    *
51    * @throws Exception the exception
52    */
53   @BeforeClass
54   public static void setUpBeforeClass() throws Exception {}
55
56   /**
57    * Sets the up.
58    *
59    * @throws Exception the exception
60    */
61   @Before
62   public void setUp() throws Exception {}
63
64   
65   /**
66    * Test analyze edit request body.
67    */
68   /*
69   @Test
70   public void testAnalyzeEditRequestBody() {
71     AttributeEditProcessor aes = new AttributeEditProcessor();
72     EditRequest request = aes.analyzeEditRequestBody(sampleJsonRequest);
73     System.out.println("JSON Body : " + sampleJsonRequest);
74     assertNotNull(request);
75     assertEquals("URI should match", "some/uri/value/here", request.getEntityUri());
76     assertEquals("Entity Type should match", "complex", request.getEntityType());
77     assertEquals("Attribute ProvStatus should match", "PREPROV",
78         request.getAttributes().get("prov-status"));
79     assertEquals("Attribute inMaint should be true", "true",
80         request.getAttributes().get("inMaint"));
81     assertEquals("Attribute isClosedLoop should be false", "false",
82         request.getAttributes().get("isClosedLoop"));
83
84   }
85   */
86
87
88   /**
89    * Test edit request.
90    *
91    * @throws IOException Signals that an I/O exception has occurred.
92    * @throws ServletException the servlet exception
93    * @throws JSONException the JSON exception
94    */
95   /*
96   @Test
97   public void testEditRequest() throws IOException, ServletException, JSONException {
98     HttpServletRequest mockRequest = mock(HttpServletRequest.class);
99     HttpServletResponse mockResponse = mock(HttpServletResponse.class);
100     ServletOutputStream mockOutput = mock(ServletOutputStream.class);
101     ServletInputStream mockInput = new MockServletInputStream(sampleJsonRequest);
102
103     when(mockRequest.getRequestURI()).thenReturn("editAttributes");
104     when(mockResponse.getOutputStream()).thenReturn(mockOutput);
105
106     when(mockRequest.getInputStream()).thenReturn(mockInput);
107
108     Principal princip = new UserPrincipal("ds1150");
109
110     when(mockRequest.getUserPrincipal()).thenReturn(princip);
111
112     PrintWriter writer = new PrintWriter("editServletTest.txt");
113     when(mockResponse.getWriter()).thenReturn(writer);
114     AttributeEditProcessor aes = new AttributeEditProcessor();
115     aes.doPost(mockRequest, mockResponse);
116     JSONObject result = null;
117     try {
118       writer.close();
119       result = new JSONObject(FileUtils.readFileToString(new File("editServletTest.txt"), "UTF-8"));
120     } catch (JSONException ex) {
121       // Nothing to catch
122     }
123     assertNotNull(result);
124     // assertEquals("Attributes updated successfully (just need PATCH !!!)", result.get("result"));
125   }
126   */
127
128   
129
130   /**
131    * The Class MockServletInputStream.
132    */
133   class MockServletInputStream extends ServletInputStream {
134     InputStream inputStream;
135
136     /**
137      * Instantiates a new mock servlet input stream.
138      *
139      * @param string the string
140      */
141     MockServletInputStream(String string) {
142       this.inputStream = IOUtils.toInputStream(string);
143     }
144
145     /* (non-Javadoc)
146      * @see java.io.InputStream#read()
147      */
148     @Override
149     public int read() throws IOException {
150       return inputStream.read();
151     }
152
153     @Override
154     public boolean isFinished() {
155       // TODO Auto-generated method stub
156       return false;
157     }
158
159     @Override
160     public boolean isReady() {
161       // TODO Auto-generated method stub
162       return false;
163     }
164
165     @Override
166     public void setReadListener(ReadListener readListener) {
167       // TODO Auto-generated method stub
168
169     }
170   }
171 }