Skip to content
Snippets Groups Projects
Commit 6cfef65d authored by felmer's avatar felmer
Browse files

LMS-1717 headless system test

SVN: 18357
parent 56012bc6
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2010 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.openbis.systemtest;
import static org.testng.AssertJUnit.assertEquals;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.mock.web.MockMultipartFile;
import org.testng.annotations.Test;
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.BatchRegistrationResult;
import ch.systemsx.cisd.openbis.generic.client.web.client.exception.UserFailureException;
import ch.systemsx.cisd.openbis.generic.client.web.server.UploadedFilesBean;
import ch.systemsx.cisd.openbis.generic.shared.basic.IEntityInformationHolderWithPermId;
import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IEntityProperty;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Material;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialIdentifier;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialType;
/**
*
*
* @author Franz-Josef Elmer
*/
public class BatchMaterialRegistrationAndUpdate extends SystemTestCase
{
private static final String MATERIAL_TYPE = "CONTROL";
private static final String SESSION_KEY = "session-key";
@Test
public void testRegistration()
{
logIntoCommonClientService();
String materialBatchData = "code\tdescription\tsize\nc1\tcompound 1\t42\nc2\tcompound 2\t43";
List<BatchRegistrationResult> result = registerMaterials(materialBatchData);
assertEquals("2 material(s) found and registered.", result.get(0).getMessage());
assertEquals(1, result.size());
assertProperties("[DESCRIPTION: compound 1, SIZE: 42]", "C1");
assertProperties("[DESCRIPTION: compound 2, SIZE: 43]", "C2");
}
@Test
public void testUpdate()
{
logIntoCommonClientService();
if (getMaterialOrNull("C1") == null)
{
String materialBatchData =
"code\tdescription\tsize\n" + "c1\tcompound 1\t42\n" + "c2\tcompound 2\t43";
registerMaterials(materialBatchData);
}
List<BatchRegistrationResult> result =
updateMaterials("code\tdescription\n" + "c1\tnew description\n" + "c2\t--DELETE--",
false);
assertEquals("2 material(s) updated.", result.get(0).getMessage());
assertEquals(1, result.size());
assertProperties("[DESCRIPTION: new description, SIZE: 42]", "C1");
assertProperties("[SIZE: 43]", "C2");
}
private void assertProperties(String expectedProperties, String materialCode)
{
assertEquals(expectedProperties, getMaterialOrNull(materialCode).getProperties().toString());
}
private Material getMaterialOrNull(String code)
{
try
{
IEntityInformationHolderWithPermId m =
commonClientService.getMaterialInformationHolder(new MaterialIdentifier(code,
MATERIAL_TYPE));
Material materialInfo = genericClientService.getMaterialInfo(new TechId(m.getId()));
assertEquals(code, materialInfo.getCode());
Collections.sort(materialInfo.getProperties(), new Comparator<IEntityProperty>()
{
public int compare(IEntityProperty p1, IEntityProperty p2)
{
return p1.getPropertyType().getCode().compareTo(p2.getPropertyType().getCode());
}
});
return materialInfo;
} catch (UserFailureException ex)
{
return null;
}
}
private List<BatchRegistrationResult> registerMaterials(String materialBatchData)
{
uploadFile(materialBatchData);
MaterialType materialType = new MaterialType();
materialType.setCode(MATERIAL_TYPE);
return genericClientService.registerMaterials(materialType, SESSION_KEY);
}
private List<BatchRegistrationResult> updateMaterials(String materialBatchData, boolean ignoreUnregistered)
{
uploadFile(materialBatchData);
MaterialType materialType = new MaterialType();
materialType.setCode(MATERIAL_TYPE);
return genericClientService.updateMaterials(materialType, SESSION_KEY, ignoreUnregistered);
}
private void uploadFile(String fileContent)
{
UploadedFilesBean bean = new UploadedFilesBean();
bean.addMultipartFile(new MockMultipartFile("my-file", fileContent.getBytes()));
HttpSession session = request.getSession();
session.setAttribute(SESSION_KEY, bean);
}
}
...@@ -29,6 +29,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SessionContext; ...@@ -29,6 +29,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SessionContext;
import ch.systemsx.cisd.openbis.generic.server.ICommonServerForInternalUse; import ch.systemsx.cisd.openbis.generic.server.ICommonServerForInternalUse;
import ch.systemsx.cisd.openbis.generic.server.util.TestInitializer; import ch.systemsx.cisd.openbis.generic.server.util.TestInitializer;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DisplaySettings; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DisplaySettings;
import ch.systemsx.cisd.openbis.plugin.generic.client.web.client.IGenericClientService;
/** /**
* @author Franz-Josef Elmer * @author Franz-Josef Elmer
...@@ -39,6 +40,8 @@ public abstract class SystemTestCase extends AbstractTestNGSpringContextTests ...@@ -39,6 +40,8 @@ public abstract class SystemTestCase extends AbstractTestNGSpringContextTests
protected ICommonServerForInternalUse commonServer; protected ICommonServerForInternalUse commonServer;
protected ICommonClientService commonClientService; protected ICommonClientService commonClientService;
protected IGenericClientService genericClientService;
protected MockHttpServletRequest request; protected MockHttpServletRequest request;
...@@ -82,6 +85,18 @@ public abstract class SystemTestCase extends AbstractTestNGSpringContextTests ...@@ -82,6 +85,18 @@ public abstract class SystemTestCase extends AbstractTestNGSpringContextTests
this.commonClientService = commonClientService; this.commonClientService = commonClientService;
} }
/**
* Sets <code>genericClientService</code>.
* <p>
* Will be automatically dependency injected by type.
* </p>
*/
@Autowired
public final void setGenericClientService(final IGenericClientService genericClientService)
{
this.genericClientService = genericClientService;
}
protected SessionContext logIntoCommonClientService() protected SessionContext logIntoCommonClientService()
{ {
SessionContext context = commonClientService.tryToLogin("test", "a"); SessionContext context = commonClientService.tryToLogin("test", "a");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment