Skip to content

Commit

Permalink
Fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
quinarygio committed Jul 3, 2017
1 parent fe5066c commit 9a05526
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
@Path("/")
public class SecurityAnalysisApi {
private final SecurityAnalysisService delegate = SecurityAnalysisServiceFactory.getSecurityServiceApi();

@POST
@Path("/security-analysis")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response process(MultipartFormDataInput form, @PathParam("format") String format, @Context SecurityContext securityContext) {
return delegate.analyze(form);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Giovanni Ferrari <giovanni.ferrari@techrain.it>
*/
public interface SecurityAnalysisService {

public Response analyze(MultipartFormDataInput dataInput);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import eu.itesla_project.security.rest.api.SecurityAnalysisService;
import eu.itesla_project.security.rest.api.impl.SecurityAnalysisServiceImpl;


/**
*
* @author Giovanni Ferrari <giovanni.ferrari@techrain.it>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SecurityAnalysisServiceImpl implements SecurityAnalysisService {
private enum Format {
CSV, JSON
}

private final SecurityAnalyzer analyzer;

public SecurityAnalysisServiceImpl(SecurityAnalyzer securityAnalyzer) {
Expand All @@ -79,8 +79,6 @@ public Response analyze(MultipartFormDataInput form) {
String out_format = getParameter(formatParts);
if(out_format != null)
format = Format.valueOf(out_format);

LOGGER.debug("format=" + format);
} catch (java.lang.IllegalArgumentException ie) {
return Response.status(Status.BAD_REQUEST).entity("Wrong format parameter").build();
}
Expand All @@ -92,7 +90,6 @@ public Response analyze(MultipartFormDataInput form) {
List<InputPart> limitParts = formParts.get("limit-types");
if (limitParts != null) {
limitTypes = getParameter(limitParts);
LOGGER.debug("limit-types=" + limitTypes);
}

Set<LimitViolationType> limitViolationTypes = null;
Expand Down Expand Up @@ -157,9 +154,8 @@ public void write(OutputStream out) throws IOException, WebApplicationException
}
}
};

}

private String getParameter(List<InputPart> parts){
Objects.requireNonNull(parts);
try {
Expand Down
7 changes: 6 additions & 1 deletion security-analysis-ws/src/main/webapp/analysis.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<!--
Copyright (c) 2016, RTE (http://www.rte-france.com) This Source Code
Form is subject to the terms of the Mozilla Public License, v. 2.0. If a
copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
-->
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -26,7 +32,6 @@ <h2>Security Analysis</h2>
<td><select name="format">
<option value="JSON">JSON</option>
<option value="CSV">CSV</option>
<option value="ERROR">ERROR</option>
</select></td>
</tr>
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,25 @@
import eu.itesla_project.security.rest.api.impl.SecurityAnalysisServiceImpl;

public class SecurityWsTest {

private SecurityAnalysisServiceImpl service;

@Before
public void setUp(){
SecurityAnalysisResult result = new SecurityAnalysisResult(
new LimitViolationsResult(true, Collections.emptyList()), Collections.emptyList());

new LimitViolationsResult(true, Collections.emptyList()), Collections.emptyList());
SecurityAnalyzer analyzer = Mockito.mock(SecurityAnalyzer.class);
when(analyzer.analyze(any(Path.class), any(Path.class))).thenReturn(result);

service = new SecurityAnalysisServiceImpl(analyzer);
}

@Test
public void testSecurityAnalysisJson() {
MultipartFormDataInput dataInput = mock(MultipartFormDataInput.class);
Map<String, List<InputPart>> formValues = new HashMap();
formValues.put("format", Arrays.asList(new InputPartImpl("JSON", MediaType.TEXT_PLAIN_TYPE)));
formValues.put("limit-types", Arrays.asList(new InputPartImpl("CURRENT", MediaType.TEXT_PLAIN_TYPE)));

MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
headers.putSingle("Content-Disposition", "filename=" + "case-file.xiidm.gz");

Expand All @@ -71,7 +69,7 @@ public void testSecurityAnalysisJson() {
Assert.assertEquals(200, res.getStatus());

}

@Test
public void testSecurityAnalysisCsv() {
MultipartFormDataInput dataInput = mock(MultipartFormDataInput.class);
Expand All @@ -97,7 +95,7 @@ public void testSecurityAnalysisCsv() {
Response res = service.analyze(dataInput);
Assert.assertEquals(200, res.getStatus());
}

@Test
public void testWrongFormat() {
MultipartFormDataInput dataInput = mock(MultipartFormDataInput.class);
Expand Down Expand Up @@ -156,7 +154,6 @@ public void testMissingCaseFile() {
MultipartFormDataInput dataInput = mock(MultipartFormDataInput.class);
Map<String, List<InputPart>> formValues = new HashMap();
formValues.put("format", Arrays.asList(new InputPartImpl("JSON", MediaType.TEXT_PLAIN_TYPE)));

formValues.put("limit-types", Arrays.asList(new InputPartImpl("CURRENT", MediaType.TEXT_PLAIN_TYPE)));

MultivaluedMap<String, String> headers2 = new MultivaluedMapImpl();
Expand All @@ -177,7 +174,6 @@ public void testWrongLimits() {
MultipartFormDataInput dataInput = mock(MultipartFormDataInput.class);
Map<String, List<InputPart>> formValues = new HashMap();
formValues.put("format", Arrays.asList(new InputPartImpl("JSON", MediaType.TEXT_PLAIN_TYPE)));

formValues.put("limit-types", Arrays.asList(new InputPartImpl("ERRR", MediaType.TEXT_PLAIN_TYPE)));

MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
Expand All @@ -187,7 +183,7 @@ public void testWrongLimits() {
Arrays.asList(new InputPartImpl(
new ByteArrayInputStream("Network".getBytes()),
MediaType.APPLICATION_OCTET_STREAM_TYPE, headers)));

MultivaluedMap<String, String> headers2 = new MultivaluedMapImpl();
headers2.putSingle("Content-Disposition", "filename=" + "contingencies-file.csv");
formValues.put("contingencies-file",
Expand Down

0 comments on commit 9a05526

Please sign in to comment.