use of com.openmeap.model.event.handler.ModelServiceRefreshHandler in project OpenMEAP by OpenMEAP.
the class ServletManagementServletTest method testRefreshApplication.
@Test
public void testRefreshApplication() throws Exception {
MockHttpServletRequest request = new Request();
MockHttpServletResponse response = new MockHttpServletResponse();
String randomUuid = UUID.randomUUID().toString();
GlobalSettings settings = modelManager.getGlobalSettings();
/////////////////
// validate that finding the application, modifying it, and then finding it again
// will return an object with the same modifications.
Application app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
app.setName(randomUuid);
Assert.assertTrue(modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
modelManager.refresh(app, null);
app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
Assert.assertTrue(!modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
ServiceManagementServlet servlet = new ServiceManagementServlet();
servlet.setModelManager(modelManager);
servlet.setModelServiceRefreshHandler(new ModelServiceRefreshHandler());
servlet.getModelServiceRefreshHandler().setModelManager(modelManager);
////////////////////
// validate the happy path of providing all the required information
String authSalt = servlet.getAuthSalt();
String authToken = AuthTokenProvider.newAuthToken(authSalt);
request.setParameter(UrlParamConstants.REFRESH_TYPE, "Application");
request.setParameter(UrlParamConstants.REFRESH_OBJ_PKID, "1");
request.setParameter(UrlParamConstants.AUTH_TOKEN, authToken);
request.setParameter(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
servlet.service(request, response);
String contentString = response.getContentAsString();
JSONObjectBuilder job = new JSONObjectBuilder();
Result result = (Result) job.fromJSON(new JSONObject(contentString), new Result());
Assert.assertTrue(result.getStatus().equals(Result.Status.SUCCESS));
Assert.assertTrue(!modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
////////////////////
// validate that failing to provide auth token fails to refresh cache
app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
app.setName(randomUuid);
response = new MockHttpServletResponse();
request.removeParameter(UrlParamConstants.AUTH_TOKEN);
request.setParameter(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
request.setParameter(UrlParamConstants.REFRESH_TYPE, "Application");
request.setParameter(UrlParamConstants.REFRESH_OBJ_PKID, "1");
servlet.service(request, response);
contentString = response.getContentAsString();
result = (Result) job.fromJSON(new JSONObject(contentString), new Result());
Assert.assertTrue(result.getStatus().equals(Result.Status.FAILURE));
Assert.assertTrue(modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
}
Aggregations