Commit c7aca594 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '4-unit-testing' into 'main'

Resolve "Unit testing"

Closes #4

See merge request genesys-pgr/amphibian!11
parents b6a81842 5e8fb70a
......@@ -15,6 +15,9 @@
<log4j2.version>2.16.0</log4j2.version>
<jackson.version>2.11.4</jackson.version>
<spring.framework.version>5.2.12.RELEASE</spring.framework.version>
<junit.version>4.13.2</junit.version>
<hamcrest-library.version>2.2</hamcrest-library.version>
<json-path.version>2.6.0</json-path.version>
</properties>
<build>
......@@ -173,6 +176,31 @@
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest-library.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>${json-path.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
......
package org.genesys.amphibian.test.api;
import com.opencsv.CSVWriter;
import org.apache.logging.log4j.core.util.StringBuilderWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.genesys.amphibian.api.v0.PreviewApi;
import org.genesys.amphibian.model.Preview;
import org.genesys.amphibian.service.PreviewService;
import org.genesys.amphibian.test.base.AbstractApiTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class PreviewApiTest extends AbstractApiTest {
@Autowired
private PreviewService previewService;
@Test
public void ingestCsvTest() throws Exception {
UUID uuid = UUID.randomUUID();
StringBuilder content = new StringBuilder();
CSVWriter writer = new CSVWriter(new StringBuilderWriter(content));
//todo insert data
String[] row1 = new String[]{"INSTCODE", "ACCENUMB", "GENUS", "SEEDWGT"};
String[] row2 = new String[]{"SYR002", "IG 137552", "Lens", "0.8799999952316284"};
List<String[]> rows = new ArrayList<>();
rows.add(row1);
rows.add(row2);
writer.writeAll(rows);
MockMultipartFile file
= new MockMultipartFile(
"file",
"preview.csv",
"text/csv",
content.toString().getBytes()
);
/*@formatter:off*/
mockMvc
.perform(multipart(PreviewApi.CONTROLLER_URL.concat("/ingest/{uuid}"), uuid.toString())
.file(file))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", is(notNullValue())))
.andExpect(jsonPath("$.referenceUuid", is(uuid.toString())))
;
/*@formatter:on*/
//waiting for data inserting
Thread.sleep(500);
Preview preview = previewService.getPreview(uuid);
assertThat(preview, is(notNullValue()));
assertThat(preview.getSheets(), hasSize(1));
assertThat(preview.getSheets().get(0).rowCount, is(2));
}
@Test
public void ingestExelTest() throws Exception {
UUID uuid = UUID.randomUUID();
StringBuilder content = new StringBuilder();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("test");
XSSFRow row;
//todo insert data
Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[] {"INSTCODE", "ACCENUMB", "GENUS", "SEEDWGT"});
data.put("2", new Object[] {"SYR002", "IG 137552", "Lens", "0.8799999952316284"});
Set<String> keyid = data.keySet();
int rowid = 0;
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
Object[] objectArr = data.get(key);
int cellid = 0;
for (Object obj : objectArr) {
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
MockMultipartFile file
= new MockMultipartFile(
"file",
"preview.exel",
"application/vnd.ms-excel",
outputStream.toByteArray()
);
/*@formatter:off*/
mockMvc
.perform(multipart(PreviewApi.CONTROLLER_URL.concat("/ingest/{uuid}"), uuid.toString())
.file(file))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", is(notNullValue())))
.andExpect(jsonPath("$.referenceUuid", is(uuid.toString())))
;
/*@formatter:on*/
//waiting for data inserting
Thread.sleep(500);
Preview preview = previewService.getPreview(uuid);
assertThat(preview, is(notNullValue()));
assertThat(preview.getSheets(), hasSize(1));
assertThat(preview.getSheets().get(0).rowCount, is(2));
}
}
/*
* Copyright 2021 Global Crop Diversity Trust
*
* 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 org.genesys.amphibian.test.base;
import org.genesys.amphibian.repositories.mongo.DatasetTableRepository;
import org.genesys.amphibian.repositories.mongo.PreviewRepository;
import org.genesys.amphibian.repositories.mongo.TableRepository;
import org.genesys.amphibian.test.config.ApplicationTestConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy(@ContextConfiguration(name = "api", classes = { ApplicationTestConfig.class }))
public abstract class AbstractApiTest {
@Autowired
private WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;
@Autowired
private PreviewRepository previewRepository;
@Autowired
private DatasetTableRepository datasetTableRepository;
@Autowired
private TableRepository tableRepository;
@After
@Transactional
public void afterTest() {
previewRepository.deleteAll();
datasetTableRepository.deleteAll();
tableRepository.deleteAll();
}
@Before
@Transactional
public void beforeTest() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
}
/*
* Copyright 2021 Global Crop Diversity Trust
*
* 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 org.genesys.amphibian.test.config;
import org.genesys.amphibian.config.SchedulerConfig;
import org.genesys.amphibian.config.WebConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan(basePackages = { "org.genesys.amphibian.service" })
@Import({ DatabaseConfig.class, SchedulerConfig.class, WebConfiguration.class })
@EnableAspectJAutoProxy
public class ApplicationTestConfig {
}
/*
* Copyright 2018 Global Crop Diversity Trust
*
* 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 org.genesys.amphibian.test.config;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.genesys.amphibian.config.ApplicationConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import java.util.ArrayList;
import java.util.List;
@Configuration
@PropertySources({ @PropertySource("classpath:/junit.properties") })
@EnableMongoRepositories(basePackages = { "org.genesys.amphibian.repositories.mongo" })
@EnableMongoAuditing(modifyOnCreate = true)
public class DatabaseConfig extends AbstractMongoConfiguration {
final static Logger LOG = LoggerFactory.getLogger(ApplicationConfig.class);
@Value("${mongo.hosts}")
private String[] dbHosts;
@Value("${mongo.port}")
private Integer dbPort;
@Value("${mongo.database}")
private String dbDatabase;
@Value("${mongo.username}")
private String dbUsername;
@Value("${mongo.password}")
private String dbPassword;
@Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
@Override
protected String getDatabaseName() {
return dbDatabase;
}
@Override
public MongoClient mongoClient() {
MongoCredential credential = MongoCredential.createCredential(dbUsername, dbDatabase, dbPassword.toCharArray());
MongoClientOptions options = new MongoClientOptions.Builder().build();
List<ServerAddress> servers = new ArrayList<>();
for (String dbHost: dbHosts) {
LOG.warn("Adding mongo at {}:{}", dbHost, dbPort);
servers.add(new ServerAddress(dbHost, dbPort));
}
MongoClient mongoClient = new MongoClient(servers, credential, options);
return mongoClient;
}
}
#-------------------------------------------------------------------------------
# Copyright 2021 Global Crop Diversity Trust
#
# 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.
#-------------------------------------------------------------------------------
mongo.hosts=
mongo.port=
mongo.database=
mongo.username=
mongo.password=
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment