Fixed tests, context issues, path info(/error) and embedded datasource issues

parent 6eed3e43
......@@ -36,11 +36,14 @@ configurations {
dependencies {
compile(
"org.springframework.boot:spring-boot-starter-tomcat",
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-data-jpa",
"com.h2database:h2"
)
// providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile(
"junit:junit",
"org.springframework:spring-test"
......
......@@ -4,6 +4,8 @@ import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Random;
......@@ -16,7 +18,11 @@ import java.util.Set;
*/
public class InitializeEmbeddedDataBaseData {
private static final Logger logger = LoggerFactory.getLogger(InitializeEmbeddedDataBaseData.class);
public InitializeEmbeddedDataBaseData(SurveyRepository surveyRepository, ParticipantRepository participantRepository) {
logger.info("Creating entities and populating H2 embedded datasource");
Survey surveyOne = new Survey();
Survey surveyTwo = new Survey();
surveyOne = surveyRepository.save(surveyOne);
......@@ -38,13 +44,8 @@ public class InitializeEmbeddedDataBaseData {
}
surveyTwo.setParticipants(participantsTwo);
System.out.println(surveyOne);
for(Survey s: surveyRepository.findAll()) {
System.out.println(s);
}
logger.info("Size participantsOne: " + participantsOne.size());
logger.info("Size participantsTwo: " + participantsTwo.size());
}
/**
......
......@@ -2,8 +2,10 @@ package no.nsd.websurvey.tromso;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
......@@ -12,15 +14,34 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
* Primary configuration class, and also the entry point for local
* embedded development.
*/
import javax.annotation.PostConstruct;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class WebSurveyTromso extends SpringBootServletInitializer {
@Autowired
private SurveyRepository surveyRepository;
@Autowired
private ParticipantRepository participantRepository;
/**
* Make sure that dummy data is populated after the context is
* running to provide DI of the correct dependencies.
*/
@PostConstruct
public void init() {
InitializeEmbeddedDataBaseData initializeEmbeddedDataBaseData =
new InitializeEmbeddedDataBaseData(surveyRepository, participantRepository);
}
/**
* Not using the default {@link org.springframework.http.converter.HttpMessageConverter} implementation
* because setPrettyPrint(true) is not enable by default by Spring Boot.
* @return {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter}
*/
@Bean
public HttpMessageConverter httpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
......@@ -29,14 +50,13 @@ public class WebSurveyTromso extends SpringBootServletInitializer {
return converter;
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(WebSurveyTromso.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
return applicationBuilder.sources(WebSurveyTromso.class);
//Initialize the data for the application.
new InitializeEmbeddedDataBaseData(
ctx.getBean(SurveyRepository.class),
ctx.getBean(ParticipantRepository.class)
);
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(WebSurveyTromso.class);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
/**
* Used to support deployment to external container.
*/
public class WebSurveyTromsoServlet extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
return applicationBuilder.sources(WebSurveyTromso.class);
}
}
\ No newline at end of file
......@@ -33,7 +33,7 @@ public class ParticipantController {
return participantService.findAll();
}
@RequestMapping(value = "survey/{id}", method = RequestMethod.GET)
@RequestMapping(value = "fromsurvey/{id}", method = RequestMethod.GET)
public List<Participant> getParticipantsFromSurveyId(@PathVariable("id") Long id) {
return participantService.findBySurveyId(id);
}
......
package no.nsd.websurvey.tromso.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Replaces the Spring Boot standard error page with a simple, less
......@@ -13,18 +22,44 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping
public class WSTErrorController implements ErrorController {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
private List<String> urlMappings = new ArrayList<>();
/**
* Simple implementation to provide a list of registered controller
* mappings and resources from all {@link org.springframework.web.bind.annotation.RestController}
*/
@PostConstruct
public void getAvailableUrls() {
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap =
this.requestMappingHandlerMapping.getHandlerMethods();
urlMappings.add("Available resources:");
for(Map.Entry<RequestMappingInfo, HandlerMethod> item : handlerMethodMap.entrySet()) {
RequestMappingInfo mapping = item.getKey();
HandlerMethod method = item.getValue();
for(String urlMapping : mapping.getPatternsCondition().getPatterns()) {
urlMappings.add(urlMapping);
}
}
}
@RequestMapping(value = "error", method = RequestMethod.GET)
public String error() {
return "Unknown resource or path";
public List<String> error() {
return urlMappings;
}
/**
* Not using nay errorPath atm.
* Not using any errorPath atm.
* @return always null
*/
@Override
public String getErrorPath() {
return null;
}
}
}
\ No newline at end of file
......@@ -21,7 +21,6 @@ import org.springframework.transaction.annotation.Transactional;
ParticipantServiceTest.class,
SurveyServiceTest.class
})
@Transactional
public class WebSurveyTromsoTestSuite {
}
......@@ -127,7 +127,7 @@ public class RestControllerTests {
RestTemplate template = new RestTemplate();
ResponseEntity<Participant[]> participantsEntity = template.getForEntity(
"http://localhost:8080/participant/survey/"+surveyId, Participant[].class);
"http://localhost:8080/participant/fromsurvey/"+surveyId, Participant[].class);
Participant[] participants = participantsEntity.getBody();
......
......@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.List;
......@@ -22,6 +23,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebSurveyTromso.class)
@IntegrationTest
@WebAppConfiguration
public class ParticipantServiceTest {
@Autowired
......
......@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.List;
......@@ -22,6 +23,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebSurveyTromso.class)
@IntegrationTest
@WebAppConfiguration
public class SurveyServiceTest {
@Autowired
......
Markdown is supported
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