2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.editattributes;
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;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.PrintWriter;
37 import java.security.Principal;
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;
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;
56 import com.att.aft.dme2.internal.jettison.json.JSONException;
57 import com.att.aft.dme2.internal.jettison.json.JSONObject;
60 * The Class EditAttributesTest.
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\" }}";
69 * Sets the up before class.
71 * @throws Exception the exception
74 public static void setUpBeforeClass() throws Exception {}
79 * @throws Exception the exception
82 public void setUp() throws Exception {}
86 * Test analyze edit request body.
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"));
106 * @throws IOException Signals that an I/O exception has occurred.
107 * @throws ServletException the servlet exception
108 * @throws JSONException the JSON exception
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);
117 * when(mockRequest.getRequestURI()).thenReturn("editAttributes");
118 * when(mockResponse.getOutputStream()).thenReturn(mockOutput);
120 * when(mockRequest.getInputStream()).thenReturn(mockInput);
122 * Principal princip = new UserPrincipal("ds1150");
124 * when(mockRequest.getUserPrincipal()).thenReturn(princip);
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")); }
138 * @throws IOException Signals that an I/O exception has occurred.
139 * @throws ServletException the servlet exception
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);
148 * when(mockRequest.getRequestURI()).thenReturn("editAttributes");
149 * when(mockResponse.getOutputStream()).thenReturn(mockOutput);
151 * when(mockRequest.getInputStream()).thenReturn(mockInput);
153 * Principal princip = new UserPrincipal("ds1150");
155 * when(mockRequest.getUserPrincipal()).thenReturn(princip);
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);
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);
170 * when(mockRequest.getCookies()).thenReturn(new Cookie[] {}); attid = aes.getAttUid(mockRequest);
171 * assertEquals(" Expected Empty ID", "", attid);
177 * The Class MockServletInputStream.
179 class MockServletInputStream extends ServletInputStream {
180 InputStream inputStream;
183 * Instantiates a new mock servlet input stream.
185 * @param string the string
187 MockServletInputStream(String string) {
188 this.inputStream = IOUtils.toInputStream(string);
194 * @see java.io.InputStream#read()
197 public int read() throws IOException {
198 return inputStream.read();
202 public boolean isFinished() {
203 // TODO Auto-generated method stub
208 public boolean isReady() {
209 // TODO Auto-generated method stub
214 public void setReadListener(ReadListener readListener) {
215 // TODO Auto-generated method stub