1

my Task ist to test a HttpServlet written in Java, which connects to a database and has the following methods implemented:

doGet(), doPost(), doDelete(), doOptions()

To test the functionality independently from the database connection I've implemented an InMemoryDao which populates a H2 database with test data from a Json file and gets injected into my ServletTest class.

Here's an example of the doGet() Method:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

    boolean all = req.getParameter("all") != null;
    boolean specific = req.getParameter("songId") != null;

    if (all == specific) {
        resp.setStatus(400);
        writePlaintext(resp, "Specify either 'all' or 'songId'.");
        return;
    }

    if (all) doGetAll(req, resp);
    if (specific) doGetSpecific(req, resp);
}

My InMemorySongDao class looks like this:

public class InMemorySongDao extends MysqlSongDao {


public InMemorySongDao() throws SQLException {
    super(new ComboPooledDataSource());
    UUID uuid = UUID.randomUUID();

    // Connect to a unique in-memory database identified by a random uuid
    this.dataSource.setJdbcUrl("jdbc:h2:mem:" + uuid);
    try (PreparedStatement st = this.dataSource.getConnection().prepareStatement(
            "CREATE TABLE songs (" +
                    "id int not null primary key auto_increment," +
                    "title varchar(100) not null," +
                    "artist varchar(100)," +
                    "label varchar(100)," +
                    "released int" +
                    ")")) {
        st.execute();
    }
}

/**
 * Creates a songs dao prefilled with the songs from the given resource.
 */
public InMemorySongDao(String resourceName) throws SQLException, IOException {
    this();
    final ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(getClass().getResource(resourceName));

    // Read array node or use empty node
    ArrayNode array = (rootNode.isArray()) ? (ArrayNode) rootNode : mapper.createArrayNode();
    try (PreparedStatement st = this.dataSource.getConnection().prepareStatement("INSERT INTO songs (id, title, artist, label, released) values (?,?,?,?,?)")) {
        // Iterate over the array and populate the database with the songs
        Iterator<JsonNode> elements = array.elements();
        while (elements.hasNext()) {
            JsonNode node = elements.next();
            if (!node.isObject()) continue;
            st.setInt(1, node.get("id").asInt());
            st.setString(2, node.get("title").asText());
            st.setString(3, node.get("artist").asText());
            st.setString(4, node.get("label").asText());
            st.setInt(5, node.get("released").asInt());
            st.addBatch();
        }
        st.executeBatch();
    }
  }
}

Would be very thankful if somebody could provide me any help with this. Unfortunately I couldn't find any proper examples by research...

Kind Regards, Mic

Mahakala108
  • 85
  • 1
  • 11

0 Answers0