JavaRush /Java Blog /Random EN /Test task for employment, let's figure it out..
timurnav
Level 21

Test task for employment, let's figure it out..

Published in the Random EN group
Friends, hello everyone. I want to share with you my experience of solving a test task for the position of a Java developer in a Russian company. I’ll say right away that implementing the main functionality of the assignment is not particularly difficult, but as always, the details and little things are important, which prevented me from submitting it on time; they never answered anything about the assignment - their vacancy was already filled when I sent it to them. I suggest you look into the task to see if 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 anyone is interested in this, welcome to the cat. I’ll say right away that I won’t post the whole solution here, but 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 No. 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 (JPG avatar picture) to the server.
    • We save the image in a directory on the server.
    • The server response is the internal URI of the image.
  2. Adding a new user.
    • We transfer the user’s personal data to the server (URI of the image, username, email, etc.).
    • We save the information in the database.
    • The server response is the unique ID of the new user.
  3. Obtaining information about the user.
    • We transmit 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 user status (Online, Offline).
    • We transfer the unique user ID and new status (Online, Offline) to the server.
    • Changing the user status.
    • Server response - unique user ID, new and previous status.
    Note: The server is querying an external API/database. Since this is a simplified test task, it is necessary to implement a “stub” with simulated access and a time delay of 5-10 seconds.
  5. Server statistics.
    • We transfer parameters to the server: 1. client status (Online, Offline or absent), 2. unique ID (timestamp) of the request (may be absent)
    • The server response is a list of users with statuses and picture URIs, as well as a unique ID (timestamp) of the request.
    Note: If the request contains parameters, the server must filter its response by them. If the request contains a unique ID (timestamp) of the request (received earlier), then the server should return only users whose statuses changed after (in time) this unique ID (timestamp).
Mandatory requirements:
- RESTful. - All data is in JSON format. - The API server should be designed taking into account 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):
- Documentation of code. - The API Server architecture must be designed for high load and scaling. - Tests.
Test 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 the test task. Can be downloaded to github.com. - Should contain 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 of: 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), therefore simplifications with explanations and reasons are allowed.
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 in understanding, so I’ll just say that my choice fell on Spring Boot, but not because I had already done something with it, but because I had already completed a real project using Spring ( but Boot was not there, as I understand it because of its simplicity). By server functionality: 1) File downloader. There is basically nothing complicated here, I just needed to figure out how the pictures are generally stored on the server, it turned out that the most convenient way is to simply place them in some special directory. We will look at 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)Changing the user status. the first two points of the task are clear as day, but what about the external request??? It’s impossible to figure it out without 100g, even now I’m not 100% sure if I understood correctly. Details below. 5)Server statistics. This is also interesting. The first point suggests implementing a method with various options for parameters, it is not yet clear how to do this, given that this should be a controller method. the second point asks all users whose status has changed after a moment in time, it seems clear, but there are subtleties.
Getting Started
oh, how many times did I read this phrase while I was working on this task! If you have ever tried to figure out how to set up a project in Spring, but for some reason you have never tried Spring Boot, congratulations, you will be simply 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 - settings for connecting to databases, servlet mapping, etc., etc., so that, for example, to reduce the amount of template code for working with We use JPA/Hibernate for databases, they hide some of the templates, but to configure them, you again need to write an xml file or configuration classes. and if you have a small project, then it turns out that you write no less code, but even vice versa. Next we wrap up working with JPA in Spring; there are many projects, but the most convenient is, of course, Spring Data. This is a very large project that can work with probably everything possible, including JPA and NoSQL and a whole bunch of different projects, it is 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 in the necessary annotations on transactionality, caching, and in special cases google (look at others) some other settings in the context configuration. But at the same time, most novice developers have absolutely no idea how to create a project in Spring. Nobody knows completely how to configure it to run the project and get the result in the browser by following the link starting with localhost:8080/*. And then Spring Boot comes into the picture! It’s better to talk about Spring Boot with a specific example! Let's start with the blank. To create a Spring Boot project, Spring developers came up with a “constructor” for creating templates. You can use it on their website, but it’s much easier to do it in our favorite IDE, Intellij IDEA. 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 the name, and then technologies that we will use, at the first stage we only need WEB - put a tick next to it and then the project is created. For Maven to pull up all the dependencies, we need to open the Maven tab in the idea and click the update button. We received a ready-made application template, which contains all the settings for client-server communication. To get a first impression, let's create a controller class (we've probably all 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 the controller method. 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 going on here. @RestController . exactly the annotation that I wrote about above. We use the rest controller because we want to immediately see the result and don’t want to write .jsp pages (wow), 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 entire class hangs at the /hello address , this means that all methods inside this class have the prefix localhost:8080/hello. Next is the first method of the class, in its own mapping the Http protocol method is indicated - a GET request (read about the Http protocol methods for yourself) What does this 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! 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 launches all the magic hidden in SpringApplication, if you call the context menu on this class, options will appear in the Run line, I recommend launching it early with a green mark, this way the console will look nicer and in the future it will be easier to read the logs directly from her. Let's 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 application for you! If you noticed there is another method in the controller, it has its own address mapping; a placeholder is added to the current address. Which is passed to the method as a parameter by Spring. It is not difficult to guess that the @PathVariable annotation is responsible for this. So on request
localhost:8080/hello/Your name
browser will show
Hello, your name!
We've sorted out the basics of Spring Boot. Next, we’ll attach the database, but that will be in the next post. Thanks to all.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION