Skip to content
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

[ISSUE #374] Add unit test class. #402

Merged
merged 4 commits into from
Jul 2, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.common;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class LiteMessageTest {

@Test
public void testGetProp() {
LiteMessage message = createLiteMessage();
Assert.assertEquals(2L, message.getProp().size());
}

@Test
public void testSetProp() {
LiteMessage message = createLiteMessage();
Map<String, String> prop = new HashMap<>();
prop.put("key3", "value3");
message.setProp(prop);
Assert.assertEquals(1L, message.getProp().size());
Assert.assertEquals("value3", message.getPropKey("key3"));
}

@Test
public void testAddProp() {
LiteMessage message = createLiteMessage();
message.addProp("key3", "value3");
Assert.assertEquals(3L, message.getProp().size());
Assert.assertEquals("value1", message.getPropKey("key1"));
}

@Test
public void testGetPropKey() {
LiteMessage message = createLiteMessage();
Assert.assertEquals("value1", message.getPropKey("key1"));
}

@Test
public void testRemoveProp() {
LiteMessage message = createLiteMessage();
message.removeProp("key1");
Assert.assertEquals(1L, message.getProp().size());
Assert.assertNull(message.getPropKey("key1"));
}

private LiteMessage createLiteMessage() {
LiteMessage result = new LiteMessage();
Map<String, String> prop = new HashMap<>();
prop.put("key1", "value1");
prop.put("key2", "value2");
result.setProp(prop);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.common.command;

import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.apache.eventmesh.common.protocol.http.body.BaseResponseBody;
import org.apache.eventmesh.common.protocol.http.body.Body;
import org.apache.eventmesh.common.protocol.http.header.Header;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HttpCommandTest {

@Mock
private Header header;

@Mock
private Body body;

private HttpCommand httpCommand;

@Before
public void before() {
httpCommand = new HttpCommand("POST", "1.1", "200");
}

@Test
public void testCreateHttpCommandResponseWithHeaderAndBody() {
HttpCommand command = httpCommand.createHttpCommandResponse(header, body);
Map<String, Object> headerMap = new HashMap<>();
headerMap.put("key1", "value1");
when(header.toMap()).thenReturn(headerMap);
Assert.assertEquals("1.1", command.getHttpVersion());
Assert.assertEquals("POST", command.getHttpMethod());
Assert.assertEquals("200", command.getRequestCode());
Assert.assertEquals("value1", command.getHeader().toMap().get("key1"));
}

@Test
public void testCreateHttpCommandResponseWithRetCodeAndRetMsg() {
HttpCommand command = httpCommand.createHttpCommandResponse(200, "SUCCESS");
Assert.assertThat(((BaseResponseBody) command.getBody()).getRetCode(), is(200));
Assert.assertEquals("SUCCESS", ((BaseResponseBody) command.getBody()).getRetMsg());
}

@Test
public void testAbstractDesc() {
HttpCommand command = httpCommand.createHttpCommandResponse(header, body);
String desc = command.abstractDesc();
Assert.assertTrue(desc.startsWith("httpCommand"));
}

@Test
public void testSimpleDesc() {
HttpCommand command = httpCommand.createHttpCommandResponse(header, body);
String desc = command.simpleDesc();
Assert.assertTrue(desc.startsWith("httpCommand"));
}

@Test
public void testHttpResponse() throws Exception {
HttpCommand command = httpCommand.createHttpCommandResponse(header, body);
DefaultFullHttpResponse response = command.httpResponse();
Assert.assertEquals("keep-alive", response.headers().get(HttpHeaderNames.CONNECTION));
}

@Test
public void testHttpResponseWithREQCmdType() throws Exception {
DefaultFullHttpResponse response = httpCommand.httpResponse();
Assert.assertNull(response);
}
}