Skip to content

Commit

Permalink
Added REST Implementation with Privilege check for the Server logs en…
Browse files Browse the repository at this point in the history
…d point. (#329)

Added REST Implementation with Privilege check for the Server logs end point.

Added REST Implementation with Privilege check for the Server logs end point.

Added REST Implementation with Privilege check for the Server logs end point.

Added REST Implementation with Privilege check for the Server logs end point.

Added REST Implementation with Privilege check for the Server logs end point.

Added REST Implementation with Privilege check for the Server logs end point.

Added REST Implementation with Privilege check for Server Logs end point.

Added REST Implementation for Server logs

Privilege check added for the REST end point

updated the privilege check

Comments updated

Changed support class for the resource

Minor fix in REST Constants

Added more improvements to get it in the Swagger Docs

Added some testcases to serverLogController

Added some unit tests to increase the coverage

Added some unit tests

Fix some minor issues

Fixed minor formatting issues

Fixed minor issues

Added some tests

Added some tests

Added some tests

Added some tests for serverLog resource

Added some tests for serverLog resource

Added some tests for serverLog resource
  • Loading branch information
suthagar23 authored and dkayiwa committed May 29, 2018
1 parent d76b294 commit c5f1730
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* 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/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.MapProperty;
import org.openmrs.module.webservices.helper.ServerLogActionWrapper;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.Listable;
import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.response.ResponseException;

/**
* {@link Resource} for ServerLogController, supporting standard CRUD operations
*/
@Resource(name = RestConstants.VERSION_1 + "/serverlog", supportedClass = ServerLogActionWrapper.class, supportedOpenmrsVersions = {
"1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*" })
public class ServerLogResource1_8 extends BaseDelegatingResource<ServerLogActionWrapper> implements Listable {

private ServerLogActionWrapper serverLogActionWrapper = new ServerLogActionWrapper();

public void setServerLogActionWrapper(ServerLogActionWrapper serverLogActionWrapper) {
this.serverLogActionWrapper = serverLogActionWrapper;
}

@Override
public SimpleObject getAll(RequestContext context) throws ResponseException {
SimpleObject rest = new SimpleObject();
rest.put("serverLog", serverLogActionWrapper.getServerLogs());
return rest;
}

@Override
public Model getGETModel(Representation rep) {
return ((ModelImpl) super.getGETModel(rep))
.property("serverLog", new MapProperty());
}

@Override
public ServerLogActionWrapper getByUniqueId(String uniqueId) {
throw new UnsupportedOperationException("Serverlog doesn't support to this action");
}

@Override
protected void delete(ServerLogActionWrapper delegate, String reason, RequestContext context) throws ResponseException {
throw new UnsupportedOperationException("Serverlog doesn't support to this action");
}

@Override
public ServerLogActionWrapper newDelegate() {
return new ServerLogActionWrapper();
}

@Override
public ServerLogActionWrapper save(ServerLogActionWrapper delegate) {
throw new UnsupportedOperationException("Serverlog doesn't support to this action");
}

@Override
public void purge(ServerLogActionWrapper delegate, RequestContext context) throws ResponseException {
throw new UnsupportedOperationException("Serverlog doesn't support to this action");
}

@Override
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("serverLog", "serverLog");
return description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web;

import org.openmrs.module.webservices.helper.ServerLogActionWrapper;
import java.util.ArrayList;
import java.util.List;

/**
* MockServerLogActionWrapper used to run the Unit tests for ServerLogResource
*/
public class MockServerLogActionWrapper extends ServerLogActionWrapper {

public List<String> mockMemoryAppenderBuffer = new ArrayList<String>();

private ServerLogActionWrapper serverLogActionWrapper;

/**
* Override method from ServerLogActionWrapper to get the logs from mockMemoryAppender
*
* @return List of log lines
*/
@Override
public List<String[]> getServerLogs() {
List<String> logLines = mockMemoryAppenderBuffer;
List<String[]> finalOutput = new ArrayList<String[]>();
serverLogActionWrapper = new ServerLogActionWrapper();
for (String logLine : logLines) {
String[] logElements = serverLogActionWrapper.logLinePatternMatcher(logLine);
finalOutput.add(logElements);
}
return finalOutput;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* 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/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openmrs.api.APIException;
import org.openmrs.module.webservices.helper.ServerLogActionWrapper;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.MockServerLogActionWrapper;
import org.openmrs.module.webservices.rest.web.api.RestService;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.ServerLogResource1_8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ServerLogController1_8Test extends MainResourceControllerTest {

@Autowired
RestService restService;

private String log1 = "INFO - Simple.appender(115) |2018-03-03 15:44:54,834| Info Message";

private String log2 = "ERROR - Simple.appender(115) |2018-03-03 15:44:54,834| Info Message";

private MockServerLogActionWrapper mockServerLogActionWrapper = new MockServerLogActionWrapper();

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() {
ServerLogResource1_8 serverLogResource1_8 = (ServerLogResource1_8) restService
.getResourceBySupportedClass(ServerLogActionWrapper.class);
serverLogResource1_8.setServerLogActionWrapper(mockServerLogActionWrapper);
}

@Test(expected = Exception.class)
public void save_shouldFailOnSave() throws Exception {
MockHttpServletRequest req = request(RequestMethod.POST, getURI());
SimpleObject resultLogs = deserialize(handle(req));
}

@Test(expected = Exception.class)
public void delete_shouldFailOnDelete() throws Exception {
MockHttpServletRequest req = request(RequestMethod.DELETE, getURI());
SimpleObject resultLogs = deserialize(handle(req));
}

@Test
@Override
public void shouldGetAll() throws Exception {
//sanity check
List<String[]> mockServerLogs = mockServerLogActionWrapper.getServerLogs();
Assert.assertEquals(mockServerLogs.size(), 0);

mockServerLogActionWrapper.mockMemoryAppenderBuffer.addAll(Arrays.asList(log1, log2));

SimpleObject response = deserialize(handle(newGetRequest(getURI())));
ArrayList<String[]> results = response.get("serverLog");
Assert.assertNotNull(results);
Assert.assertEquals(results.size(), getAllCount());

Assert.assertEquals(mockServerLogActionWrapper.getServerLogs().size(), getAllCount());
}

@Test(expected = Exception.class)
@Override
public void shouldGetFullByUuid() throws Exception {
MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
req.addParameter("v", "full");
handle(req);
}

@Test(expected = Exception.class)
@Override
public void shouldGetDefaultByUuid() throws Exception {
MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
req.addParameter("v", "full");
handle(req);
}

@Test(expected = Exception.class)
@Override
public void shouldGetRefByUuid() throws Exception {
MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid());
req.addParameter("v", "ref");
handle(req);
}

@Override
public String getURI() {
return "serverlog";
}

@Override
public String getUuid() {
return "log1";
}

@Override
public long getAllCount() {
return mockServerLogActionWrapper.mockMemoryAppenderBuffer.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* 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/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;

import org.apache.struts.mock.MockHttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.openmrs.module.webservices.helper.ServerLogActionWrapper;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.MockServerLogActionWrapper;
import org.openmrs.module.webservices.rest.web.api.RestService;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController;
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import java.util.ArrayList;

/**
* Integration tests for the ServerLogResource class
*/
public class ServerLogResource1_8Test extends BaseModuleWebContextSensitiveTest {

@Autowired
RestService restService;

@Autowired
private MainResourceController mainResourceController;

private MockServerLogActionWrapper mockServerLogActionWrapper = new MockServerLogActionWrapper();

public String getURI() {
return "serverlog";
}

@Test
public void testGetAll() {
ServerLogResource1_8 serverLogResource = (ServerLogResource1_8) restService
.getResourceBySupportedClass(ServerLogActionWrapper.class);
serverLogResource.setServerLogActionWrapper(mockServerLogActionWrapper);

MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
MockHttpServletResponse response = new MockHttpServletResponse();
SimpleObject result = mainResourceController.get(getURI(), request, response);

ArrayList<String[]> serverLog = result.get("serverLog");
Assert.assertEquals(serverLog.size(), 0);

String mockLogLine1 = "INFO - Simple.appender(115) |2018-03-03 15:44:54,834| Info Message";
// Add some mock log lines to mockMemoryAppenderBuffer
mockServerLogActionWrapper.mockMemoryAppenderBuffer.add(mockLogLine1);
result = mainResourceController.get(getURI(), request, response);
serverLog = result.get("serverLog");
Assert.assertNotEquals(serverLog.size(), 0);

String[] logLine1 = serverLog.get(0);
Assert.assertNotEquals(logLine1[0], null);
Assert.assertNotEquals(logLine1[1], null);
Assert.assertNotEquals(logLine1[2], null);
Assert.assertNotEquals(logLine1[3], null);
}

@Test(expected = Exception.class)
public void shouldThrowExceptionWhenRequestGetDefaultByUuid() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
MockHttpServletResponse response = new MockHttpServletResponse();
SimpleObject result = mainResourceController.get(getURI() + "/" + getUuid(), request, response);

ArrayList<String[]> serverLog = result.get("serverLog");
}

public String getUuid() {
return "log1";
}
}
Loading

0 comments on commit c5f1730

Please sign in to comment.