Adding UI extensibility
[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 static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.PrintWriter;
37 import java.security.Principal;
38
39 import javax.servlet.ReadListener;
40 import javax.servlet.ServletException;
41 import javax.servlet.ServletInputStream;
42 import javax.servlet.ServletOutputStream;
43 import javax.servlet.http.Cookie;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47 import org.apache.commons.io.FileUtils;
48 import org.apache.commons.io.IOUtils;
49 import org.apache.openejb.core.security.jaas.UserPrincipal;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.onap.aai.sparky.editattributes.AttributeEditProcessor;
54 import org.onap.aai.sparky.editattributes.entity.EditRequest;
55
56 import com.att.aft.dme2.internal.jettison.json.JSONException;
57 import com.att.aft.dme2.internal.jettison.json.JSONObject;
58
59 /**
60  * The Class EditAttributesTest.
61  */
62 public class EditAttributesTest {
63   String sampleJsonRequest =
64       "{ \"entity-uri\" : \"some/uri/value/here\", \"entity-type\" : \"complex\","
65           + " \"attributes\" : { \"prov-status\" : \"PREPROV\", \"inMaint\" : \"true\","
66           + " \"isClosedLoop\" : \"false\" }}";
67
68   /**
69    * Sets the up before class.
70    *
71    * @throws Exception the exception
72    */
73   @BeforeClass
74   public static void setUpBeforeClass() throws Exception {}
75
76   /**
77    * Sets the up.
78    *
79    * @throws Exception the exception
80    */
81   @Before
82   public void setUp() throws Exception {}
83
84
85   /**
86    * Test analyze edit request body.
87    */
88   /*
89    * @Test public void testAnalyzeEditRequestBody() { AttributeEditProcessor aes = new
90    * AttributeEditProcessor(); EditRequest request = aes.analyzeEditRequestBody(sampleJsonRequest);
91    * System.out.println("JSON Body : " + sampleJsonRequest); assertNotNull(request); assertEquals(
92    * "URI should match", "some/uri/value/here", request.getEntityUri()); assertEquals(
93    * "Entity Type should match", "complex", request.getEntityType()); assertEquals(
94    * "Attribute ProvStatus should match", "PREPROV", request.getAttributes().get("prov-status"));
95    * assertEquals("Attribute inMaint should be true", "true",
96    * request.getAttributes().get("inMaint")); assertEquals("Attribute isClosedLoop should be false",
97    * "false", request.getAttributes().get("isClosedLoop"));
98    * 
99    * }
100    */
101
102
103   /**
104    * Test edit request.
105    *
106    * @throws IOException Signals that an I/O exception has occurred.
107    * @throws ServletException the servlet exception
108    * @throws JSONException the JSON exception
109    */
110   /*
111    * @Test public void testEditRequest() throws IOException, ServletException, JSONException {
112    * HttpServletRequest mockRequest = mock(HttpServletRequest.class); HttpServletResponse
113    * mockResponse = mock(HttpServletResponse.class); ServletOutputStream mockOutput =
114    * mock(ServletOutputStream.class); ServletInputStream mockInput = new
115    * MockServletInputStream(sampleJsonRequest);
116    * 
117    * when(mockRequest.getRequestURI()).thenReturn("editAttributes");
118    * when(mockResponse.getOutputStream()).thenReturn(mockOutput);
119    * 
120    * when(mockRequest.getInputStream()).thenReturn(mockInput);
121    * 
122    * Principal princip = new UserPrincipal("ds1150");
123    * 
124    * when(mockRequest.getUserPrincipal()).thenReturn(princip);
125    * 
126    * PrintWriter writer = new PrintWriter("editServletTest.txt");
127    * when(mockResponse.getWriter()).thenReturn(writer); AttributeEditProcessor aes = new
128    * AttributeEditProcessor(); aes.doPost(mockRequest, mockResponse); JSONObject result = null; try
129    * { writer.close(); result = new JSONObject(FileUtils.readFileToString(new
130    * File("editServletTest.txt"), "UTF-8")); } catch (JSONException ex) { // Nothing to catch }
131    * assertNotNull(result); // assertEquals("Attributes updated successfully (just need PATCH !!!)",
132    * result.get("result")); }
133    */
134
135   /**
136    * Test get att uid.
137    *
138    * @throws IOException Signals that an I/O exception has occurred.
139    * @throws ServletException the servlet exception
140    */
141   /*
142    * @Test public void testGetAttUid() throws IOException, ServletException { HttpServletRequest
143    * mockRequest = mock(HttpServletRequest.class); HttpServletResponse mockResponse =
144    * mock(HttpServletResponse.class); ServletOutputStream mockOutput =
145    * mock(ServletOutputStream.class); ServletInputStream mockInput = new
146    * MockServletInputStream(sampleJsonRequest);
147    * 
148    * when(mockRequest.getRequestURI()).thenReturn("editAttributes");
149    * when(mockResponse.getOutputStream()).thenReturn(mockOutput);
150    * 
151    * when(mockRequest.getInputStream()).thenReturn(mockInput);
152    * 
153    * Principal princip = new UserPrincipal("ds1150");
154    * 
155    * when(mockRequest.getUserPrincipal()).thenReturn(princip);
156    * 
157    * PrintWriter writer = new PrintWriter("editServletTest.txt");
158    * when(mockResponse.getWriter()).thenReturn(writer);
159    * when(mockRequest.getCookies()).thenReturn(new Cookie[] {new Cookie("attESHr",
160    * "DENNIS|SEBASTIAN|dennis.sebastian@amdocs.com|||ko2649||ds1150," +
161    * "RBFMSKQ,Z9V2298,9762186|YNNNNNNNNNNNNNYNNYYNNNNN|DENNIS|EY6SC9000|")}); AttributeEditProcessor
162    * aes = new AttributeEditProcessor(); String attid = aes.getAttUid(mockRequest); assertEquals(
163    * " Expected ATTUID is wrong", "ds1150", attid);
164    * 
165    * when(mockRequest.getCookies()).thenReturn(new Cookie[] {new Cookie("attESHr",
166    * "DENNIS%7cSEBASTIAN%7cdennisse%40amdocs%2ecom%7c%7c%7cko2649%7c%7cds1150%2cRDJJFLM%" +
167    * "2cP86NJ85%2c8127688%7cYNNNNNNNNNNNNNYNNYNYNNNN%7cDENNIS%7cEY6SC9000%7c")}); attid =
168    * aes.getAttUid(mockRequest); assertEquals(" Expected ATTUID is wrong", "ds1150", attid);
169    * 
170    * when(mockRequest.getCookies()).thenReturn(new Cookie[] {}); attid = aes.getAttUid(mockRequest);
171    * assertEquals(" Expected Empty ID", "", attid);
172    * 
173    * }
174    */
175
176   /**
177    * The Class MockServletInputStream.
178    */
179   class MockServletInputStream extends ServletInputStream {
180     InputStream inputStream;
181
182     /**
183      * Instantiates a new mock servlet input stream.
184      *
185      * @param string the string
186      */
187     MockServletInputStream(String string) {
188       this.inputStream = IOUtils.toInputStream(string);
189     }
190
191     /*
192      * (non-Javadoc)
193      * 
194      * @see java.io.InputStream#read()
195      */
196     @Override
197     public int read() throws IOException {
198       return inputStream.read();
199     }
200
201     @Override
202     public boolean isFinished() {
203       // TODO Auto-generated method stub
204       return false;
205     }
206
207     @Override
208     public boolean isReady() {
209       // TODO Auto-generated method stub
210       return false;
211     }
212
213     @Override
214     public void setReadListener(ReadListener readListener) {
215       // TODO Auto-generated method stub
216
217     }
218   }
219 }