From 160049ea6761d007f4f22497e9706b9fe961c3bf Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 18 Dec 2021 22:25:16 -0500 Subject: [PATCH] Seems to be working fine. --- configurationapp/pom.xml | 39 ++++-- .../dao/configurations/ConfigurationDao.java | 117 +++++++++++++++++- .../att/data/configurations/ConfigValue.java | 18 ++- .../ConfigurationController.java | 86 ++++++++++--- .../src/main/resources/application.properties | 3 +- .../src/main/resources/static/css/main.css | 5 + .../src/main/resources/static/index.html | 30 +++-- .../src/main/resources/static/js/app.js | 75 ++++++++++- .../configurations/ConfigurationDaoTest.java | 84 +++++++++++++ 9 files changed, 414 insertions(+), 43 deletions(-) create mode 100644 configurationapp/src/test/java/com/att/dao/configurations/ConfigurationDaoTest.java diff --git a/configurationapp/pom.xml b/configurationapp/pom.xml index 78931d3..f1fce4b 100644 --- a/configurationapp/pom.xml +++ b/configurationapp/pom.xml @@ -5,7 +5,7 @@ com.att configurationapp - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT jar configurationapp @@ -39,16 +39,39 @@ spring-boot-starter-test test + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + - - - org.springframework.boot - spring-boot-maven-plugin - - - configurationapp + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + 1.1.0 + + + org.junit.jupiter + junit-jupiter-engine + 5.1.0 + + + + + configurationapp diff --git a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java index f889558..3a56fc4 100644 --- a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java +++ b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java @@ -1,15 +1,22 @@ package com.att.dao.configurations; -import com.att.data.configurations.ConfigValue; -import org.springframework.stereotype.Service; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.springframework.stereotype.Service; + +import com.att.data.configurations.ConfigValue; + +/** + * Data access layer implementation with basic CRUD operations, stores the data + * in memory + * + */ @Service public class ConfigurationDao { + private class IdProvider { private int currentId; @@ -26,23 +33,123 @@ public int getNextId() { * No DB, so store the configs in a map. */ private Map> currentConfigurations; + + /** + * Internal class to generate IDs. Similar to a database sequence. + */ private IdProvider idProvider; + private static final String NO_NULLS_MSG = "Null values are not allowed"; + public ConfigurationDao() { idProvider = new IdProvider(); currentConfigurations = new HashMap<>(); + + init(); + } + /** + * Returns a list of configurations for the selected month + * + * @param yearMonth + * @return + */ public List getConfigurationsForYearMonth(String yearMonth) { - return new ArrayList<>(); + if (yearMonth == null) { + throw new IllegalArgumentException(NO_NULLS_MSG); + } + + return currentConfigurations.get(yearMonth); } + /** + * Adds a single configuration, does not check for duplicates. + * + * @param yearMonth + * @param value + */ public void addConfiguration(String yearMonth, ConfigValue value) { - int newId = idProvider.getNextId(); + if (yearMonth == null || value == null) { + throw new IllegalArgumentException(NO_NULLS_MSG); + } + + if (value.getConfigId() == -1) { + // preserve existing, but assign new if does not exist + value.setConfigId(idProvider.getNextId()); + + } + List list = currentConfigurations.get(yearMonth); + if (list == null) { + list = new ArrayList(); + } + list.add(value); + currentConfigurations.put(yearMonth, list); } + /** + * Removes all the configurations for the selected month + * + * @param yearMonth + */ public void removeAllConfigurationsForYearMonth(String yearMonth) { + if (yearMonth != null) { + currentConfigurations.remove(yearMonth); + } + } + /** + * Removes all the configurations for the selected month + * + * @param yearMonth + */ + public void removeConfiguration(String yearMonth, int id) { + if (yearMonth != null) { + List list = currentConfigurations.get(yearMonth); + for (ConfigValue c : list) { + if (c.getConfigId() == id) { + list.remove(c); + break; + } + } + } } + + /** + * Removes all the configurations. + */ + public void removeAllConfigurations() { + currentConfigurations = new HashMap<>(); + } + + /** + * Returns a map of all configurations + * + * @return + */ + public Map> getCurrentConfigurations() { + // it should be a DB call here, so let's not bother with creating a deep copy + return currentConfigurations; + } + + private void init() { + // should be equivalent to initial database call with a default query + List list1 = new ArrayList<>(); + + list1.add(new ConfigValue("A", idProvider.getNextId())); + list1.add(new ConfigValue("B", idProvider.getNextId())); + list1.add(new ConfigValue("C", idProvider.getNextId())); + list1.add(new ConfigValue("D", idProvider.getNextId())); + currentConfigurations.put("012018", list1); + + List list2 = new ArrayList<>(); + list2.add(new ConfigValue("A", idProvider.getNextId())); + list2.add(new ConfigValue("C", idProvider.getNextId())); + list2.add(new ConfigValue("F", idProvider.getNextId())); + list2.add(new ConfigValue("G", idProvider.getNextId())); + list2.add(new ConfigValue("H", idProvider.getNextId())); + currentConfigurations.put("022018", list2); + } + } diff --git a/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java b/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java index 55339a3..2b378d7 100644 --- a/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java +++ b/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java @@ -1,25 +1,35 @@ package com.att.data.configurations; /** - * Data Model + * Data Model, contains configuration name and configuration id values. Id is + * equal to -1 by default if not provided. */ public class ConfigValue { private String configName; - private int configId; + private int configId = -1; // not a required field public ConfigValue(String name, int id) { + checkName(name); this.configId = id; this.configName = name; } - public ConfigValue() { - + public ConfigValue(String name) { + checkName(name); + this.configName = name; } public void setConfigName(String name) { + checkName(name); this.configName = name; } + private void checkName(String name) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("null or empty cofig name is not allowed"); + } + } + public String getConfigName() { return this.configName; } diff --git a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java index 995402a..c04addf 100644 --- a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java +++ b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java @@ -1,45 +1,101 @@ package com.att.web.configuarations; -import com.att.dao.configurations.ConfigurationDao; -import com.att.data.configurations.ConfigValue; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +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.bind.annotation.RestController; -import java.util.ArrayList; -import java.util.List; +import com.att.dao.configurations.ConfigurationDao; +import com.att.data.configurations.ConfigValue; @RestController -@RequestMapping(value="/configuration") +@RequestMapping(value = "/configuration") public class ConfigurationController { + private static final String ERROR_OCCURED = "Error occured"; private ConfigurationDao dao; + Logger log = LoggerFactory.getLogger(ConfigurationController.class); @Autowired public ConfigurationController(ConfigurationDao dao) { this.dao = dao; } - @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.GET) + @RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.GET) @ResponseBody - public List getConfigurationsForYearMonth( - @PathVariable("yearMonthNumber") String yearMonth) { + public List getConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) { + try { + log.debug("inside getConfigurationsForYearMonth, yearMonth=" + yearMonth); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); + } - return new ArrayList<>(); + return dao.getConfigurationsForYearMonth(yearMonth); } - @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.DELETE) + @RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.DELETE) public void deleteConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) { try { + log.debug("inside deleteConfigurationsForYearMonth"); + + dao.removeAllConfigurationsForYearMonth(yearMonth); } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); + } + } + + @RequestMapping(value = "/{yearMonthNumber}/{id}", method = RequestMethod.DELETE) + public void deleteConfiguration(@PathVariable("yearMonthNumber") String yearMonth, @PathVariable("id") int id) { + try { + log.debug("inside deleteConfiguration"); + + dao.removeConfiguration(yearMonth, id); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); } } - @RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT }) - public void addConfigurationForYearMonth( - @PathVariable("yearMonthNumber") String yearMonth, - @RequestBody ConfigValue value) { + @RequestMapping(value = "/{yearMonthNumber}", method = { RequestMethod.POST, RequestMethod.PUT }) + public void addConfigurationForYearMonth(@PathVariable("yearMonthNumber") String yearMonth, + @RequestParam String configName) { + try { + log.debug("inside addConfigurationForYearMonth"); + dao.addConfiguration(yearMonth, new ConfigValue(configName)); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); + } + } + @RequestMapping(method = RequestMethod.POST) + public void addConfiguration(@RequestParam String configName) { + try { + // default selection with all months + addConfigurationForYearMonth("", configName); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); + } + } + + @RequestMapping(method = RequestMethod.GET) + @ResponseBody + public Map> getCurrentConfigurations() { + try { + log.debug("inside getCurrentConfigurations"); + + return dao.getCurrentConfigurations(); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); + return null; + } } } diff --git a/configurationapp/src/main/resources/application.properties b/configurationapp/src/main/resources/application.properties index 6c77a8e..b267165 100644 --- a/configurationapp/src/main/resources/application.properties +++ b/configurationapp/src/main/resources/application.properties @@ -1,6 +1,7 @@ server.port=9000 spring.mvc.favicon.enabled=false -logging.level.root=WARN +logging.level.root=ERROR +logging.level.com.att=DEBUG logging.level.org.springframework.web=DEBUG logging.level.org.hibernate=ERROR \ No newline at end of file diff --git a/configurationapp/src/main/resources/static/css/main.css b/configurationapp/src/main/resources/static/css/main.css index d03098f..8c9d424 100644 --- a/configurationapp/src/main/resources/static/css/main.css +++ b/configurationapp/src/main/resources/static/css/main.css @@ -8,4 +8,9 @@ .content { +} + +.tdstyle { + width: 30%; + text-align: center; } \ No newline at end of file diff --git a/configurationapp/src/main/resources/static/index.html b/configurationapp/src/main/resources/static/index.html index 7cd14ba..e296985 100644 --- a/configurationapp/src/main/resources/static/index.html +++ b/configurationapp/src/main/resources/static/index.html @@ -2,10 +2,11 @@ Configuration App - + +

Welcome To The Configuration App

@@ -22,20 +23,35 @@

Welcome To The Configuration App

- +
- - + + + + - - +
Configuration IdConfiguration NameMonthConfiguration IdConfiguration NameAction
+ +
+ +
+
+

Configuration will be added for the selected month.

+
+
+ + +
+
+
+
- +