Skip to content

Requesting a PR for java coding challenge. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ cmake-build-*/

# File-based project format
*.iws
*.iml
*.ipr

# IntelliJ
out/
Expand Down
88 changes: 87 additions & 1 deletion ANSWERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,96 @@

## A - The entities

There will be 5 entities in the class diagram to represent the ford-example.xml. However
I added a 6th entity called CarBrand that will allow any company such as Ford, Chevy, Audi, BMW,
etc to post XML data to the REST service we will create. Here are the entities:

### 1. CarBrand
CarBrand will contain the name of a vendor and the catalogue. CarBrand will have a One-to-One
mapping with Catalogue. CarBrand will allow us to search for a Model list based on brand name.

### 2. Catalogue
Catalogue will contain a list of Model. Catalogue will have a unidirectional One-to-Many relationship with Model

### 3. Model
Model will contain an Engine, Wheels, SubModel. Model will have a One-to-One relationship with Engine, Wheels and SubModel.

### 4. Engine
Engine will contain 2 fields, power and type. Engine will have a One-to-One relationship with
Model.

### 5. SubModels
SubModel will contain a list of Models. SubModel will have a One-to-One relationship with the
Model that contains it. SubModel has a One-to-Many relationship with the Model list it contains.

### 6. Wheels
Wheels will contain 2 fields, size and type. Wheels will have a One-to-One relationship with
Model.


## B - Ingest the data

#### 1. To read XML file from ingester I used the XStream library which I find very useful and easy to use to read
serialize XML strings to POJOs. I also created DTOs (Data Transfer Objects) to match every entity. The reason
for introducing DTO is simple. We don't want to expose entities to our RestController, instead
our RestController will only know about DTOs.
#### 2. List of DTOs:
CarBrandDto, CalalogueDto, ModelDto, SubModelDto, EngineDto and WheelsDto
Repositories were implemented to persist all entities.
#### 3. The list of repositories are:
CarBrandRepository, CalalogueRepository, ModelRepository, EngineRepository, WheelsRepository
and SubModelRepository
#### 4. Two helper classed ConverterHelper and XStreamHelper were implemented. ConverterHelper to help with converting between entities and DTOs XStreamHelper to help with reading and writing XML.
#### 5. Added an exception CarBrandNotFoundException that will be thrown if the ingester finds that a brand already exists in the database. A brand name is unique and thus a check has to be done to make sure a row does not exist in the database.


## C - Expose data with a RESTful API

#### 1. Add a REST Controller CarBrandController to get model by ID and get models by brand
#### 2. Added and interface ICarBrandService and its implementation service CarBrandService.
#### 3. Added postman_get.model_by_id.png and postman_get.models_by_brand.png to display JSON response from
the 2 endpoints.
#### 4. Added a helper class JacksonHelper to pretty print JSON in the logs
#### 5. Endpoints exposed: GET /cars/{modelId} and GET /cars/byBrand/{name}


## D - Adding images

## E - Improvements
#### 1 To add images for any model I would expose an upload REST controller to consume a modeId and a MultiPart file. A service carBrandService could then find the model based on a modelId and process
the Multipart files and store the contents in byte format on the model entity so the image can be stored in a database or preferable
in a document store such as AWS S3.

#### 2. Add LOB to Model entity class:

@Lob
@Column(name="model_image")
private byte[] modelImage;

#### 3. Create an REST ImageController

@GetMapping(value = "cars/upload/model/{id}" , consumes="Multipart/formdata")
public String uploadModelImage(@PathVariable Long modelId, @RequestParam("image") MultipartFile multipartFile) throws IOException {
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
ModelDto modelDto = carBrandService.getModel(modelId);
modelDto.setFileName(fileName);
modelDto.setModelImage(multipartFile);
return new ResponseEntity<>(carBrandService.saveModelImage(modelDto), HttpStatus.OK);
}

#### 4. CarBrandService would process multipart file and extract contents as bytes and persist using ModelRepository

#### 5. If a user request an endpoint from part C then I would get send the model JSON response and REDIRECT to a download image endpoint to send the image file to the clients machine.

## E - Improvements

#### 1. Creating the CarBrand entity is an improvement because it allow us to store catalogues for any vendor as well as allow us to query by brand.

#### 2. Converting the ingester task to consume JSON instead of XML would improve performance for large files. It would also mean the file size would be smaller for JSON vs XML and that would matter if we intend to also store the files we get from vendors in a document store such as AWS S3.

#### 3. Engine and Wheels entities could be embedded into the Model entity using @Embedded and this would simplify the amount of code we have to write.

#### 4. CarBrand entity can be elimminate as well if we add a brandName filed or attribute on the Catalogue entity. This would simplify the code.

#### 5. Writing unit and integration test for the IngesterTask and the CarBrandService and making sure they scale under load would improve the service.

#### 6. Running the code through Sonar and CheckStyle would help eliminate code smells before the code gets to QA/Prod etc
278 changes: 258 additions & 20 deletions cars/.gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,267 @@
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar
### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

### STS ###
.apt_generated
.classpath
# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath
.project
.settings

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

### Eclipse Patch ###
# Eclipse Core
.project

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Annotation Processing
.apt_generated

.sts4-cache/

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws
*.iml
*.ipr

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# JetBrains templates
**___jb_tmp___

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
.idea/sonarlint

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

### VS Code ###
.vscode/
**/nbproject/private/
**/nbproject/Makefile-*.mk
**/nbproject/Package-*.bash
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

### OSX ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk
Loading