Initial Commit

This commit is contained in:
2018-09-06 22:24:42 +10:00
commit 557435e166
14 changed files with 720 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan({"controller","service"})
public class PollsSpringApplication {
public static void main(String[] args) {
SpringApplication.run(PollsSpringApplication.class, args);
}
}

View File

@@ -0,0 +1,44 @@
package controller;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import model.Poll;
import model.PollChoice;
import service.PollService;
@RestController
@RequestMapping("/questions")
public class PollController {
@Autowired
PollService pollService;
@RequestMapping("/")
public ArrayList<Poll> getAll(){
return pollService.getAllPolls();
}
@RequestMapping("testing")
public String getDefault(){
return "hello";
}
@RequestMapping("{id}")
public Poll getCurrentPoll(@PathVariable("") int id) {
return pollService.getPoll(id);
}
@RequestMapping("{id:[\\d]+}/choices/{choice}")
public PollChoice getCurrentPoll(@PathVariable("") int id, @PathVariable("") int choice) {
return pollService.getcurrentPollChoice(id,choice);
}
}

View File

@@ -0,0 +1,50 @@
package model;
import java.util.ArrayList;
public class Poll {
String url;
ArrayList<PollChoice> PollChoice;
String question;
String publishStamp;
public Poll(String url, ArrayList<PollChoice> pollChoice, String question, String publishStamp) {
this.url = url;
PollChoice = pollChoice;
this.question = question;
this.publishStamp = publishStamp;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public ArrayList<PollChoice> getPollChoice() {
return PollChoice;
}
public void setPollChoice(ArrayList<PollChoice> pollChoice) {
PollChoice = pollChoice;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getPublishStamp() {
return publishStamp;
}
public void setPublishStamp(String publishStamp) {
this.publishStamp = publishStamp;
}
}

View File

@@ -0,0 +1,38 @@
package model;
public class PollChoice {
int votes;
String choice;
String url;
public PollChoice(int votes, String choice, String url) {
this.votes = votes;
this.choice = choice;
this.url = url;
}
public int getVotes() {
return votes;
}
public void setVotes(int votes) {
this.votes = votes;
}
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@@ -0,0 +1,67 @@
package service;
import java.util.Date;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
import model.Poll;
import model.PollChoice;
@Service
public class PollService {
ArrayList<Poll> pollsArray = new ArrayList<Poll>();
Date date = new Date();
public PollService(){
String url = "/questions/1";
String question = "Which is your favourite State?";
PollChoice pollChoice1= new PollChoice(86,"QLD","/questions/1/choices/1");
PollChoice pollChoice2= new PollChoice(43,"NSW","/questions/1/choices/2");
PollChoice pollChoice3= new PollChoice(79,"VIC","/questions/1/choices/3");
PollChoice pollChoice4= new PollChoice(51,"WA","/questions/1/choices/4");
PollChoice pollChoice5= new PollChoice(33,"TAS","/questions/1/choices/5");
PollChoice pollChoice6= new PollChoice(12,"ACT","/questions/1/choices/6");
PollChoice pollChoice7= new PollChoice(21,"SA","/questions/1/choices/7");
PollChoice pollChoice8= new PollChoice(18,"NT","/questions/1/choices/8");
ArrayList<PollChoice> pollChoiceArrayList = new ArrayList<PollChoice>();
pollChoiceArrayList.add(pollChoice1);
pollChoiceArrayList.add(pollChoice2);
pollChoiceArrayList.add(pollChoice3);
pollChoiceArrayList.add(pollChoice4);
pollChoiceArrayList.add(pollChoice5);
pollChoiceArrayList.add(pollChoice6);
pollChoiceArrayList.add(pollChoice7);
pollChoiceArrayList.add(pollChoice8);
String publishStamp = String.valueOf(date);
Poll poll = new Poll(url,pollChoiceArrayList, question, publishStamp);
pollsArray.add(poll);
}
public Poll getPoll(int id){
if (pollsArray == null) {
return null;
}else {
return pollsArray.get((id - 1));
}
}
public ArrayList<Poll> getAllPolls(){
return pollsArray;
}
public PollChoice getcurrentPollChoice(int question, int pollChoice){
Poll currentPoll = pollsArray.get((question - 1));
ArrayList<PollChoice> currentPollChoices = currentPoll.getPollChoice();
PollChoice currentPollChoice = currentPollChoices.get((pollChoice - 1));
return currentPollChoice;
}
}

View File

@@ -0,0 +1,16 @@
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PollsSpringApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,45 @@
package controller;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import com.example.demo.PollsSpringApplication;
import net.minidev.json.JSONObject;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PollsSpringApplication.class)
@AutoConfigureMockMvc
public class PollControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception{
this.mockMvc.perform(get("/questions/"))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$[0].question",is("Which is your favourite State?")))
.andExpect(jsonPath("$[0].url",is("/questions/1")))
.andExpect(jsonPath("$[0].pollChoice[0].votes",is(86)))
.andExpect(jsonPath("$[0].pollChoice[0].choice",is("QLD")))
.andExpect(jsonPath("$[0].pollChoice[0].url",is("/questions/1/choices/1")));
}
}