JavaRush /Java Blog /Random EN /Employment test, let's figure it out ..
timurnav
Level 21

Employment test, let's figure it out ..

Published in the Random EN group
Friends, hello everyone. I want to share with you the experience of solving a test task for the position of a java developer in a Russian company. I must say right away that it is not particularly difficult to implement the main functionality of the assignment, but as always, the details and little things that prevented me from submitting it on time are important, they didn’t answer me on the assignment - the vacancy they had already closed was when I sent them. I propose to deal with the task whether I did everything that was required of me. And for those who have no idea how to make it, I will add a lot of water about how I dealt with it. If this is suddenly interesting to anyone - welcome under cat. I’ll say right away that I won’t post the whole solution here, but for some reason there will be a lot of explanations for beginners, if anyone is not interested in reading my outpourings, here’s the project on github. I’ll start with the text of the task itself.
Test task №1
Description: API server (JSON HTTP API) Development tools: Java Framework: Play Framework 2.4 (or higher) or Spring boot 1.2.3 (or higher) Database: MySQL Protocol: HTTP, port 80 Functionality (requests):
  1. Loader.
    • We transfer the file to the server (JPG avatar picture).
    • We save the image in a directory on the server.
    • The server's response is the internal URI of the picture.
  2. Adding a new user.
    • We transfer the user's personal data to the server (image URI, username, email, etc.).
    • We store information in a database.
    • The server's response is the unique ID of the new user.
  3. Getting information about the user.
    • We pass a unique user ID to the server.
    • Reading information from the database.
    • The server's response is the user's personal data (see above).
  4. Changing the user status (Online, Offline).
    • We send a unique user ID and a new status (Online, Offline) to the server.
    • Change the user's status.
    • Server response - unique user ID, new and previous status.
    Note: The server is making a request to an external API/database. Since this is a simplified test task, it is necessary to implement a "stub" with an imitation of access and a time delay of 5-10 seconds.
  5. Server statistics.
    • We pass parameters to the server: 1. client status (Online, Offline or absent), 2. unique ID (timestamp) of the request (may be absent)
    • The server's response is a list of users with statuses and image URI, as well as a unique ID (timestamp) of the request.
    Note: If the request contains parameters, then the server must filter its response by them. If the request contains a unique ID (timestamp) of the request (obtained earlier), then the server should return only users whose statuses have changed after (in time) this unique ID (timestamp).
Mandatory requirements:
- RESTful. - All data in JSON format. - The API server should be designed to ensure that requests 3 and 5 have the highest priority (relative to requests 1, 2, 4) and must be completed as quickly as possible. - Error processing.
Optional requirements (desirable):
- Code documentation. - The architecture of the API Server should be designed for high load and scaling. - Tests.
Test task result:
- The result of the test task must be provided in an archive and with detailed instructions for its deployment. It is advisable to attach a Dockerfile to build a Docker container for a test task. Can be downloaded from github.com. - Should contain a brief documentation of the created API (list of requests, request parameters, request formats, response formats, etc.). - Information about the time spent on the test task in the context: design, programming, documentation, etc. Please note that this test task is intended only to assess knowledge and skills, and does not aim to create a finished product (API server), so simplifications are allowed with an explanation and indication of reasons.
attentive and experienced programmers can skip the next section, here I will deal with the text of the task itself. The "header" of the task does not cause any difficulties with perception, so I'll just say that my choice fell on Spring Boot, but not because I've done something on it before, but because I've already gone through a real project using Spring(but Boot was not there, as I understand it, because of its simplicity). By server functionality: 1) File uploader. There is nothing fundamentally complicated here, I just needed to figure out how pictures are generally stored on the server, it turned out that the most convenient way is to simply place them in some directory specially designated for this. We will analyze the specific implementation below. 2) Adding a new user, a simple operation, if you have ever made CRUD applications, then he will support me, if not, you will see everything below. 3) Obtaining information about the user. no questions - everything is clear. 4) Change the status of the user. the first two points of the task are clear as day, but what about the external request ??? here without 100g you can’t figure it out, even now I’m not 100% sure if I understood correctly. Details below. 5) Server statistics. It's also interesting here. the first point is to implement the method with different options for parameters, while it is not clear how to do this, given that it should be a controller method. The second point is asked of all users whose status has changed after a moment in time, it seems understandable, but there are subtleties.
Getting Started
oh, how many times I read this phrase while I dealt with this task! If you have ever tried to figure out how to set up a Spring project, but for some reason have never tried Spring Boot, congratulations, you will simply be delighted with what I will write below. I read somewhere that programmers used to transfer a very large amount of code from project to project, this is template code - database connection settings, servlet mapping, and so on, so, for example, to reduce the amount of template code for working with we use JPA/Hibernate databases, they hide part of the templates, but to set them up again, you need to write an xml file or configuration classes. and if you have a small project, it turns out that you write no less code, and even vice versa. Next, we wrap work with JPA in Spring, there are many projects, but the most convenient one is, of course, Spring Data. This is a very large project that can work with probably everything and JPA and NoSQL a whole bunch of different projects, it's incredibly magical, we will use it in our project. Using Spring, we almost get rid of the database connection settings, Spring does everything for us, we just need to stick the necessary transactional, caching annotations and, in special cases, google (peep from others) some more settings in the context configuration. But at the same time, most novice developers have absolutely no idea how to create a project in Spring. No one knows completely how to set it up in order to run the project and get the result in the browser by clicking on the link starting with localhost:8080/*. This is where Spring Boot comes into play! It’s better to talk about Spring Boot with a specific example! Let's start with the workpiece. In order to create a Spring Boot project, the Spring developers came up with a templating "builder". You can use it on their website, but it's much easier to do it in our favorite Intellij IDEA IDE. And so: File-> New-> Project In the window, go to the Spring Initializr tab, jdk should be set in it, and the URL https://start.spring.io, check the Internet connection, then you will need to select a name, and then technologies that we will use, at the first stage, we only need WEB - put a check next to it and then a project is created. In order for maven to pull up all the dependencies, we need to open the Maven tab in the idea and click the update button. We have received a ready-made application template, in which all settings for client-server communication are buried. To get a first impression, let's create a controller class (everyone has probably heard about MVC). In all Spring applications, controllers have a fairly simple design - this is a class that is marked with the @Controller annotation (prefixes are possible, for example, @RestController), this class is responsible for processing incoming requests. In order for the controller to recognize a request for some address, you need to map this address to a controller method. Below is the code of this class, while we will work without a hierarchy of directories, so we will put it in one package with the class created automatically by Initializr, I call it DemoApplication. In all Spring applications, controllers have a fairly simple design - this is a class that is marked with the @Controller annotation (prefixes are possible, for example, @RestController), this class is responsible for processing incoming requests. In order for the controller to recognize a request for some address, you need to map this address to a controller method. Below is the code of this class, while we will work without a hierarchy of directories, so we will put it in one package with the class created automatically by Initializr, I call it DemoApplication. In all Spring applications, controllers have a fairly simple design - this is a class that is marked with the @Controller annotation (prefixes are possible, for example, @RestController), this class is responsible for processing incoming requests. In order for the controller to recognize a request for some address, you need to map this address to a controller method. Below is the code of this class, while we will work without a hierarchy of directories, so we will put it in one package with the class created automatically by Initializr, I call it DemoApplication. in order for the controller to recognize a request for some address, you need to map this address to a controller method. Below is the code of this class, while we will work without a hierarchy of directories, so we will put it in one package with the class created automatically by Initializr, I call it DemoApplication. in order for the controller to recognize a request for some address, you need to map this address to a controller method. Below is the code of this class, while we will work without a hierarchy of directories, so we will put it in one package with the class created automatically by Initializr, I call it DemoApplication. 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.RestController; @RestController @RequestMapping(value = "/hello") public class DemoController { @RequestMapping(method = RequestMethod.GET) public String halloWorld() { return "Hello World!"; } @RequestMapping(value = "/{name}", method = RequestMethod.GET) public String halloName(@PathVariable("name") String name) { return "Hello, " + name + "!"; } } let's figure out what's here. @RestController . just the annotation that I wrote about above. we use the restcontroller because we want to see the result immediately and do not want to write .jsp pages (fu-what), it will be easier for us to immediately see the result in the browser as a string. @RequestMapping is just a binding to an address. the public address prefix will be: localhost:8080. As we can see the whole class hangs on the address /hello, which means that all methods within this class are prefixed with localhost:8080/hello. Next, the first method of the class, in its own mapping, the Http protocol method is specified - a GET request (read about the Http protocol methods yourself) What does it all mean? by making a GET request to the address localhost:8080/hello - we will receive a response in the form of the string "Hello World!", let's check it out! In the DemoApplication class, there is one cool annotation that can be said to single-handedly launch the entire spring context - @SpringBootApplication. The main method of this class becomes magical, it just starts all the magic hidden in SpringApplication, if you call the context menu on this class, options will appear in the Run line, I recommend starting early with a green label, so the console will look nicer and in the future it will be easier to read the logs directly from her. We launch the application. when the console output stops, you should see in the console
2015-09-02 09:25:36.895 INFO 5844 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2015-09-02 09:25:36.900 INFO 5844 --- [ main] demo.DemoApplication : Started DemoApplication in **** seconds (JVM running for 15.501)
where "****" is the duration of the application launch :) after that, in any browser (or curl, or whatever you use?) you need to type the address to which the controller method was mapped
localhost:8080/hello
The browser should display the canonical
hello world!
here's a web app for you! If you notice there is another method in the controller, it has its own address mapping, a placeholder is added to the current address. Which spring is passed to the method as a parameter. It's not hard to guess what the @PathVariable annotation is responsible for. So per request
localhost:8080/hello/your name
the browser will show
Hello, your name!
We figured out the basics of Spring Boot. Next, we will fasten the database, but this will be in the next post. Thanks to all.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION