diff --git a/.gitignore b/.gitignore index 11ea0a7..9640be3 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ target/ # misc local-test.sh mvncolor.sh -shippable/ +/shippable/ diff --git a/.m2/settings.xml b/.m2/settings.xml deleted file mode 100644 index 051016a..0000000 --- a/.m2/settings.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - admin - {PASSWORD} - central - - - admin - {PASSWORD} - snapshots - - - - - - - - false - - central - libs-release - https://shipdemo.jfrog.io/shipdemo/libs-release - - - - snapshots - libs-snapshot - https://shipdemo.jfrog.io/shipdemo/libs-snapshot - - - - - - false - - central - plugins-release - https://shipdemo.jfrog.io/shipdemo/plugins-release - - - - snapshots - plugins-snapshot - https://shipdemo.jfrog.io/shipdemo/plugins-snapshot - - - artifactory - - - - artifactory - - diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 58808f4..0000000 --- a/Dockerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM tomcat:8.0.20-jre8 - -RUN rm -rf /usr/local/tomcat/webapps/* - -COPY $SHIPPABLE_BUILD_DIR/target/HelloWorld.war /usr/local/tomcat/webapps/HelloWorld.war - -# COPY $SHIPPABLE_BUILD_DIR/target/sample.war /usr/local/tomcat/webapps/sample.war diff --git a/pom.xml b/pom.xml index 30ef6e5..6372acc 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,11 @@ 4.0.0 - com.shipfmrsamplejava - helloworld + com.shippable + properties war 0.0.1-SNAPSHOT - HelloWorld Maven Webapp + Shippable Properties Maven Webapp http://maven.apache.org @@ -13,14 +13,6 @@ ttrahan - - - snapshots - shipdemo-snapshots - https://shipdemo.jfrog.io/shipdemo/libs-snapshot-local - - - junit @@ -47,61 +39,72 @@ ${spring.version} - org.springframework - spring-test - 4.0.5.RELEASE + org.springframework + spring-test + 4.0.5.RELEASE - org.springframework.boot - spring-boot-starter-web - 1.4.0.RELEASE - - - - org.springframework.boot - spring-boot-starter-test - 1.4.0.RELEASE - + org.springframework.boot + spring-boot-starter-web + 1.4.0.RELEASE + + + org.springframework.boot + spring-boot-starter-test + 1.4.0.RELEASE + - org.springframework.boot - spring-boot-starter-tomcat - 1.4.0.RELEASE - provided + com.fasterxml.jackson.core + jackson-core + 2.8.6 + + + com.fasterxml.jackson.core + jackson-databind + 2.8.6 + + + com.fasterxml.jackson.core + jackson-annotations + 2.8.6 + + + org.springframework.boot + spring-boot-starter-tomcat + 1.4.0.RELEASE + provided - - - HelloWorld + Shippable-Properties + + org.jacoco + jacoco-maven-plugin + 0.7.8 + + ${basedir}/target/coverage-reports/jacoco-unit.exec + ${basedir}/target/coverage-reports/jacoco-unit.exec + + + + jacoco-initialize + + prepare-agent + + + + jacoco-site + package + + report + + + + org.apache.maven.plugins maven-compiler-plugin @@ -112,38 +115,21 @@ - org.apache.maven.plugins - maven-surefire-plugin - 2.19.1 - - true - shippable/testresults - - - - org.apache.maven.surefire - surefire-junit47 - 2.19.1 - - - - + true + shippable/testresults + + + + org.apache.maven.surefire + surefire-junit47 + 2.19.1 + + + diff --git a/shippable.yml b/shippable.yml index 0a0d251..442ae21 100644 --- a/shippable.yml +++ b/shippable.yml @@ -3,35 +3,33 @@ language: java jdk: - oraclejdk8 -branches: - only: - - github-beta - -env: - global: - - secure: pTctULZoZ0Ejip41hbaQq9HpfHE/TAIrcWZlZ2yDSQ4FDK4TllXTAZe/A6nWf+0mZ4JKMxYQcNaum52xtFUAmfyg+mpqJksedfZPcLmyev8AqWunUS1DSDi2MZ0po+ou4uUUYE/7ewT42HXyVSi/Rvx6MWsNqzv5k/sTQEq79FGXgnJ91JzWi/Mp1YzA5jm/yvd02OepLcqBXSyYr9QFqlI0xUuLj6Asenp2rT8Yq4wLQOIyQbBCdxaBKbni+tM4QdbfJrCEClK/MB/X0rVymO9eOA1TzoqxszldDbaPXzsum0cbLpPwPP4Ix3R+jLQxvjQ3fC7OM/QgpS0bfLXfoQ== - build: + pre_ci_boot: + image_name: drydock/u14 + image_tag: tip + pull: false + options: '--privileged=true --net=bridge' + ci: + - sudo wget http://mirrors.ibiblio.org/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz + - sudo tar xzf apache-maven-3.3.9-bin.tar.gz -C /usr/local && rm -f apache-maven-3.3.9-bin.tar.gz + - ln -fs /usr/local/apache-maven-$3.3.9/bin/mvn /usr/bin + - echo "export PATH=$PATH:/usr/local/apache-maven-3.3.9/bin" >> $HOME/.bashrc + - wget http://138.197.80.53:8080/docs/appdev/sample/sample.war - mkdir -p shippable/testresults && mkdir -p shippable/codecoverage - - sed -i "s/{PASSWORD}/$PASSWORD/" .m2/settings.xml - - mvn -q -B -s .m2/settings.xml -U clean cobertura:cobertura deploy - - docker build -t ttrahan/javahelloworld:$BRANCH.$BUILD_NUMBER . - - docker push ttrahan/javahelloworld:$BRANCH.$BUILD_NUMBER + - /usr/local/apache-maven-3.3.9/bin/mvn -q -B clean cobertura:cobertura install + - ls -al $SHIPPABLE_BUILD_DIR/target + - jfrog rt u $SHIPPABLE_BUILD_DIR/target/HelloWorld.war builds/HelloWorld.war + - ls $SHIPPABLE_BUILD_DIR/target integrations: hub: - - integrationName: ttrahan-dh - type: docker + - integrationName: avinci-jf + type: artifactory notifications: - - integrationName: trigger-javahello-pipeline + - integrationName: demo-jar-trg type: webhook payload: - - versionName=$BRANCH.$BUILD_NUMBER - branches: - only: - - github-beta + - versionName=$BUILD_NUMBER on_success: always - on_failure: never - on_start: never diff --git a/src/main/java/com/shipfmrsamplejava/helloworld/controller/HelloWorldController.java b/src/main/java/com/shipfmrsamplejava/helloworld/controller/HelloWorldController.java deleted file mode 100644 index b42df53..0000000 --- a/src/main/java/com/shipfmrsamplejava/helloworld/controller/HelloWorldController.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.shipfmrsamplejava.helloworld.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.ModelAndView; - -@Controller -public class HelloWorldController { - String message = "Welcome to Spring MVC, "; - - @RequestMapping("/hello") - public ModelAndView showMessage( - @RequestParam(value = "name", required = false, defaultValue = "World") String name) { - System.out.println("in controller"); - - String environment = System.getenv("ENVIRONMENT"); - - ModelAndView mv = new ModelAndView("helloworld"); - mv.addObject("message", message); - mv.addObject("environment", environment); - if (name == "") { - mv.addObject("name", "World"); - } else { - mv.addObject("name", name); - } - return mv; - } -} diff --git a/src/main/java/com/shippable/properties/controller/PropertiesController.java b/src/main/java/com/shippable/properties/controller/PropertiesController.java new file mode 100644 index 0000000..55b1c4e --- /dev/null +++ b/src/main/java/com/shippable/properties/controller/PropertiesController.java @@ -0,0 +1,137 @@ +package com.shippable.properties.controller; + +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import com.shippable.properties.model.PropertiesVessel; + +//@EnableWebMvc +@Controller +public class PropertiesController { + Map propertiesMap = new HashMap<>(); + Map namesMap = new HashMap<>(); + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView showRoot(HttpServletRequest request) { + return showIndex(request); + } + + private PropertiesVessel getNamesMapProperties(String remoteAddr) { + PropertiesVessel properties = namesMap.get(remoteAddr); + if(properties == null) { + System.out.println("Creating new names properties map for " + remoteAddr); + properties = new PropertiesVessel(); + namesMap.put(remoteAddr, properties); + } + return properties; + } + + private PropertiesVessel getPropertiesMapProperties(String remoteAddr) { + PropertiesVessel properties = propertiesMap.get(remoteAddr); + if(properties == null) { + System.out.println("Creating new properties map for " + remoteAddr); + properties = new PropertiesVessel(); + propertiesMap.put(remoteAddr, properties); + } + return properties; + } + + @RequestMapping(value = "/index", method = RequestMethod.GET) + public ModelAndView showIndex(HttpServletRequest request) { + + PropertiesVessel properties = getNamesMapProperties(request.getRemoteAddr()); + + PropertiesVessel.Property firstNameProperty = properties.getProperty("firstName"); + if(firstNameProperty.getValue() == null) { + properties.setProperty("firstName", "Unknown"); + } + + PropertiesVessel.Property lastNameProperty = properties.getProperty("lastName"); + if(lastNameProperty.getValue() == null) { + properties.setProperty("lastName", "Unknown"); + } + + ModelAndView mv = new ModelAndView("index"); + mv.addObject("firstName", properties.getProperty("firstName").getValue()); + mv.addObject("lastName", properties.getProperty("lastName").getValue()); + return mv; + } + + @RequestMapping(value ="/index", method = RequestMethod.POST) + public ModelAndView postIndex( + @RequestParam(required = false) String firstName, + @RequestParam(required = false) String lastName, + HttpServletRequest request) { + + PropertiesVessel properties = getNamesMapProperties(request.getRemoteAddr()); + + PropertiesVessel.Property firstNameProperty = properties.getProperty("firstName"); + if(firstNameProperty.getValue() == null) { + properties.setProperty("firstName", "Unknown"); + } + if(firstName != null) { + properties.setProperty("firstName", firstName); + } + + PropertiesVessel.Property lastNameProperty = properties.getProperty("lastName"); + if(lastNameProperty.getValue() == null) { + properties.setProperty("lastName", "Unknown"); + } + if(lastName != null) { + properties.setProperty("lastName", lastName); + } + + ModelAndView mv = new ModelAndView("index"); + mv.addObject("firstName", properties.getProperty("firstName").getValue()); + mv.addObject("lastName", properties.getProperty("lastName").getValue()); + return mv; + } + + @RequestMapping(value = "/properties.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody PropertiesVessel getProperties(HttpServletRequest request) { + PropertiesVessel properties = getPropertiesMapProperties(request.getRemoteAddr()); + + return properties; + } + + @RequestMapping(value = "/property", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody PropertiesVessel putProperty(@RequestParam("name") String propertyName, @RequestParam("value") String propertyValue, HttpServletRequest request) { + return setProperty(request.getRemoteAddr(), propertyName, propertyValue); + } + + @RequestMapping(value = "/property", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody PropertiesVessel postProperty(@RequestParam("name") String propertyName, @RequestParam("value") String propertyValue, HttpServletRequest request) { + return setProperty(request.getRemoteAddr(), propertyName, propertyValue); + } + + @RequestMapping(value = "/property", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody PropertiesVessel.Property getPropertyResponse(@RequestParam("name") String propertyName, HttpServletRequest request) { + return getProperty(request.getRemoteAddr(), propertyName); + } + + private PropertiesVessel.Property getProperty(String remoteAddr, String propertyName) { + PropertiesVessel properties = getPropertiesMapProperties(remoteAddr); + + return properties.getProperty(propertyName); + } + + private PropertiesVessel setProperty(String remoteAddr, String propertyName, String propertyValue) { + PropertiesVessel properties = getPropertiesMapProperties(remoteAddr); + + properties.setProperty("hostDateTime", LocalDateTime.now().toString()); + properties.setProperty(propertyName, propertyValue); + return properties; + } + +} diff --git a/src/main/java/com/shippable/properties/model/PropertiesSerializer.java b/src/main/java/com/shippable/properties/model/PropertiesSerializer.java new file mode 100644 index 0000000..e6a2784 --- /dev/null +++ b/src/main/java/com/shippable/properties/model/PropertiesSerializer.java @@ -0,0 +1,22 @@ +package com.shippable.properties.model; + +import java.io.IOException; +import java.util.Set; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.shippable.properties.model.PropertiesVessel.Property; + +public class PropertiesSerializer extends JsonSerializer> { + public void serialize(Set properties, JsonGenerator jgen, SerializerProvider provider) + throws IOException, JsonProcessingException { + jgen.writeStartObject(); + //jgen.writeObjectField("size", properties.size()); + for(Property property : properties) { + jgen.writeObjectField(property.getName(), property.getValue()); + } + jgen.writeEndObject(); + } +} diff --git a/src/main/java/com/shippable/properties/model/PropertiesVessel.java b/src/main/java/com/shippable/properties/model/PropertiesVessel.java new file mode 100644 index 0000000..06b5a5f --- /dev/null +++ b/src/main/java/com/shippable/properties/model/PropertiesVessel.java @@ -0,0 +1,68 @@ +package com.shippable.properties.model; + +import java.util.HashSet; +import java.util.Set; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +public class PropertiesVessel { + @JsonSerialize(using = PropertiesSerializer.class) + private Set properties = new HashSet<>(); + + public Set getProperties() { + return properties; + } + + public Property getProperty(String propertyToFind) { + for (Property property : properties) { + if (property.getName().equals(propertyToFind)) { + return property; + } + } + return new Property(propertyToFind, null); + } + + @JsonSerialize(using = PropertySerializer.class) + public static class Property implements Comparable { + public String getName() { + return name; + } + + public String getValue() { + return value; + } + + private String name; + private String value; + + public Property(String name, String value) { + this.name = name; + this.value = value; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Property)) { + return false; + } + + return this.name.equals(((Property) o).getName()); + } + + @Override + public int compareTo(Property arg0) { + return this.name.compareTo(arg0.getName()); + } + + @Override + public int hashCode() { + return this.name.hashCode(); + } + } + + public void setProperty(String name, String value) { + Property property = new Property(name, value); + properties.remove(property); + properties.add(property); + } +} diff --git a/src/main/java/com/shippable/properties/model/PropertySerializer.java b/src/main/java/com/shippable/properties/model/PropertySerializer.java new file mode 100644 index 0000000..df23b24 --- /dev/null +++ b/src/main/java/com/shippable/properties/model/PropertySerializer.java @@ -0,0 +1,18 @@ +package com.shippable.properties.model; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.shippable.properties.model.PropertiesVessel.Property; + +public class PropertySerializer extends JsonSerializer { + public void serialize(Property property, JsonGenerator jgen, SerializerProvider provider) + throws IOException, JsonProcessingException { + jgen.writeStartObject(); + jgen.writeObjectField(property.getName(), property.getValue()); + jgen.writeEndObject(); + } +} diff --git a/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/src/main/webapp/WEB-INF/dispatcher-servlet.xml index 58abbbf..24b9b3c 100644 --- a/src/main/webapp/WEB-INF/dispatcher-servlet.xml +++ b/src/main/webapp/WEB-INF/dispatcher-servlet.xml @@ -1,14 +1,21 @@ - - +http://www.springframework.org/schema/context/spring-context-3.0.xsd +http://www.springframework.org/schema/mvc +http://www.springframework.org/schema/mvc/spring-mvc.xsd"> + + + + + + @@ -18,4 +25,20 @@ http://www.springframework.org/schema/context/spring-context-3.0.xsd"> .jsp + + + + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/views/helloworld.jsp b/src/main/webapp/WEB-INF/views/helloworld.jsp deleted file mode 100644 index 0bb94fc..0000000 --- a/src/main/webapp/WEB-INF/views/helloworld.jsp +++ /dev/null @@ -1,15 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=ISO-8859-1" - pageEncoding="ISO-8859-1"%> - - - - -Java - Spring 4 MVC -HelloWorld - - -
-

Hello World - ${environment}

-

${message}${name}!

-
- - diff --git a/src/main/webapp/WEB-INF/views/index.jsp b/src/main/webapp/WEB-INF/views/index.jsp new file mode 100644 index 0000000..89bbbce --- /dev/null +++ b/src/main/webapp/WEB-INF/views/index.jsp @@ -0,0 +1,27 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Hello! + + +
+ +
+

Hello, ${firstName} ${lastName}!

+

+ +

+ <%-- submit --%> + +

+
+
+ + + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index d65faee..257c44d 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -1,29 +1,20 @@ - - - - - Archetype Created Web Application - - - dispatcher - org.springframework.web.servlet.DispatcherServlet - 1 - - - - dispatcher - / - - - - contextConfigLocation - /WEB-INF/dispatcher-servlet.xml - - - - org.springframework.web.context.ContextLoaderListener - - + + + Archetype Created Web Application + + dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + dispatcher + / + + + contextConfigLocation + /WEB-INF/dispatcher-servlet.xml + + + org.springframework.web.context.ContextLoaderListener + + \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index ec09725..b1454b1 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,22 +4,24 @@ -Spring 4 MVC - HelloWorld Index Page +Hello! -
-

Hello World

-

- - -

- <%-- submit --%> -
- -
-

-
+
+
+

Hello, ${firstName} ${lastName}!

+

+ +

+ <%-- submit --%> + +

+
+
diff --git a/src/test/java/ControllerTest.java b/src/test/java/ControllerTest.java deleted file mode 100644 index 516f6f0..0000000 --- a/src/test/java/ControllerTest.java +++ /dev/null @@ -1,84 +0,0 @@ -import org.junit.Assert; -import org.junit.Test; -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.web.WebAppConfiguration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.web.context.WebApplicationContext; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.http.MediaType; -import org.springframework.test.util.ReflectionTestUtils; -import com.shipfmrsamplejava.helloworld.controller.HelloWorldController; -import com.shipfmrsamplejava.helloworld.controller.HelloWorldController.*; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; - -// -// public class SimpleTestNameInput { -// -// @Test -// public void testNameInput() { -// HelloWorldController hello = new HelloWorldController(); -// ModelAndView result = hello.showMessage("Test Name"); -// Assert.assertEquals("Test Name", result.name); -// } -// -// @Test -// public void testNoNameInput() { -// HelloWorldController hello = new HelloWorldController(); -// ModelAndView result = hello.showMessage(""); -// Assert.assertEquals("World", result.name); -// } -// } - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -// @WebAppConfiguration -@SpringBootTest -public class ControllerTest { - - @Autowired - private HelloWorldController helloWorldController; - - @InjectMocks - HelloWorldController controller; - - MockMvc mockMvc; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - mockMvc = MockMvcBuilders.standaloneSetup(controller) - .build(); - } - - @Test - public void testNameInput() throws Exception { - this.mockMvc.perform(get("/hello") - .param("name", "Tee") - .accept(MediaType.TEXT_HTML) - ) .andExpect(status().isOk()) - .andExpect(view().name("helloworld")) - .andExpect(model().attribute("name", "Tee")); - } - @Test - public void testNullNameInput() throws Exception { - this.mockMvc.perform(get("/hello") - .param("name", "") - .accept(MediaType.TEXT_HTML) - ) .andExpect(status().isOk()) - .andExpect(view().name("helloworld")) - .andExpect(model().attribute("name", "World")); - } -} diff --git a/src/test/java/PropertiesControllerTest.java b/src/test/java/PropertiesControllerTest.java new file mode 100644 index 0000000..1312a0f --- /dev/null +++ b/src/test/java/PropertiesControllerTest.java @@ -0,0 +1,137 @@ +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +import com.shippable.properties.controller.PropertiesController; +import com.shippable.properties.model.PropertiesVessel; + +import junit.framework.Assert; + +// +// public class SimpleTestNameInput { +// +// @Test +// public void testNameInput() { +// HelloWorldController hello = new HelloWorldController(); +// ModelAndView result = hello.showMessage("Test Name"); +// Assert.assertEquals("Test Name", result.name); +// } +// +// @Test +// public void testNoNameInput() { +// HelloWorldController hello = new HelloWorldController(); +// ModelAndView result = hello.showMessage(""); +// Assert.assertEquals("World", result.name); +// } +// } + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +// @WebAppConfiguration +@SpringBootTest +public class PropertiesControllerTest { + + @Autowired + private PropertiesController propertiesController; + + @InjectMocks + PropertiesController controller; + + MockMvc mockMvc; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/WEB-INF/jsp/view/"); + viewResolver.setSuffix(".jsp"); + + mockMvc = MockMvcBuilders.standaloneSetup(controller).setViewResolvers(viewResolver).build(); + } + + @Test + public void testRoot() throws Exception { + this.mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()) + .andExpect(view().name("index")); + } + + @Test + public void testGetIndex() throws Exception { + this.mockMvc.perform(get("/index").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()); + } + + @Test + public void testPropertiesMapSet() throws Exception { + this.mockMvc.perform(get("/index").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()); + this.mockMvc.perform(get("/index").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()); + } + + @Test + public void testPostIndex() throws Exception { + this.mockMvc.perform(post("/index").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()) + .andExpect(model().attribute("firstName", "Unknown")).andExpect(model().attribute("lastName", "Unknown")); + this.mockMvc.perform(post("/index").param("firstName", "testFirst").param("lastName", "testLast").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()) + .andExpect(model().attribute("firstName", "testFirst")).andExpect(model().attribute("lastName", "testLast")); + this.mockMvc.perform(post("/index").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()) + .andExpect(model().attribute("firstName", "testFirst")).andExpect(model().attribute("lastName", "testLast")); + } + + @Test + public void testPutProperty() throws Exception { + this.mockMvc.perform(put("/property").param("name", "testProperty").param("value", "testValue").accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk());; + } + + @Test + public void testGetProperties() throws Exception { + this.mockMvc.perform(get("/properties.json").accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()); + } + + @Test + public void testSetProperty() throws Exception { + this.mockMvc.perform(post("/property").param("name", "testProperty").param("value", "testValue").accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()); + this.mockMvc.perform(get("/property").param("name", "testProperty").param("value", "testValue").accept(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()); + } + + @Test + public void testPropertyVessel() throws Exception { + PropertiesVessel pv = new PropertiesVessel(); + pv.setProperty("testName", "testValue"); + PropertiesVessel.Property p = pv.getProperty("testName"); + Assert.assertSame("testValue", p.getValue()); + } + + @Test + public void testPropertyVesselProperty() throws Exception { + PropertiesVessel.Property p1 = new PropertiesVessel.Property("testName", "testValue"); + PropertiesVessel.Property p2 = new PropertiesVessel.Property("testName", "testValue"); + PropertiesVessel.Property p3 = new PropertiesVessel.Property("testName2", "testValue"); + PropertiesVessel.Property p4 = new PropertiesVessel.Property("testName", "testValue2"); + + Assert.assertEquals(p1, p2); + Assert.assertNotSame(p1, p3); + Assert.assertNotSame(p1, p4); + Assert.assertEquals(p1.equals("string"), false); + Assert.assertEquals(p1.compareTo(p2), 0); + Assert.assertEquals(p1.compareTo(p3), -1); + Assert.assertEquals(p3.compareTo(p1), 1); + + } +} diff --git a/src/test/resources/ControllerTest-context.xml b/src/test/resources/PropertiesControllerTest-context.xml similarity index 72% rename from src/test/resources/ControllerTest-context.xml rename to src/test/resources/PropertiesControllerTest-context.xml index c33b942..d13abab 100644 --- a/src/test/resources/ControllerTest-context.xml +++ b/src/test/resources/PropertiesControllerTest-context.xml @@ -4,6 +4,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - +