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 java.io.IOException;
29 import java.io.InputStream;
31 import javax.servlet.ReadListener;
32 import javax.servlet.ServletInputStream;
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
40 * The Class EditAttributesTest.
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\" }}";
49 * Sets the up before class.
51 * @throws Exception the exception
54 public static void setUpBeforeClass() throws Exception {}
59 * @throws Exception the exception
62 public void setUp() throws Exception {}
66 * Test analyze edit request body.
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"));
91 * @throws IOException Signals that an I/O exception has occurred.
92 * @throws ServletException the servlet exception
93 * @throws JSONException the JSON exception
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);
103 when(mockRequest.getRequestURI()).thenReturn("editAttributes");
104 when(mockResponse.getOutputStream()).thenReturn(mockOutput);
106 when(mockRequest.getInputStream()).thenReturn(mockInput);
108 Principal princip = new UserPrincipal("ds1150");
110 when(mockRequest.getUserPrincipal()).thenReturn(princip);
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;
119 result = new JSONObject(FileUtils.readFileToString(new File("editServletTest.txt"), "UTF-8"));
120 } catch (JSONException ex) {
123 assertNotNull(result);
124 // assertEquals("Attributes updated successfully (just need PATCH !!!)", result.get("result"));
131 * The Class MockServletInputStream.
133 class MockServletInputStream extends ServletInputStream {
134 InputStream inputStream;
137 * Instantiates a new mock servlet input stream.
139 * @param string the string
141 MockServletInputStream(String string) {
142 this.inputStream = IOUtils.toInputStream(string);
146 * @see java.io.InputStream#read()
149 public int read() throws IOException {
150 return inputStream.read();
154 public boolean isFinished() {
155 // TODO Auto-generated method stub
160 public boolean isReady() {
161 // TODO Auto-generated method stub
166 public void setReadListener(ReadListener readListener) {
167 // TODO Auto-generated method stub