2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.ruby;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.text.ParseException;
32 import java.time.format.DateTimeFormatter;
34 import org.junit.Test;
35 import org.onap.so.client.ruby.beans.Ruby;
37 import static org.apache.commons.lang3.StringUtils.*;
38 import com.fasterxml.jackson.databind.ObjectMapper;
40 public class RubyCheckClientTest {
41 private final String fileLocation = "src/test/resources/org/onap/so/client/ruby/create-ticket/";
42 private static final String REQUEST_ID = "abc123";
43 private static final String SOURCE_NAME = "source-name";
44 private static final String TIME = "test-time";
45 private static final String REASON = "reason";
46 private static final String WORK_FLOW_ID = "work-flow-Id";
47 private static final String NOTIFICATION = "notification";
52 public void verifyRubyCreateTicketRequest() throws IOException, ParseException{
53 String content = this.getJson("create-ticket-request.json");
54 ObjectMapper mapper = new ObjectMapper();
55 Ruby expected = mapper.readValue(content, Ruby.class);
56 RubyClient client = new RubyClient();
57 RubyClient spy = spy(client);
58 when(spy.getTime()).thenReturn(TIME);
59 String actual = spy.buildRequest(REQUEST_ID, SOURCE_NAME, REASON, WORK_FLOW_ID, NOTIFICATION);
60 assertEquals("payloads are equal", mapper.writeValueAsString(expected), actual);
65 public void verifyTimeFormat() {
66 RubyClient client = new RubyClient();
67 String time = client.getTime();
68 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z");
69 formatter.parse(time);
74 public void verifyReasonCharLimit() throws IOException{
75 final String reasonLong = repeat("*", 256);
76 RubyClient client = new RubyClient();
78 client.buildRequest(REQUEST_ID, SOURCE_NAME, reasonLong, WORK_FLOW_ID, NOTIFICATION);
79 fail("Should have thrown IllegalArgumentException but did not!");
81 catch(final IllegalArgumentException e){
82 final String msg = "reason exceeds 255 characters";
83 assertEquals(msg, e.getMessage());
88 public void verifyNotificationCharLimit() throws IOException{
89 final String notificationLong = repeat("*", 1025);
90 RubyClient client = new RubyClient();
92 client.buildRequest(REQUEST_ID, SOURCE_NAME, REASON, WORK_FLOW_ID, notificationLong);
93 fail("Should have thrown IllegalArgumentException but did not!");
95 catch(final IllegalArgumentException e){
96 final String msg = "notification exceeds 1024 characters";
97 assertEquals(msg, e.getMessage());
101 private String getJson(String filename) throws IOException {
102 return new String(Files.readAllBytes(Paths.get(fileLocation + filename)));